cvitool 1.0.71 → 1.0.72

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.
@@ -1,6 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ /// <reference types="node" />
3
4
  import { Encoding } from 'crypto';
5
+ import { ReadStream } from 'fs';
4
6
  interface randomStringOptions {
5
7
  special?: boolean;
6
8
  lowercase?: boolean;
@@ -8,9 +10,15 @@ interface randomStringOptions {
8
10
  number?: boolean;
9
11
  specials?: string;
10
12
  }
13
+ interface getMediaFileTypeRes {
14
+ type: string;
15
+ extname: string;
16
+ }
11
17
  declare function randomString(length: number, options?: randomStringOptions): string;
12
18
  declare function encryptCBC(data: string, length: 128 | 192 | 256, key: Buffer, iv: Buffer, inputEncoding?: Encoding): string;
13
19
  declare function decryptCBC(encryptData: string, length: 128 | 192 | 256, key: Buffer, iv: Buffer, outputEncoding?: Encoding): string;
14
20
  declare function md5(data: string, inputEncoding?: Encoding): string;
15
21
  declare function execCmdCommand(command: string): Promise<unknown>;
16
- export { randomStringOptions, randomString, encryptCBC, decryptCBC, md5, execCmdCommand };
22
+ declare function getValueType(value: any): "string" | "number" | "undefined" | "object" | "array" | "null" | "unkonw";
23
+ declare function getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes>;
24
+ export { randomStringOptions, getMediaFileTypeRes, randomString, encryptCBC, decryptCBC, md5, execCmdCommand, getValueType, getMediaFileType };
@@ -9,9 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.execCmdCommand = exports.md5 = exports.decryptCBC = exports.encryptCBC = exports.randomString = void 0;
12
+ exports.getMediaFileType = exports.getValueType = exports.execCmdCommand = exports.md5 = exports.decryptCBC = exports.encryptCBC = exports.randomString = void 0;
13
13
  const crypto_1 = require("crypto");
14
14
  const child_process_1 = require("child_process");
15
+ const hgo = require("./hgo");
15
16
  function randomString(length, options) {
16
17
  let { special = false, lowercase = true, upperCase = true, number = true, specials } = options || {};
17
18
  if (specials) {
@@ -92,3 +93,146 @@ function execCmdCommand(command) {
92
93
  });
93
94
  }
94
95
  exports.execCmdCommand = execCmdCommand;
96
+ function getValueType(value) {
97
+ const typeStr = Object.prototype.toString.call(value);
98
+ let type;
99
+ switch (typeStr) {
100
+ case '[object Number]':
101
+ type = 'number';
102
+ break;
103
+ case '[object String]':
104
+ type = 'string';
105
+ break;
106
+ case '[object Object]':
107
+ type = 'object';
108
+ break;
109
+ case '[object Array]':
110
+ type = 'array';
111
+ break;
112
+ case '[object Undefined]':
113
+ type = 'undefined';
114
+ break;
115
+ case '[object Null]':
116
+ type = 'null';
117
+ break;
118
+ default:
119
+ type = 'unkonw';
120
+ break;
121
+ }
122
+ ;
123
+ return type;
124
+ }
125
+ exports.getValueType = getValueType;
126
+ function getMediaFileType(target) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ const readStreamOnceBuffer = (stream) => __awaiter(this, void 0, void 0, function* () {
129
+ return new Promise((resolve, reject) => {
130
+ stream.once('data', chunk => {
131
+ stream.close();
132
+ resolve(chunk);
133
+ });
134
+ stream.on('error', e => {
135
+ reject(e);
136
+ });
137
+ });
138
+ });
139
+ const checkString = (waitMatchStr, byteStart, byteEnd, buffer) => {
140
+ return waitMatchStr === Buffer.from(buffer.slice(byteStart, byteEnd + 1)).toString();
141
+ };
142
+ const checkBuffer = (waitMatchBuffer, byteStart, byteEnd, buffer) => {
143
+ let equal = true;
144
+ const sliceBufferList = buffer.slice(byteStart, byteEnd + 1);
145
+ for (const [index, item] of waitMatchBuffer.entries()) {
146
+ if (item !== sliceBufferList[index]) {
147
+ equal = false;
148
+ break;
149
+ }
150
+ }
151
+ return equal;
152
+ };
153
+ const getResult = (typeStr) => {
154
+ const arr = typeStr.split('|');
155
+ return {
156
+ type: arr[0],
157
+ extname: arr[1]
158
+ };
159
+ };
160
+ let buffer;
161
+ const targetType = this.getValueType(target);
162
+ if (targetType === 'string') {
163
+ const res = yield hgo.get(target, {
164
+ resType: 'buffer',
165
+ headers: {
166
+ Range: 'bytes=0-20'
167
+ }
168
+ });
169
+ buffer = res.resBody;
170
+ }
171
+ else if (targetType === 'array') {
172
+ buffer = target;
173
+ }
174
+ else {
175
+ buffer = yield readStreamOnceBuffer(target);
176
+ }
177
+ buffer = buffer.slice(0, 20);
178
+ if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
179
+ return getResult('image|jpg');
180
+ }
181
+ if (checkString('PNG', 1, 3, buffer)) {
182
+ return getResult('image|png');
183
+ }
184
+ if (checkBuffer([0x49, 0x44, 0x33], 0, 2, buffer) || checkBuffer([0xff, 0xf3], 0, 1, buffer)) {
185
+ return getResult('audio|mp3');
186
+ }
187
+ if (checkString('WAV', 8, 10, buffer)) {
188
+ return getResult('audio|wav');
189
+ }
190
+ if (checkString('M4A', 8, 10, buffer)) {
191
+ return getResult('audio|m4a');
192
+ }
193
+ if (checkString('heic', 8, 11, buffer) || checkString('mif1', 8, 11, buffer)) {
194
+ return getResult('image|heic');
195
+ }
196
+ if (checkString('ftyp', 4, 7, buffer)) {
197
+ if (checkString('ftypqt', 4, 9, buffer)) {
198
+ return getResult('video|mov');
199
+ }
200
+ return getResult('video|mp4');
201
+ }
202
+ if (checkString('WEBP', 8, 11, buffer)) {
203
+ return getResult('image|webp');
204
+ }
205
+ if (checkBuffer([0x47, 0x49, 0x46], 0, 2, buffer)) {
206
+ return getResult('image|gif');
207
+ }
208
+ if (checkBuffer([0x42, 0x4d], 0, 1, buffer)) {
209
+ return getResult('image|bmp');
210
+ }
211
+ if (checkBuffer([0x46, 0x4c, 0x56], 0, 2, buffer)) {
212
+ return getResult('video|flv');
213
+ }
214
+ if (checkBuffer([0x4f, 0x67, 0x67, 0x53], 0, 3, buffer)) {
215
+ return getResult('video|ogg');
216
+ }
217
+ if (checkBuffer([0x1a, 0x45, 0xdf, 0xa3], 0, 3, buffer)) {
218
+ return getResult('video|mkv');
219
+ }
220
+ if (checkBuffer([0x30, 0x26, 0xb2], 0, 2, buffer)) {
221
+ return getResult('video|wmv');
222
+ }
223
+ if (checkString('AVI', 8, 10, buffer)) {
224
+ return getResult('video|avi');
225
+ }
226
+ if (checkBuffer([0xff, 0xf1], 0, 1, buffer)) {
227
+ return getResult('audio|aac');
228
+ }
229
+ if (checkString('PCM', 10, 12, buffer)) {
230
+ return getResult('audio|pcm');
231
+ }
232
+ if (checkBuffer([0x66, 0x4c, 0x61, 0x43], 0, 3, buffer)) {
233
+ return getResult('audio|flac');
234
+ }
235
+ return getResult('unknow|unknow');
236
+ });
237
+ }
238
+ exports.getMediaFileType = getMediaFileType;
package/index.d.ts CHANGED
@@ -14,7 +14,8 @@ import {
14
14
  } from './src/streamhelper';
15
15
 
16
16
  import {
17
- randomStringOptions
17
+ randomStringOptions,
18
+ getMediaFileTypeRes
18
19
  } from './src/cutil';
19
20
 
20
21
  interface Hgo {
@@ -119,7 +120,17 @@ interface Cutil {
119
120
  * 调用终端执行命令
120
121
  * @param command
121
122
  */
122
- execCmdCommand(command: string): string
123
+ execCmdCommand(command: string): Promise<string>
124
+ /**
125
+ * 获取给定值node类型
126
+ * @param value
127
+ */
128
+ getValueType(value: any): string
129
+ /**
130
+ * 获取媒体文件类型
131
+ * @param target 文件url/buffer数据/可读流
132
+ */
133
+ getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes>
123
134
  }
124
135
 
125
136
  declare const hgo: Hgo;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvitool",
3
- "version": "1.0.71",
3
+ "version": "1.0.72",
4
4
  "description": "cvitool",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,6 +15,9 @@
15
15
  "eslint-config-standard": "16.0.3",
16
16
  "@types/node": "20.10.4"
17
17
  },
18
+ "engines": {
19
+ "node": ">=16.0.0"
20
+ },
18
21
  "files": [
19
22
  "build/src",
20
23
  "src",
package/src/cutil.ts CHANGED
@@ -8,6 +8,8 @@ import {
8
8
  import {
9
9
  spawn
10
10
  } from 'child_process';
11
+ import { ReadStream } from 'fs';
12
+ import * as hgo from './hgo';
11
13
 
12
14
  interface randomStringOptions {
13
15
  special?: boolean,
@@ -17,6 +19,11 @@ interface randomStringOptions {
17
19
  specials?: string
18
20
  }
19
21
 
22
+ interface getMediaFileTypeRes {
23
+ type: string,
24
+ extname: string
25
+ }
26
+
20
27
  function randomString(length: number, options?: randomStringOptions) {
21
28
  let { special = false, lowercase = true, upperCase = true, number = true, specials } = options || {};
22
29
  if (specials) {
@@ -93,11 +100,155 @@ async function execCmdCommand(command: string) {
93
100
  });
94
101
  }
95
102
 
103
+ function getValueType(value: any) {
104
+ const typeStr = Object.prototype.toString.call(value);
105
+ let type: 'number' | 'string' | 'array' | 'object' | 'undefined' | 'null' | 'unkonw';
106
+ switch (typeStr) {
107
+ case '[object Number]':
108
+ type = 'number';
109
+ break;
110
+ case '[object String]':
111
+ type = 'string';
112
+ break;
113
+ case '[object Object]':
114
+ type = 'object';
115
+ break;
116
+ case '[object Array]':
117
+ type = 'array';
118
+ break;
119
+ case '[object Undefined]':
120
+ type = 'undefined';
121
+ break;
122
+ case '[object Null]':
123
+ type = 'null';
124
+ break;
125
+ default:
126
+ type = 'unkonw';
127
+ break;
128
+ };
129
+ return type;
130
+ }
131
+
132
+ async function getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes> {
133
+ const readStreamOnceBuffer = async (stream: ReadStream): Promise<Buffer> => {
134
+ return new Promise((resolve, reject) => {
135
+ stream.once('data', chunk => {
136
+ stream.close();
137
+ resolve(chunk as Buffer);
138
+ });
139
+ stream.on('error', e => {
140
+ reject(e);
141
+ });
142
+ });
143
+ };
144
+ const checkString = (waitMatchStr: string, byteStart: number, byteEnd: number, buffer: Buffer) => {
145
+ return waitMatchStr === Buffer.from(buffer.slice(byteStart, byteEnd + 1)).toString();
146
+ };
147
+ const checkBuffer = (waitMatchBuffer: number[], byteStart: number, byteEnd: number, buffer: Buffer) => {
148
+ let equal = true;
149
+ const sliceBufferList = buffer.slice(byteStart, byteEnd + 1);
150
+ for (const [index, item] of waitMatchBuffer.entries()) {
151
+ if (item !== sliceBufferList[index]) {
152
+ equal = false;
153
+ break;
154
+ }
155
+ }
156
+ return equal;
157
+ };
158
+ const getResult = (typeStr: string) => {
159
+ const arr = typeStr.split('|');
160
+ return {
161
+ type: arr[0],
162
+ extname: arr[1]
163
+ };
164
+ };
165
+ let buffer: Buffer;
166
+ const targetType = this.getValueType(target);
167
+ if (targetType === 'string') {
168
+ const res = await hgo.get(
169
+ target as string,
170
+ {
171
+ resType: 'buffer',
172
+ headers: {
173
+ Range: 'bytes=0-20'
174
+ }
175
+ }
176
+ );
177
+ buffer = res.resBody as Buffer;
178
+ } else if (targetType === 'array') {
179
+ buffer = target as Buffer;
180
+ } else {
181
+ buffer = await readStreamOnceBuffer(target as ReadStream);
182
+ }
183
+ buffer = buffer.slice(0, 20);
184
+ if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
185
+ return getResult('image|jpg');
186
+ }
187
+ if (checkString('PNG', 1, 3, buffer)) {
188
+ return getResult('image|png');
189
+ }
190
+ if (checkBuffer([0x49, 0x44, 0x33], 0, 2, buffer) || checkBuffer([0xff, 0xf3], 0, 1, buffer)) {
191
+ return getResult('audio|mp3');
192
+ }
193
+ if (checkString('WAV', 8, 10, buffer)) {
194
+ return getResult('audio|wav');
195
+ }
196
+ if (checkString('M4A', 8, 10, buffer)) {
197
+ return getResult('audio|m4a');
198
+ }
199
+ if (checkString('heic', 8, 11, buffer) || checkString('mif1', 8, 11, buffer)) {
200
+ return getResult('image|heic');
201
+ }
202
+ if (checkString('ftyp', 4, 7, buffer)) {
203
+ if (checkString('ftypqt', 4, 9, buffer)) {
204
+ return getResult('video|mov');
205
+ }
206
+ return getResult('video|mp4');
207
+ }
208
+ if (checkString('WEBP', 8, 11, buffer)) {
209
+ return getResult('image|webp');
210
+ }
211
+ if (checkBuffer([0x47, 0x49, 0x46], 0, 2, buffer)) {
212
+ return getResult('image|gif');
213
+ }
214
+ if (checkBuffer([0x42, 0x4d], 0, 1, buffer)) {
215
+ return getResult('image|bmp');
216
+ }
217
+ if (checkBuffer([0x46, 0x4c, 0x56], 0, 2, buffer)) {
218
+ return getResult('video|flv');
219
+ }
220
+ if (checkBuffer([0x4f, 0x67, 0x67, 0x53], 0, 3, buffer)) {
221
+ return getResult('video|ogg');
222
+ }
223
+ if (checkBuffer([0x1a, 0x45, 0xdf, 0xa3], 0, 3, buffer)) {
224
+ return getResult('video|mkv');
225
+ }
226
+ if (checkBuffer([0x30, 0x26, 0xb2], 0, 2, buffer)) {
227
+ return getResult('video|wmv');
228
+ }
229
+ if (checkString('AVI', 8, 10, buffer)) {
230
+ return getResult('video|avi');
231
+ }
232
+ if (checkBuffer([0xff, 0xf1], 0, 1, buffer)) {
233
+ return getResult('audio|aac');
234
+ }
235
+ if (checkString('PCM', 10, 12, buffer)) {
236
+ return getResult('audio|pcm');
237
+ }
238
+ if (checkBuffer([0x66, 0x4c, 0x61, 0x43], 0, 3, buffer)) {
239
+ return getResult('audio|flac');
240
+ }
241
+ return getResult('unknow|unknow');
242
+ }
243
+
96
244
  export {
97
245
  randomStringOptions,
246
+ getMediaFileTypeRes,
98
247
  randomString,
99
248
  encryptCBC,
100
249
  decryptCBC,
101
250
  md5,
102
- execCmdCommand
251
+ execCmdCommand,
252
+ getValueType,
253
+ getMediaFileType
103
254
  };