mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 14:49:19 +00:00
上传的文件自动删除
This commit is contained in:
parent
c1199e1649
commit
27e25d9258
@ -54,21 +54,25 @@ class FileController {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传接口
|
* 处理文件上传请求
|
||||||
* @example 示例请求
|
* 客户端应以 `multipart/form-data` 格式上传文件,字段名为 `file`
|
||||||
|
* @example 示例请求(使用 axios 和 form-data)
|
||||||
* ```js
|
* ```js
|
||||||
* const form = new FormData();
|
* const form = new FormData();
|
||||||
* const fileStream = fs.createReadStream(filePath);
|
* const fileStream = fs.createReadStream(filePath);
|
||||||
* form.append('file', fileStream);
|
* form.append('file', fileStream);
|
||||||
* const uploadUrl = `http://localhost:4000/upload?dir=${uploadDir}`;
|
* const uploadUrl = `http://localhost:4000/upload?dir=example&expire=600`;
|
||||||
* const response = await axios.post(uploadUrl, form, {
|
* const response = await axios.post(uploadUrl, form, {
|
||||||
* headers: {
|
* headers: {
|
||||||
* ...form.getHeaders(),
|
* ...form.getHeaders(),
|
||||||
* },
|
* },
|
||||||
* maxContentLength: Infinity,
|
* maxContentLength: Infinity,
|
||||||
* maxBodyLength: Infinity,
|
* maxBodyLength: Infinity,
|
||||||
* });
|
* });
|
||||||
* ```
|
* ```
|
||||||
|
*
|
||||||
|
* @queryParam dir 上传到的相对目录,默认根目录
|
||||||
|
* @queryParam expire 文件保留时间,默认 600 秒
|
||||||
* @param req
|
* @param req
|
||||||
* @param res
|
* @param res
|
||||||
*/
|
*/
|
||||||
@ -80,10 +84,12 @@ class FileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const uploadDir = req.query.dir?.toString() || '';
|
const uploadDir = req.query.dir?.toString() || '';
|
||||||
|
const deleteAfter = parseInt(req.query.expire as string) || 10 * 60;
|
||||||
const { fullPath, relativePath } = await this.FileService.saveUploadedFile(
|
const { fullPath, relativePath } = await this.FileService.saveUploadedFile(
|
||||||
req.file,
|
req.file,
|
||||||
uploadDir
|
uploadDir
|
||||||
);
|
);
|
||||||
|
await this.FileService.scheduleDelete(fullPath, deleteAfter * 1000);
|
||||||
await response.success(res, {
|
await response.success(res, {
|
||||||
message: '文件上传成功..',
|
message: '文件上传成功..',
|
||||||
filePath: fullPath,
|
filePath: fullPath,
|
||||||
|
@ -52,6 +52,22 @@ class FileService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定时删除文件
|
||||||
|
* @param filePath 文件绝对路径
|
||||||
|
* @param timeoutMs 毫秒,默认10分钟
|
||||||
|
*/
|
||||||
|
public async scheduleDelete(filePath: string, timeoutMs: number = 10 * 60 * 1000): Promise<void> {
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
await fs.unlink(filePath);
|
||||||
|
logger.info(`已自动删除文件: ${filePath}`);
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn(`删除文件失败: ${filePath}`, err);
|
||||||
|
}
|
||||||
|
}, timeoutMs);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查路径合法性
|
* 检查路径合法性
|
||||||
* @param relativePath
|
* @param relativePath
|
||||||
|
Loading…
x
Reference in New Issue
Block a user