mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -0,0 +1,54 @@
|
||||
// utils/assetUpload.util.js
|
||||
//
|
||||
// Client-side gate for the Add Asset multi-file drop zone. Mirrors what the
|
||||
// backend actually knows how to classify (resolveFileType() in
|
||||
// assets.controller.js: image/video/audio by MIME prefix, everything else
|
||||
// under application/* or text/* becomes "document") but rejects generic
|
||||
// binaries up front instead of letting them 500 on the server.
|
||||
|
||||
const ALLOWED_DOCUMENT_MIME_TYPES = new Set([
|
||||
"application/pdf",
|
||||
"application/msword",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"application/vnd.ms-excel",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"application/vnd.ms-powerpoint",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
"application/zip",
|
||||
"application/x-zip-compressed",
|
||||
"application/json",
|
||||
"application/rtf",
|
||||
"text/plain",
|
||||
"text/csv",
|
||||
"text/markdown",
|
||||
]);
|
||||
|
||||
const ALLOWED_DOCUMENT_EXTENSIONS = new Set([
|
||||
"pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx",
|
||||
"zip", "json", "rtf", "txt", "csv", "md",
|
||||
]);
|
||||
|
||||
export function fileExtension(filename = "") {
|
||||
const dot = filename.lastIndexOf(".");
|
||||
return dot > 0 ? filename.slice(dot + 1).toLowerCase() : "";
|
||||
}
|
||||
|
||||
// { ok: true } or { ok: false, reason }
|
||||
export function validateAssetFile(file) {
|
||||
const mime = file.type || "";
|
||||
|
||||
if (mime.startsWith("image/") || mime.startsWith("video/") || mime.startsWith("audio/")) {
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
const ext = fileExtension(file.name);
|
||||
if (ALLOWED_DOCUMENT_MIME_TYPES.has(mime) || ALLOWED_DOCUMENT_EXTENSIONS.has(ext)) {
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
const label = ext ? `.${ext}` : (mime || "unknown");
|
||||
return {
|
||||
ok: false,
|
||||
reason: `Unsupported file type "${label}". Allowed formats: images, video, audio, documents.`,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user