Skip to content

Commit 8056614

Browse files
author
Brent Engelbrecht
committed
Set content type appropriately for downloads
1 parent cd8c31a commit 8056614

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

src/routes/files.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ const fileService = require('../services/fileService')
33
async function upload (ctx) {
44
const fileToUpload = ctx.request.files.file
55
const currentUser = ctx.request.jwtPayload.sub
6-
const { key, url } = await fileService.uploadFile({
7-
fileName: fileToUpload.name,
8-
filePath: fileToUpload.path,
9-
fileType: fileToUpload.type
10-
})
6+
const { key, url } = await fileService.uploadFile(fileToUpload)
117
await fileService.setTags(key, [{ Key: 'username', Value: `${currentUser}` }])
128
ctx.body = { key, url }
139
}
@@ -21,6 +17,7 @@ async function download (ctx) {
2117
}
2218

2319
const fileData = await fileService.downloadFile(fileToDownload)
20+
ctx.set('Content-Type', fileData.ContentType)
2421
ctx.body = fileData.Body
2522
}
2623

src/services/fileService.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ const getTags = async key => {
4949
})
5050
}
5151

52-
const uploadFile = async ({ fileName, filePath, fileType }) => {
52+
const uploadFile = async (fileObject) => {
5353
return new Promise((resolve, reject) => {
5454
getS3Ref()
55-
const stream = fs.createReadStream(filePath)
55+
const stream = fs.createReadStream(fileObject.path)
5656
stream.on('error', err => {
5757
reject(err)
5858
})
5959

6060
s3.upload({
6161
Bucket: process.env.BUCKET,
6262
Body: stream,
63-
Key: fileName,
64-
ContentType: fileType
63+
Key: fileObject.name,
64+
ContentType: fileObject.type
6565
},
6666
(err, data) => {
6767
if (err) {

0 commit comments

Comments
 (0)