📝 add documentation for save image helper

add new documentation for save image to filesystem and it's module helper
This commit is contained in:
unknown
2025-06-29 05:28:14 +07:00
parent fea545f0de
commit 26295c749c
3 changed files with 10 additions and 0 deletions

View File

@ -12,15 +12,19 @@ export const saveFile = async (
file: File, file: File,
{ folder, prefix }: SaveFileOptions { folder, prefix }: SaveFileOptions
): Promise<string> => { ): Promise<string> => {
// create a unique file name using the prefix and a random UUID
const ext = mime.extension(file.type) || "bin"; const ext = mime.extension(file.type) || "bin";
const uniqueName = `${prefix ?? ""}${crypto.randomUUID()}.${ext}`; const uniqueName = `${prefix ?? ""}${crypto.randomUUID()}.${ext}`;
// create the relative and absolute paths for the file
const relativeFolder = path.join("uploads", folder); const relativeFolder = path.join("uploads", folder);
const relativePath = path.join(relativeFolder, uniqueName); const relativePath = path.join(relativeFolder, uniqueName);
const absolutePath = path.join(process.cwd(), relativePath); 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 mkdir(path.dirname(absolutePath), { recursive: true });
await writeFile(absolutePath, Buffer.from(await file.arrayBuffer())); await writeFile(absolutePath, Buffer.from(await file.arrayBuffer()));
// return the relative path to the saved file
return relativePath; return relativePath;
}; };

View File

@ -2,10 +2,12 @@ import { saveFile } from "..";
import { AppError } from "../../../error/instances/app"; import { AppError } from "../../../error/instances/app";
export const saveAvatar = async (file: File): Promise<string> => { export const saveAvatar = async (file: File): Promise<string> => {
// Validate that the uploaded file is a single file
if (Array.isArray(file)) { if (Array.isArray(file)) {
throw new AppError(415, "Can't upload more than 1 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"]; const allowedTypes = ["image/png", "image/jpeg", "image/webp"];
if (!allowedTypes.includes(file.type)) { if (!allowedTypes.includes(file.type)) {
throw new AppError( throw new AppError(
@ -14,6 +16,7 @@ export const saveAvatar = async (file: File): Promise<string> => {
); );
} }
// Save the file using the saveFile helper function with specific folder and prefix
return await saveFile(file, { return await saveFile(file, {
folder: "avatar", folder: "avatar",
prefix: "usr-", prefix: "usr-",

View File

@ -2,10 +2,12 @@ import { saveFile } from "..";
import { AppError } from "../../../error/instances/app"; import { AppError } from "../../../error/instances/app";
export const saveCommentBackground = async (file: File): Promise<string> => { export const saveCommentBackground = async (file: File): Promise<string> => {
// Validate that the uploaded file is a single file
if (Array.isArray(file)) { if (Array.isArray(file)) {
throw new AppError(415, "Can't upload more than 1 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"]; const allowedTypes = ["image/png", "image/jpeg", "image/webp"];
if (!allowedTypes.includes(file.type)) { if (!allowedTypes.includes(file.type)) {
throw new AppError( throw new AppError(
@ -14,6 +16,7 @@ export const saveCommentBackground = async (file: File): Promise<string> => {
); );
} }
// Save the file using the saveFile helper function with specific folder and prefix
return await saveFile(file, { return await saveFile(file, {
folder: "comment-backgorund", folder: "comment-backgorund",
prefix: "cmnt-bg-", prefix: "cmnt-bg-",