In this video it's gonna be up to you to add validation for the end point that's going to allow users
to upload their avatars to upload their images.
So over here I'll paste in the challenged comments for getting that done just below the existing code
from the previous challenge.
What are you going to do.
You're going to add validation to the Avatar upload route.
We want to limit the Upload Size to one megabyte and we only want to allow the following files.
JP G.
JP e.g. and P energy images.
Everything else should be rejected.
Finally you want to test your work upload larger files even if they're images.
Those should fail and then upload non images and that should fail as well so take some time to knock
this out.
Don't forget to test your work when you're done come back and click play
how'd that go.
Let's go ahead and kick things off.
Up above inside of our call to Malta where we're going to set up all of these options.
The first one is going to be the file size limit so I'll set up limits alongside of dest and right here
we'll go ahead and provide on that object file size.
Now to restrict it to one megabyte that is 1 million.
A one with six zeros afterwards.
Perfect.
Next up we only want to allow JP G.
J pegs and PSG files.
We're going to do that by setting up file filter like we had before.
Now there are two ways you could have done this.
You could have copied that code and repurposed it or you could have written things out from scratch
either approach is fine.
Right here we have file filter with request file and the callback and we're gonna go ahead and set that
function up from scratch so we'll start with our condition.
If that condition doesn't run we'll use callback to go ahead and accept the given upload.
Now if this thing fails that means it wasn't an image and we'll use callback with return to make sure
the function stops and an error gets shown to the user right here.
That's a new error and we'll go ahead and use the following message.
Please upload an image.
Excellent.
Now the only thing we have to focus on is the condition itself.
We'll be using the logical not operator to make sure that we reject when it's not one of the following
extensions and right here we'll be using the file dot original name and property once again and we're
going to use match to provide the same regular expression we used before with an extra extension tossed
onto the list.
So right here that is backslash dot in parentheses then followed by a dollar sign and right here we
have all of our file extensions separated by the vertical bar so we can start off with J P G then J
P E G then finally PMG and if there's other types I wanted to support I could add those on as well.
Now with this in place we should be all done and we can test our work.
What I'm going to do is remove the challenge comments down below.
I'll go ahead and save the changes we made to our call to motor and we'll head back over to postmen.
Now in this case I do want to head over to the upload Avatar request and I can kick things off by uploading
profile picture.
This is indeed an image and it is much smaller than one megabyte.
So this should be accepted.
I'll send that off the server did indeed accept the upload.
Now let's go ahead and try the fall.
JP file.
It's an image but it's too large.
Right here we can see that failed.
And finally we'll upload something that's not an image at all right here.
I'll go ahead and upload the PDX document in that directory I'll send that off to the server and down
below I can see my error please upload an image.
Now that we have this in place we're gonna go ahead and continue on with other topics in the next lesson
we'll talk about how we can handle the errors from Malta and send back a Jason response instead of sending
back this HDL page with a ton of unnecessary information we don't want to expose.
We're gonna go ahead and cover that in the next lesson.
I'm excited to get to it so we'll see you then.