[4.1] - The process to upload files to S3 #1676
-
Hey guys! I'm making use of the manually process to upload files to S3. But there are some things that I didn't find in the docs. I comment two lines in the code: 1 - Why the validation props is not working in this situation? 2 - Does this validation happen before the file is uploaded? I'm doing this because validation props doen't work, and I want to make this validation as faster as I can. The code: async store({ request, response }) {
const validationOptions = {
type: 'image',
maxSize: '5242880', //bytes
extnames: ['png', 'jpeg', 'jpg']
}
// 1 - The validationOptions doesn't take any effect
await request.multipart.file('photo', validationOptions, async file => {
try {
// 2 - Does this validation happen before the file is uploaded?
if(file.stream.byteCount > 5242880) {
console.log('Fall here!')
return true
}
const contentType = file.headers['content-type']
const acl = 'public-read'
const filename = `${Date.now()}_${file.clientName}`
const url = await Drive.put('photos/' + filename, file.stream, {
contentType,
acl
})
const photo = await Photo.create({
photo: filename,
name: file.clientName,
type: file.type,
subtype: file.subtype
})
return response.status(200).send(photo)
} catch (err) {
return response.status(err.status).send({
error: {
message: err.message
}
})
}
}).process()
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In v4 when you are consuming the stream, then you have to perform validations manually as you are doing it right now. In v5, we have changed the behavior and the validations of its own.
Validation happens as stream gets consumed/read by the write stream and in case of error the write stream will stop consuming the read stream (the uploaded file stream) |
Beta Was this translation helpful? Give feedback.
In v4 when you are consuming the stream, then you have to perform validations manually as you are doing it right now. In v5, we have changed the behavior and the validations of its own.
Validation happens as stream gets consumed/read by the write stream and in case of error the write stream will stop consuming the read stream (the uploaded file stream)