Validate Multipart File While Streaming Files

#1

How to validate request.multipart.file? Cant find in adonis docs. I am using latest version of adonis.

0 Likes

#2

What do you want to validate? What kind of a file ?
Let us suppose I want to validate users’ avatar:


request.multipart.file('avatar', {
  types: ["jpeg", "jpg"],
  size: "4mb"
  }, async (file) => {
    const content = await getStream.buffer(file.stream)
    await content._validateFile(); 
  });
0 Likes

#3

exactly the same as you write in your code but its not working. i dont know why?

Here is the code. i am uploading the file s3 and i am sending the pdf files.
Code let me upload it :frowning:

request.multipart.file(‘media_file’, {
types: [“jpeg”, “jpg”],
size: “1mb”
}, async (file) => {
file.size = file.stream.byteCount;
await file.validateFile();
temp_file_name = randomString({ length: 10, letters: false }) + '
’ + file.clientName;
await Drive.disk(‘s3’).put(temp_file_name, file.stream, { ACL: ‘public-read’ })
file_obj = file;
})

0 Likes

#4

You need to first understand how streams works and what you wants to do is not possible

0 Likes

#5

I thought may be there is a way just like @begueradj explain to me.

Also please can you explain me that what is the purpose of second parameter of the below function.

request.multipart.file('profile_pic', {}, async (file) => {
    await Drive.disk('s3').put(file.clientName, file.stream)
  })

please check this link

Thanks In Advance.

0 Likes

#6

Have you read about streaming files ?

Also this may help you.

0 Likes

#7

yeah man,i already read it. but there is no help about validation the stream.

I want to validate the file before it goes to s3 and also i wanna use this brilliant feature of streaming :slight_smile:

Till now i couldn’t find a way to do that.

0 Likes

#8

The answer to what is the purpose of the second parameter, is in your above code:

0 Likes

#9

but its not working.

0 Likes

#10

I checked the source code, that second parameter refers to options. Which options? Can not guess for the moment. But coming back to your main question: you can restrict the files to upload to the server from the client side (pure JavaScript).

0 Likes

#11

Thanks for the feedback @begueradj :smile: i will try it because it seems like i have no other choice right now.

0 Likes