Fix error handle
fix pagination issue change logic for change passsword and profile update
This commit is contained in:
29
src/service/disk-storage.ts
Normal file
29
src/service/disk-storage.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { AppError } from '@/lib/errors';
|
||||
import fs, { writeFile } from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const uploadDir = './data/avatar';
|
||||
|
||||
export async function saveFile(key: string, file: Buffer | File) {
|
||||
if (!uploadDir) {
|
||||
throw new Error('Upload directory not found');
|
||||
}
|
||||
|
||||
const fileBuffer =
|
||||
file instanceof File ? Buffer.from(await file.arrayBuffer()) : file;
|
||||
|
||||
const filePath = path.join(uploadDir, key);
|
||||
|
||||
try {
|
||||
await fs.mkdir(uploadDir, { recursive: true });
|
||||
await writeFile(filePath, fileBuffer);
|
||||
return key;
|
||||
} catch (error) {
|
||||
console.error(`Error saving file: ${key}`, error);
|
||||
throw new AppError(
|
||||
'FILE_SAVE_ERROR',
|
||||
`Failed to save file: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
500,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user