39 lines
991 B
TypeScript
39 lines
991 B
TypeScript
import { writeFileSync } from "fs";
|
|
import { extname } from "path";
|
|
import { v4 } from "uuid";
|
|
import { ContentFilenameRegex } from "@server/constants";
|
|
import { log } from "@server/logger";
|
|
import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn";
|
|
|
|
export default eventHandler(async (ev) => {
|
|
const noMultipart = "no multipart data found!???!?";
|
|
isLoggedIn(ev);
|
|
let mpd = await readMultipartFormData(ev);
|
|
if (!mpd)
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: noMultipart,
|
|
});
|
|
log.debug("woooo", { label: "http/upload" });
|
|
let file = mpd[0];
|
|
log.debug(mpd[0].filename!, { label: "http/upload" });
|
|
if (!file) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: noMultipart,
|
|
});
|
|
}
|
|
if (!ContentFilenameRegex.test(file.filename!)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: "unsupported file type!",
|
|
});
|
|
}
|
|
let b = `${v4()}${extname(file.filename!)}`;
|
|
writeFileSync(`tmp/${b}`, file.data);
|
|
return {
|
|
success: true,
|
|
fileName: b,
|
|
};
|
|
});
|