@cnbcool/cnb-api-generate 1.2.6 → 1.2.7
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.
- package/client/utils/upload.ts +60 -3
- package/package.json +1 -2
package/client/utils/upload.ts
CHANGED
|
@@ -8,9 +8,67 @@
|
|
|
8
8
|
|
|
9
9
|
import fs from 'fs';
|
|
10
10
|
import nodePath from 'path';
|
|
11
|
-
import { lookup as mimeLookup } from 'mime-types';
|
|
12
11
|
import type { ResolvedShortcut } from '../shortcuts';
|
|
13
12
|
|
|
13
|
+
/** 常见文件扩展名 → MIME 类型映射(仅用于上传场景,不引入外部依赖) */
|
|
14
|
+
const MIME_MAP: Record<string, string> = {
|
|
15
|
+
'.txt': 'text/plain',
|
|
16
|
+
'.html': 'text/html', '.htm': 'text/html',
|
|
17
|
+
'.css': 'text/css',
|
|
18
|
+
'.csv': 'text/csv',
|
|
19
|
+
'.js': 'application/javascript', '.mjs': 'application/javascript',
|
|
20
|
+
'.json': 'application/json',
|
|
21
|
+
'.xml': 'application/xml',
|
|
22
|
+
'.pdf': 'application/pdf',
|
|
23
|
+
'.zip': 'application/zip',
|
|
24
|
+
'.gz': 'application/gzip', '.gzip': 'application/gzip',
|
|
25
|
+
'.tar': 'application/x-tar',
|
|
26
|
+
'.7z': 'application/x-7z-compressed',
|
|
27
|
+
'.rar': 'application/vnd.rar',
|
|
28
|
+
'.doc': 'application/msword',
|
|
29
|
+
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
30
|
+
'.xls': 'application/vnd.ms-excel',
|
|
31
|
+
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
32
|
+
'.ppt': 'application/vnd.ms-powerpoint',
|
|
33
|
+
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
34
|
+
'.png': 'image/png',
|
|
35
|
+
'.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
|
|
36
|
+
'.gif': 'image/gif',
|
|
37
|
+
'.svg': 'image/svg+xml',
|
|
38
|
+
'.webp': 'image/webp',
|
|
39
|
+
'.ico': 'image/x-icon',
|
|
40
|
+
'.bmp': 'image/bmp',
|
|
41
|
+
'.mp3': 'audio/mpeg',
|
|
42
|
+
'.wav': 'audio/wav',
|
|
43
|
+
'.ogg': 'audio/ogg',
|
|
44
|
+
'.mp4': 'video/mp4',
|
|
45
|
+
'.webm': 'video/webm',
|
|
46
|
+
'.avi': 'video/x-msvideo',
|
|
47
|
+
'.mov': 'video/quicktime',
|
|
48
|
+
'.woff': 'font/woff', '.woff2': 'font/woff2',
|
|
49
|
+
'.ttf': 'font/ttf',
|
|
50
|
+
'.otf': 'font/otf',
|
|
51
|
+
'.md': 'text/markdown',
|
|
52
|
+
'.yaml': 'text/yaml', '.yml': 'text/yaml',
|
|
53
|
+
'.sh': 'application/x-sh',
|
|
54
|
+
'.py': 'text/x-python',
|
|
55
|
+
'.rb': 'text/x-ruby',
|
|
56
|
+
'.go': 'text/x-go',
|
|
57
|
+
'.rs': 'text/x-rust',
|
|
58
|
+
'.ts': 'text/typescript', '.tsx': 'text/typescript',
|
|
59
|
+
'.jsx': 'text/jsx',
|
|
60
|
+
'.java': 'text/x-java-source',
|
|
61
|
+
'.c': 'text/x-c', '.h': 'text/x-c',
|
|
62
|
+
'.cpp': 'text/x-c++src', '.hpp': 'text/x-c++hdr',
|
|
63
|
+
'.log': 'text/plain',
|
|
64
|
+
'.patch': 'text/x-diff', '.diff': 'text/x-diff',
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function mimeLookup(filePath: string): string {
|
|
68
|
+
const ext = nodePath.extname(filePath).toLowerCase();
|
|
69
|
+
return MIME_MAP[ext] || 'application/octet-stream';
|
|
70
|
+
}
|
|
71
|
+
|
|
14
72
|
// 上传文件大小上限:100MB
|
|
15
73
|
const MAX_FILE_SIZE = 100 * 1024 * 1024;
|
|
16
74
|
|
|
@@ -115,8 +173,7 @@ export async function handleUpload(
|
|
|
115
173
|
return { status: 400, data: { error: `文件过大 (${(fileSize / 1024 / 1024).toFixed(1)}MB),上限 ${MAX_FILE_SIZE / 1024 / 1024}MB` } };
|
|
116
174
|
}
|
|
117
175
|
|
|
118
|
-
const
|
|
119
|
-
const contentType = typeof mimeResult === 'string' ? mimeResult : 'application/octet-stream';
|
|
176
|
+
const contentType = mimeLookup(filePath);
|
|
120
177
|
|
|
121
178
|
// 2. 调用上传 API 获取 upload_url
|
|
122
179
|
const isIssueUpload = shortcut.module === 'issues';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cnbcool/cnb-api-generate",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.7",
|
|
4
4
|
"main": "./built/index.js",
|
|
5
5
|
"module": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -40,7 +40,6 @@
|
|
|
40
40
|
"debug": "4.4.1",
|
|
41
41
|
"glob": "^13.0.1",
|
|
42
42
|
"lodash": "^4.17.23",
|
|
43
|
-
"mime-types": "^2.1.35",
|
|
44
43
|
"ora": "5.4.1",
|
|
45
44
|
"prettier": "3.4.2",
|
|
46
45
|
"rimraf": "6.0.1",
|