明树Git Lab
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
J
jt_backend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengfanpei
jt_backend
Commits
48fd7a80
Commit
48fd7a80
authored
Nov 25, 2025
by
zfp1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
b1f9ac9d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
0 deletions
+43
-0
fileController.js
controller/fileController.js
+41
-0
request.js
middleware/request.js
+1
-0
fileRouter.js
router/fileRouter.js
+1
-0
No files found.
controller/fileController.js
View file @
48fd7a80
...
@@ -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
middleware/request.js
View file @
48fd7a80
...
@@ -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
));
...
...
router/fileRouter.js
View file @
48fd7a80
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment