operation-evidence.post.mjs 2.42 KB
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