cvitool 1.0.771 → 1.0.772

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -107,6 +107,7 @@ declare function execCmdCommand(command: string, timeout?: number): Promise<unkn
107
107
  */
108
108
  declare function getValueType(value: any): "string" | "number" | "boolean" | "undefined" | "object" | "function" | "buffer" | "array" | "null" | "unkonw";
109
109
  /**
110
+ * ### 废弃 ###
110
111
  * 获取媒体文件类型(此方法取文件前30个字节进行检查)
111
112
  * @param target
112
113
  * @returns
@@ -172,4 +173,15 @@ declare function getObjPathMapValue(obj: CustomObject): CustomObject;
172
173
  * @returns
173
174
  */
174
175
  declare function getURLExtname(url: string): string;
175
- export { randomStringOptions, getMediaFileTypeRes, CustomObject, randomString, encryptCBC, decryptCBC, md5, execCmdCommand, getValueType, getMediaFileType, checkURLResource, writeJsonFileSync, readJsonFileSync, uuid, validate, RegStr, getObjPathMapValue, getURLExtname };
176
+ /**
177
+ * 通过file-type库获取文件类型
178
+ * @param input
179
+ * @param reqUrlTimeout
180
+ * @returns
181
+ */
182
+ declare function getFileType(input: string | Buffer | Readable, reqUrlTimeout?: number): Promise<{
183
+ type: string;
184
+ extname: string;
185
+ mime: string;
186
+ }>;
187
+ export { randomStringOptions, getMediaFileTypeRes, CustomObject, randomString, encryptCBC, decryptCBC, md5, execCmdCommand, getValueType, getMediaFileType, checkURLResource, writeJsonFileSync, readJsonFileSync, uuid, validate, RegStr, getObjPathMapValue, getURLExtname, getFileType };
@@ -24,11 +24,14 @@ exports.uuid = uuid;
24
24
  exports.validate = validate;
25
25
  exports.getObjPathMapValue = getObjPathMapValue;
26
26
  exports.getURLExtname = getURLExtname;
27
+ exports.getFileType = getFileType;
27
28
  const crypto_1 = require("crypto");
28
29
  const child_process_1 = require("child_process");
29
30
  const fs_1 = require("fs");
30
31
  const hgo = require("./hgo");
32
+ const stream_1 = require("stream");
31
33
  const path = require("path");
34
+ const file_type_1 = require("file-type");
32
35
  const RegStr = {
33
36
  zzs: '^[1-9]\\d*$',
34
37
  zxs: (digit) => `^(0|[1-9]\\d*)(\\.\\d{1,${digit}})$`,
@@ -217,6 +220,7 @@ function getValueType(value) {
217
220
  return type;
218
221
  }
219
222
  /**
223
+ * ### 废弃 ###
220
224
  * 获取媒体文件类型(此方法取文件前30个字节进行检查)
221
225
  * @param target
222
226
  * @returns
@@ -623,3 +627,39 @@ function pickObjPathMapValue(obj, path, pathMapValue) {
623
627
  function getURLExtname(url) {
624
628
  return path.extname(new URL(url).pathname).substring(1);
625
629
  }
630
+ /**
631
+ * 通过file-type库获取文件类型
632
+ * @param input
633
+ * @param reqUrlTimeout
634
+ * @returns
635
+ */
636
+ function getFileType(input_1) {
637
+ return __awaiter(this, arguments, void 0, function* (input, reqUrlTimeout = 10 * 1000) {
638
+ let stream = input;
639
+ if (getValueType(input) === 'string') {
640
+ if (input.startsWith('http') || input.startsWith('https')) {
641
+ const res = yield hgo.get(input, { resType: 'stream', timeout: reqUrlTimeout });
642
+ stream = res.resBody;
643
+ }
644
+ else {
645
+ stream = (0, fs_1.createReadStream)(input);
646
+ }
647
+ }
648
+ if (getValueType(input) === 'buffer') {
649
+ stream = stream_1.Readable.from([input]);
650
+ }
651
+ const { ext = '', mime = '' } = yield (0, file_type_1.fromStream)(stream);
652
+ if (!ext || !mime) {
653
+ return {
654
+ type: 'unknow',
655
+ extname: 'unknow',
656
+ mime: 'unknow'
657
+ };
658
+ }
659
+ return {
660
+ type: mime.split('/')[0],
661
+ extname: ext,
662
+ mime
663
+ };
664
+ });
665
+ }
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "cvitool",
3
- "version": "1.0.771",
3
+ "version": "1.0.772",
4
4
  "description": "cvitool",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "npm run build && node ./build/test/index.test.js",
8
8
  "build": "rm -rf build && tsc"
9
9
  },
10
- "dependencies": {},
10
+ "dependencies": {
11
+ "file-type": "16.5.4"
12
+ },
11
13
  "devDependencies": {
12
14
  "@types/node": "20.10.4",
13
15
  "@typescript-eslint/eslint-plugin": "5.54.0",
package/src/cutil.ts CHANGED
@@ -9,10 +9,11 @@ import {
9
9
  import {
10
10
  spawn
11
11
  } from 'child_process';
12
- import { writeFileSync, readFileSync } from 'fs';
12
+ import { writeFileSync, readFileSync, createReadStream } from 'fs';
13
13
  import * as hgo from './hgo';
14
14
  import { Readable } from 'stream';
15
15
  import * as path from 'path';
16
+ import { fromStream } from 'file-type';
16
17
 
17
18
  interface randomStringOptions {
18
19
  special?: boolean,
@@ -238,6 +239,7 @@ function getValueType(value: any) {
238
239
  }
239
240
 
240
241
  /**
242
+ * ### 废弃 ###
241
243
  * 获取媒体文件类型(此方法取文件前30个字节进行检查)
242
244
  * @param target
243
245
  * @returns
@@ -651,6 +653,40 @@ function getURLExtname(url: string) {
651
653
  return path.extname(new URL(url).pathname).substring(1);
652
654
  }
653
655
 
656
+ /**
657
+ * 通过file-type库获取文件类型
658
+ * @param input
659
+ * @param reqUrlTimeout
660
+ * @returns
661
+ */
662
+ async function getFileType(input: string | Buffer | Readable, reqUrlTimeout = 10 * 1000) {
663
+ let stream: Readable = input as Readable;
664
+ if (getValueType(input) === 'string') {
665
+ if ((input as string).startsWith('http') || (input as string).startsWith('https')) {
666
+ const res = await hgo.get(input as string, { resType: 'stream', timeout: reqUrlTimeout });
667
+ stream = res.resBody as Readable;
668
+ } else {
669
+ stream = createReadStream(input as string);
670
+ }
671
+ }
672
+ if (getValueType(input) === 'buffer') {
673
+ stream = Readable.from([input as Buffer]);
674
+ }
675
+ const { ext = '', mime = '' } = await fromStream(stream);
676
+ if (!ext || !mime) {
677
+ return {
678
+ type: 'unknow',
679
+ extname: 'unknow',
680
+ mime: 'unknow'
681
+ };
682
+ }
683
+ return {
684
+ type: mime.split('/')[0],
685
+ extname: ext,
686
+ mime
687
+ };
688
+ }
689
+
654
690
  export {
655
691
  randomStringOptions,
656
692
  getMediaFileTypeRes,
@@ -669,5 +705,6 @@ export {
669
705
  validate,
670
706
  RegStr,
671
707
  getObjPathMapValue,
672
- getURLExtname
708
+ getURLExtname,
709
+ getFileType
673
710
  };