Skip to content

Commit

Permalink
Merge pull request #541 from rfcx/fix-multer-upload
Browse files Browse the repository at this point in the history
Fix multer upload
  • Loading branch information
Tooseriuz authored Feb 22, 2024
2 parents fea68ef + 7b603db commit 9e6e98e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions common/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ function update (user, attrs) {

function checkUserPicture (files) {
return new Promise((resolve, reject) => {
if (Array.isArray(files.file)) {
if (files.length > 1) {
return reject(new ValidationError('It is only one file allowed to be uploaded.'))
}
const file = files.file
const file = files[0]
if (!file) {
return reject(new ValidationError('No file provided.'))
}
Expand Down
7 changes: 5 additions & 2 deletions core/classifiers/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ module.exports = async (req, res) => {

// Upload model if included
let modelUrl = ''
if (req.files && req.files.file) {
const file = req.files.file
if (req.files && req.files.length > 0) {
if (req.files.length > 1) {
throw new ValidationError('Can upload only 1 file at a time')
}
const file = req.files[0]
if (!file.originalname.endsWith('.tar.gz')) {
throw new ValidationError('File must be .tar.gz')
}
Expand Down
6 changes: 4 additions & 2 deletions noncore/v1/guardians/guardians-checkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ router.route('/:guardian_id/checkins')
})
.then(function () {
// parse, review and save screenshots
const screenShotInfo = checkInHelpers.screenshots.info(req.files.screenshot, strArrToJSArr(this.json.screenshots, '|', '*'),
const screenshotFile = req.files.find(x => x.fieldname === 'screenshot')
const screenShotInfo = checkInHelpers.screenshots.info(screenshotFile, strArrToJSArr(this.json.screenshots, '|', '*'),
this.dbGuardian.id, this.dbGuardian.guid, this.dbCheckIn.id)
const proms = []
for (const screenShotInfoInd in screenShotInfo) {
Expand All @@ -126,7 +127,8 @@ router.route('/:guardian_id/checkins')
.then(function () {
const self = this
// parse, review and save audio
const audioInfo = checkInHelpers.audio.info(req.files.audio, req.rfcx.api_url_domain, strArrToJSArr(this.json.audio, '|', '*'), this.dbGuardian, this.dbCheckIn)
const audioFile = req.files.find(x => x.fieldname === 'audio')
const audioInfo = checkInHelpers.audio.info(audioFile, req.rfcx.api_url_domain, strArrToJSArr(this.json.audio, '|', '*'), this.dbGuardian, this.dbCheckIn)
const proms = []
for (const audioInfoInd in audioInfo) {
const info = audioInfo[audioInfoInd]
Expand Down
6 changes: 3 additions & 3 deletions noncore/v1/users/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ router.route('/avatar-change')
})
.then(() => {
const opts = {
filePath: req.files.file.path,
fileName: `/userpics/${guid}${path.extname(req.files.file.originalname)}`,
filePath: req.files[0].path,
fileName: `/userpics/${guid}${path.extname(req.files[0].originalname)}`,
bucket: process.env.USERS_BUCKET,
acl: 'public-read'
}
return usersService.uploadImageFile(opts)
})
.then(() => {
const url = `https://${process.env.USERS_BUCKET}.s3-${process.env.AWS_REGION_ID}.amazonaws.com/userpics/${guid}${path.extname(req.files.file.originalname)}`
const url = `https://${process.env.USERS_BUCKET}.s3-${process.env.AWS_REGION_ID}.amazonaws.com/userpics/${guid}${path.extname(req.files[0].originalname)}`
return usersService.prepareUserUrlPicture(user, url)
.then(() => {
return url
Expand Down

0 comments on commit 9e6e98e

Please sign in to comment.