operation-evidence.post.mjs
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { e as eventHandler, m as readMultipartFormData } from '../../../nitro/nitro.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:url';
import '@iconify/utils';
import 'consola';
const MAX_BYTES = 50 * 1024 * 1024;
const ALLOWED_TYPES = /* @__PURE__ */ new Set([
"image/jpeg",
"image/png",
"image/webp",
"image/heic",
"video/mp4",
"video/quicktime",
"video/webm"
]);
function pad2(value) {
return String(value).padStart(2, "0");
}
function randomId() {
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
}
function extFromFile(fileName, contentType) {
if (typeof fileName === "string") {
const dot = fileName.lastIndexOf(".");
if (dot >= 0 && dot < fileName.length - 1) {
return fileName.slice(dot).toLowerCase();
}
}
return (contentType == null ? void 0 : contentType.startsWith("video/")) ? ".mp4" : ".jpg";
}
const operationEvidence_post = eventHandler(async (event) => {
const files = await readMultipartFormData(event);
const file = files == null ? void 0 : files.find((item) => item.name === "file" && item.data);
if (!(file == null ? void 0 : file.data) || file.data.length === 0) {
return {
success: false,
errorCode: "VALIDATION_ERROR",
message: "\u8BF7\u4E0A\u4F20\u56FE\u7247\u6216\u89C6\u9891\u9644\u4EF6\u3002"
};
}
if (!file.type || !ALLOWED_TYPES.has(file.type)) {
return {
success: false,
errorCode: "UNSUPPORTED_FILE_TYPE",
message: "\u9644\u4EF6\u683C\u5F0F\u4EC5\u652F\u6301\u56FE\u7247\u6216\u89C6\u9891\u3002"
};
}
if (file.data.length > MAX_BYTES) {
return {
success: false,
errorCode: "FILE_TOO_LARGE",
message: "\u9644\u4EF6\u5927\u5C0F\u4E0D\u80FD\u8D85\u8FC7 50MB\u3002"
};
}
const now = /* @__PURE__ */ new Date();
const relativePath = [
"operation-evidence",
String(now.getUTCFullYear()),
pad2(now.getUTCMonth() + 1),
pad2(now.getUTCDate()),
`${randomId()}${extFromFile(file.filename, file.type)}`
].join("/");
return {
success: true,
errorCode: null,
message: "\u9644\u4EF6\u4E0A\u4F20\u6210\u529F\u3002",
relativePath,
contentType: file.type,
fileName: file.filename || "",
size: file.data.length
};
});
export { operationEvidence_post as default };
//# sourceMappingURL=operation-evidence.post.mjs.map