明树Git Lab

Commit 48fd7a80 authored by zfp1's avatar zfp1

update

parent b1f9ac9d
...@@ -86,6 +86,46 @@ async function downloadFileById(req, res, next) { ...@@ -86,6 +86,46 @@ async function downloadFileById(req, res, next) {
} }
async function showFileById(req, res, next) {
try {
// ----------
let fileId = req.params.fileId;
let fileinfo = await DB.File.findOne({ where: {id: fileId}, raw: true });
if (!(fileinfo && fileinfo.id)) {
return res.sendError(errorMessage.resourceNotFound)
}
let {
originalname,
filename,
size,
mimetype,
url,
path,
nginxpath,
} = fileinfo;
//TODO:path处理
const filePath = Path.join(__dirname, '../../../', path);
if (!fs.existsSync(filePath)) {
return res.sendError(errorMessage.resourceNotFound)
}
// 3. 设置强制下载响应头
res.setHeader('Content-Type', mimetype);
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(filename)}"`);
// 4. 创建文件流并传输
const fileStream = fs.createReadStream(filePath);
fileStream.pipe(res);
// 错误处理
fileStream.on('error', (err) => {
console.error(`File stream error: ${err}`);
if (!res.headersSent) res.status(500).send('File streaming error');
});
} catch (error) {
next(error);
}
}
...@@ -98,4 +138,5 @@ module.exports = { ...@@ -98,4 +138,5 @@ module.exports = {
upload, upload,
batchUpload, batchUpload,
downloadFileById, downloadFileById,
showFileById,
} }
\ No newline at end of file
...@@ -63,6 +63,7 @@ function checkPath(path) { ...@@ -63,6 +63,7 @@ function checkPath(path) {
/^\/user\/login$/, /^\/user\/login$/,
/^\/user\/regist$/, /^\/user\/regist$/,
/^\/file\/download\/\d+$/, /^\/file\/download\/\d+$/,
/^\/file\/show\/\d+$/,
]; ];
return allowedPatterns.some(pattern => pattern.test(path)); return allowedPatterns.some(pattern => pattern.test(path));
......
...@@ -36,5 +36,6 @@ router.post('/upload', upload.single('file'), fileController.upload); ...@@ -36,5 +36,6 @@ router.post('/upload', upload.single('file'), fileController.upload);
router.post('/batch/upload', upload.array('files'), fileController.batchUpload); router.post('/batch/upload', upload.array('files'), fileController.batchUpload);
router.get('/download/:fileId', fileController.downloadFileById); router.get('/download/:fileId', fileController.downloadFileById);
router.get('/show/:fileId', fileController.showFileById);
module.exports = router; module.exports = router;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment