管理端可配类型/体积;上传改流式避免整文件进内存;发帖先选再传并拦截未上传提交;提高 Next proxy 体积上限。 Co-authored-by: Cursor <cursoragent@cursor.com>
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
/** 附件类型徽章分类(详情页与发帖编辑共用) */
|
|
|
|
export type FileKind = "image" | "archive" | "doc" | "media" | "file";
|
|
|
|
export function fileKind(name: string, mime: string): FileKind {
|
|
const m = (mime || "").toLowerCase();
|
|
const ext = (name.split(".").pop() || "").toLowerCase();
|
|
if (
|
|
m.startsWith("image/") ||
|
|
["png", "jpg", "jpeg", "gif", "webp", "svg", "bmp", "avif", "ico", "heic"].includes(ext)
|
|
) {
|
|
return "image";
|
|
}
|
|
if (
|
|
m.includes("zip") ||
|
|
m.includes("rar") ||
|
|
m.includes("7z") ||
|
|
m.includes("compress") ||
|
|
["zip", "rar", "7z", "tar", "gz", "bz2", "xz", "tgz"].includes(ext)
|
|
) {
|
|
return "archive";
|
|
}
|
|
if (
|
|
m.startsWith("audio/") ||
|
|
m.startsWith("video/") ||
|
|
["mp3", "wav", "flac", "aac", "ogg", "mp4", "mkv", "webm", "mov", "avi"].includes(ext)
|
|
) {
|
|
return "media";
|
|
}
|
|
if (
|
|
m.includes("pdf") ||
|
|
m.includes("word") ||
|
|
m.includes("text") ||
|
|
m.includes("sheet") ||
|
|
m.includes("excel") ||
|
|
["pdf", "doc", "docx", "txt", "md", "xls", "xlsx", "ppt", "pptx", "csv", "json"].includes(ext)
|
|
) {
|
|
return "doc";
|
|
}
|
|
return "file";
|
|
}
|
|
|
|
export function fileExtBadge(name: string): string {
|
|
const i = name.lastIndexOf(".");
|
|
if (i <= 0 || i === name.length - 1) return "FILE";
|
|
const ext = name.slice(i + 1).replace(/[^a-zA-Z0-9]/g, "");
|
|
if (!ext || ext.length > 5) return "FILE";
|
|
return ext.toUpperCase();
|
|
}
|