2023-10-02 16:00:33 -04:00
|
|
|
import { writeFileSync } from "fs";
|
|
|
|
import { extname } from "path";
|
|
|
|
import { v4 } from "uuid";
|
|
|
|
import { ContentFilenameRegex } from "~/lib/server/constants";
|
2023-10-12 22:56:50 -04:00
|
|
|
import { log } from "~/lib/server/logger";
|
2023-10-02 16:00:33 -04:00
|
|
|
import isLoggedIn from "~/lib/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,
|
|
|
|
});
|
2023-10-12 22:56:50 -04:00
|
|
|
log.debug("woooo", { label: "http/upload" });
|
2023-10-02 16:00:33 -04:00
|
|
|
let file = mpd[0];
|
2023-10-12 22:56:50 -04:00
|
|
|
log.debug(mpd[0].filename!, { label: "http/upload" });
|
2023-10-02 16:00:33 -04:00
|
|
|
if (!file) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
message: noMultipart,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!ContentFilenameRegex.test(file.filename!)) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
message: "unsupported file type!",
|
|
|
|
});
|
|
|
|
}
|
2023-10-12 22:56:50 -04:00
|
|
|
let b = `${v4()}${extname(file.filename!)}`;
|
2023-10-02 16:00:33 -04:00
|
|
|
writeFileSync(`tmp/${b}`, file.data);
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
fileName: b,
|
|
|
|
};
|
|
|
|
});
|