diff --git a/src/helpers/files/saveFile/index.ts b/src/helpers/files/saveFile/index.ts index 9457096..9013fb9 100644 --- a/src/helpers/files/saveFile/index.ts +++ b/src/helpers/files/saveFile/index.ts @@ -12,15 +12,19 @@ export const saveFile = async ( file: File, { folder, prefix }: SaveFileOptions ): Promise => { + // create a unique file name using the prefix and a random UUID const ext = mime.extension(file.type) || "bin"; const uniqueName = `${prefix ?? ""}${crypto.randomUUID()}.${ext}`; + // create the relative and absolute paths for the file const relativeFolder = path.join("uploads", folder); const relativePath = path.join(relativeFolder, uniqueName); const absolutePath = path.join(process.cwd(), relativePath); + // ensure the directory exists and write the file to the filesystem await mkdir(path.dirname(absolutePath), { recursive: true }); await writeFile(absolutePath, Buffer.from(await file.arrayBuffer())); + // return the relative path to the saved file return relativePath; }; diff --git a/src/helpers/files/saveFile/modules/saveAvatar.ts b/src/helpers/files/saveFile/modules/saveAvatar.ts index 8c04e75..60de033 100644 --- a/src/helpers/files/saveFile/modules/saveAvatar.ts +++ b/src/helpers/files/saveFile/modules/saveAvatar.ts @@ -2,10 +2,12 @@ import { saveFile } from ".."; import { AppError } from "../../../error/instances/app"; export const saveAvatar = async (file: File): Promise => { + // Validate that the uploaded file is a single file if (Array.isArray(file)) { throw new AppError(415, "Can't upload more than 1 file"); } + // Validate that the file type is one of the allowed types, currently only .jpg, .png, and .webp are allowed const allowedTypes = ["image/png", "image/jpeg", "image/webp"]; if (!allowedTypes.includes(file.type)) { throw new AppError( @@ -14,6 +16,7 @@ export const saveAvatar = async (file: File): Promise => { ); } + // Save the file using the saveFile helper function with specific folder and prefix return await saveFile(file, { folder: "avatar", prefix: "usr-", diff --git a/src/helpers/files/saveFile/modules/saveCommentBackgorund.ts b/src/helpers/files/saveFile/modules/saveCommentBackgorund.ts index 8f7ebb1..ad1ab78 100644 --- a/src/helpers/files/saveFile/modules/saveCommentBackgorund.ts +++ b/src/helpers/files/saveFile/modules/saveCommentBackgorund.ts @@ -2,10 +2,12 @@ import { saveFile } from ".."; import { AppError } from "../../../error/instances/app"; export const saveCommentBackground = async (file: File): Promise => { + // Validate that the uploaded file is a single file if (Array.isArray(file)) { throw new AppError(415, "Can't upload more than 1 file"); } + // Validate that the file type is one of the allowed types, currently only .jpg, .png, and .webp are allowed const allowedTypes = ["image/png", "image/jpeg", "image/webp"]; if (!allowedTypes.includes(file.type)) { throw new AppError( @@ -14,6 +16,7 @@ export const saveCommentBackground = async (file: File): Promise => { ); } + // Save the file using the saveFile helper function with specific folder and prefix return await saveFile(file, { folder: "comment-backgorund", prefix: "cmnt-bg-",