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