dev #5
+11
-3
@@ -7,6 +7,15 @@ function getWorkingDirectory(): string {
|
||||
return process.cwd();
|
||||
}
|
||||
|
||||
function buildUploadPath(type: string, datePath: string): string {
|
||||
const uploadBaseDir = process.env.UPLOAD_DIR || './uploads';
|
||||
return path.join(getWorkingDirectory(), uploadBaseDir, type, datePath);
|
||||
}
|
||||
|
||||
function buildFilePath(uploadDir: string, fileName: string): string {
|
||||
return path.join(uploadDir, fileName);
|
||||
}
|
||||
|
||||
export interface UploadOptions {
|
||||
maxSize?: number;
|
||||
allowedTypes?: string[];
|
||||
@@ -132,9 +141,8 @@ export async function uploadFile(
|
||||
throw new Error('文件内容与声明类型不匹配');
|
||||
}
|
||||
|
||||
const uploadBaseDir = process.env.UPLOAD_DIR || './uploads';
|
||||
const datePath = getDatePath();
|
||||
const uploadDir = path.join(getWorkingDirectory(), uploadBaseDir, type, datePath);
|
||||
const uploadDir = buildUploadPath(type, datePath);
|
||||
|
||||
if (!existsSync(uploadDir)) {
|
||||
await mkdir(uploadDir, { recursive: true });
|
||||
@@ -144,7 +152,7 @@ export async function uploadFile(
|
||||
const extension = getFileExtension(file.type);
|
||||
const sanitizedOriginalName = sanitizeFileName(file.name);
|
||||
const fileName = `${fileId}${extension}`;
|
||||
const filePath = path.join(uploadDir, fileName);
|
||||
const filePath = buildFilePath(uploadDir, fileName);
|
||||
|
||||
await writeFile(filePath, buffer);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user