1 00:00:00,150 --> 00:00:05,700 In this video it's gonna be up to you to add validation for the end point that's going to allow users 2 00:00:05,700 --> 00:00:09,320 to upload their avatars to upload their images. 3 00:00:09,330 --> 00:00:15,420 So over here I'll paste in the challenged comments for getting that done just below the existing code 4 00:00:15,450 --> 00:00:17,160 from the previous challenge. 5 00:00:17,160 --> 00:00:18,420 What are you going to do. 6 00:00:18,480 --> 00:00:22,210 You're going to add validation to the Avatar upload route. 7 00:00:22,230 --> 00:00:28,830 We want to limit the Upload Size to one megabyte and we only want to allow the following files. 8 00:00:28,860 --> 00:00:30,080 JP G. 9 00:00:30,090 --> 00:00:33,150 JP e.g. and P energy images. 10 00:00:33,150 --> 00:00:35,070 Everything else should be rejected. 11 00:00:35,460 --> 00:00:40,370 Finally you want to test your work upload larger files even if they're images. 12 00:00:40,380 --> 00:00:47,580 Those should fail and then upload non images and that should fail as well so take some time to knock 13 00:00:47,580 --> 00:00:48,180 this out. 14 00:00:48,180 --> 00:00:52,520 Don't forget to test your work when you're done come back and click play 15 00:00:56,290 --> 00:00:57,040 how'd that go. 16 00:00:57,070 --> 00:00:58,750 Let's go ahead and kick things off. 17 00:00:58,750 --> 00:01:03,890 Up above inside of our call to Malta where we're going to set up all of these options. 18 00:01:03,910 --> 00:01:10,990 The first one is going to be the file size limit so I'll set up limits alongside of dest and right here 19 00:01:11,020 --> 00:01:15,190 we'll go ahead and provide on that object file size. 20 00:01:15,400 --> 00:01:18,540 Now to restrict it to one megabyte that is 1 million. 21 00:01:18,550 --> 00:01:21,430 A one with six zeros afterwards. 22 00:01:21,490 --> 00:01:22,720 Perfect. 23 00:01:22,720 --> 00:01:25,480 Next up we only want to allow JP G. 24 00:01:25,600 --> 00:01:28,050 J pegs and PSG files. 25 00:01:28,120 --> 00:01:32,840 We're going to do that by setting up file filter like we had before. 26 00:01:32,950 --> 00:01:34,980 Now there are two ways you could have done this. 27 00:01:35,080 --> 00:01:39,880 You could have copied that code and repurposed it or you could have written things out from scratch 28 00:01:39,910 --> 00:01:41,470 either approach is fine. 29 00:01:41,500 --> 00:01:49,930 Right here we have file filter with request file and the callback and we're gonna go ahead and set that 30 00:01:49,930 --> 00:01:53,820 function up from scratch so we'll start with our condition. 31 00:01:54,040 --> 00:02:00,490 If that condition doesn't run we'll use callback to go ahead and accept the given upload. 32 00:02:00,490 --> 00:02:06,850 Now if this thing fails that means it wasn't an image and we'll use callback with return to make sure 33 00:02:06,850 --> 00:02:11,170 the function stops and an error gets shown to the user right here. 34 00:02:11,200 --> 00:02:15,910 That's a new error and we'll go ahead and use the following message. 35 00:02:15,910 --> 00:02:18,840 Please upload an image. 36 00:02:19,000 --> 00:02:20,200 Excellent. 37 00:02:20,210 --> 00:02:24,370 Now the only thing we have to focus on is the condition itself. 38 00:02:24,400 --> 00:02:30,640 We'll be using the logical not operator to make sure that we reject when it's not one of the following 39 00:02:30,640 --> 00:02:38,890 extensions and right here we'll be using the file dot original name and property once again and we're 40 00:02:38,890 --> 00:02:45,760 going to use match to provide the same regular expression we used before with an extra extension tossed 41 00:02:45,760 --> 00:02:46,690 onto the list. 42 00:02:46,690 --> 00:02:55,030 So right here that is backslash dot in parentheses then followed by a dollar sign and right here we 43 00:02:55,030 --> 00:03:03,400 have all of our file extensions separated by the vertical bar so we can start off with J P G then J 44 00:03:03,430 --> 00:03:12,250 P E G then finally PMG and if there's other types I wanted to support I could add those on as well. 45 00:03:12,280 --> 00:03:16,600 Now with this in place we should be all done and we can test our work. 46 00:03:16,630 --> 00:03:21,170 What I'm going to do is remove the challenge comments down below. 47 00:03:21,390 --> 00:03:28,230 I'll go ahead and save the changes we made to our call to motor and we'll head back over to postmen. 48 00:03:28,260 --> 00:03:34,860 Now in this case I do want to head over to the upload Avatar request and I can kick things off by uploading 49 00:03:34,860 --> 00:03:36,470 profile picture. 50 00:03:36,480 --> 00:03:40,250 This is indeed an image and it is much smaller than one megabyte. 51 00:03:40,320 --> 00:03:42,240 So this should be accepted. 52 00:03:42,270 --> 00:03:46,770 I'll send that off the server did indeed accept the upload. 53 00:03:46,770 --> 00:03:49,460 Now let's go ahead and try the fall. 54 00:03:49,530 --> 00:03:50,660 JP file. 55 00:03:50,670 --> 00:03:53,010 It's an image but it's too large. 56 00:03:53,010 --> 00:03:55,400 Right here we can see that failed. 57 00:03:55,470 --> 00:04:00,320 And finally we'll upload something that's not an image at all right here. 58 00:04:00,370 --> 00:04:07,470 I'll go ahead and upload the PDX document in that directory I'll send that off to the server and down 59 00:04:07,470 --> 00:04:11,390 below I can see my error please upload an image. 60 00:04:11,580 --> 00:04:17,490 Now that we have this in place we're gonna go ahead and continue on with other topics in the next lesson 61 00:04:17,700 --> 00:04:25,050 we'll talk about how we can handle the errors from Malta and send back a Jason response instead of sending 62 00:04:25,050 --> 00:04:31,310 back this HDL page with a ton of unnecessary information we don't want to expose. 63 00:04:31,410 --> 00:04:34,200 We're gonna go ahead and cover that in the next lesson. 64 00:04:34,200 --> 00:04:36,480 I'm excited to get to it so we'll see you then.