Skip to content
Snippets Groups Projects
Commit c86f656f authored by Alexandru Munteanu's avatar Alexandru Munteanu
Browse files

Merge branch 'faraday-master' of gitlab.coko.foundation:xpub/xpub into faraday-master

parents a19c0cc4 c8aeb66e
No related branches found
No related tags found
No related merge requests found
...@@ -21,28 +21,53 @@ const AWSBackend = app => { ...@@ -21,28 +21,53 @@ const AWSBackend = app => {
bucket: process.env.AWS_BUCKET, bucket: process.env.AWS_BUCKET,
contentType: multerS3.AUTO_CONTENT_TYPE, contentType: multerS3.AUTO_CONTENT_TYPE,
key: (req, file, cb) => { key: (req, file, cb) => {
cb(null, uuid.v4()) const fileKey = `${req.body.fragmentId}/${uuid.v4()}`
cb(null, fileKey)
}, },
}), }),
// fileFilter: (req, file, cb) => { fileFilter: (req, file, cb) => {
// console.log('req in fileFilter', req.body.get('fileType')) if (
// console.log('file in filter:', file) req.body.fileType === 'manuscripts' ||
// if (req.body.fileType === 'supplementary') { req.body.fileType === 'coverLetter'
// cb(null, false) ) {
// return if (
// } file.mimetype === 'application/pdf' ||
// cb(null, true) file.mimetype === 'application/msword' ||
// }, file.mimetype ===
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
) {
return cb(null, true)
}
req.fileValidationError = 'Only Word documents and PDFs are allowed'
return cb(null, false)
}
return cb(null, true)
},
}) })
app.post( app.post(
'/api/aws-upload', '/api/aws-upload',
authBearer, authBearer,
upload.single('file'), upload.single('file'),
async (req, res) => { async (req, res) => {
// console.log('FILE:', req.file) if (req.fileValidationError !== undefined) {
return res.status(400).json({ error: req.fileValidationError })
}
res.status(200).json({
id: req.file.key,
name: req.file.originalname,
size: req.file.size,
})
},
)
app.get(
'/api/aws-signed-url/:fragmentId/:fileId',
authBearer,
async (req, res) => {
const params = { const params = {
Bucket: process.env.AWS_BUCKET, Bucket: process.env.AWS_BUCKET,
Key: req.file.key, Key: `${req.params.fragmentId}${req.params.fileId}`,
} }
s3.getSignedUrl('getObject', params, (err, data) => { s3.getSignedUrl('getObject', params, (err, data) => {
...@@ -51,27 +76,28 @@ const AWSBackend = app => { ...@@ -51,27 +76,28 @@ const AWSBackend = app => {
return return
} }
res.status(200).json({ res.status(200).json({
id: req.file.key,
name: req.file.originalname,
size: req.file.size,
signedUrl: data, signedUrl: data,
}) })
}) })
}, },
) )
app.delete('/api/aws-delete/:fileId', authBearer, async (req, res) => { app.delete(
const params = { '/api/aws-delete/:fragmentId/:fileId',
Bucket: process.env.AWS_BUCKET, authBearer,
Key: req.params.fileId, async (req, res) => {
} const params = {
s3.deleteObject(params, (err, data) => { Bucket: process.env.AWS_BUCKET,
if (err) { Key: `${req.params.fragmentId}${req.params.fileId}`,
res.status(err.statusCode).json({ error: err.message })
return
} }
res.status(204).json() s3.deleteObject(params, (err, data) => {
}) if (err) {
}) res.status(err.statusCode).json({ error: err.message })
return
}
res.status(204).json()
})
},
)
} }
module.exports = AWSBackend module.exports = AWSBackend
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