1
00:00:08,650 --> 00:00:12,970
In this video, let's implement what is called validation.

2
00:00:13,360 --> 00:00:23,710
For example, if we are accepting only images by the end, user is trying to upload a PDF file or if

3
00:00:23,710 --> 00:00:31,300
we are accepting a specific size of a file, let's say one megabyte, but a user is trying to upload

4
00:00:31,300 --> 00:00:32,439
ten megabytes.

5
00:00:32,470 --> 00:00:34,690
How are we going to prevent that?

6
00:00:34,900 --> 00:00:35,310
Okay.

7
00:00:35,320 --> 00:00:39,490
Let me show you inside the motor configuration here.

8
00:00:39,670 --> 00:00:44,160
We are going to pass an additional property called Limits.

9
00:00:44,500 --> 00:00:52,930
And for this one, we are going to tell our package that, hey, we want to accept a file with specific

10
00:00:52,930 --> 00:00:53,970
file limit.

11
00:00:53,980 --> 00:01:02,500
So here you provide file size and specify the size of the file that you want to upload.

12
00:01:02,590 --> 00:01:08,230
In our case, we want to accept a file, not more than one megabytes.

13
00:01:08,230 --> 00:01:09,250
So here we go.

14
00:01:09,280 --> 00:01:16,070
You provide one and then six zeros, and this is equivalent to one megabytes.

15
00:01:16,090 --> 00:01:22,570
So with this one, let's go ahead and then upload a file which is more than one megabytes.

16
00:01:22,840 --> 00:01:28,060
Inside a folder, you're going to have an image which is three megabytes.

17
00:01:28,060 --> 00:01:30,530
So let's see if you're going to get some error.

18
00:01:30,550 --> 00:01:32,610
So now back to Postman.

19
00:01:32,620 --> 00:01:41,580
Let's go ahead and select the file called Large Image and let me send and you can see that more error

20
00:01:41,590 --> 00:01:44,010
that is file two large.

21
00:01:44,020 --> 00:01:46,480
You see how we are preventing that.

22
00:01:46,510 --> 00:01:48,880
Now, this is one use case.

23
00:01:49,030 --> 00:01:54,520
Another use case is how can we prevent a specific file type?

24
00:01:54,640 --> 00:02:01,820
So let's say that we are receiving all the images, but a user is trying to upload a PDF file.

25
00:02:01,840 --> 00:02:04,090
How are we going to prevent that?

26
00:02:04,120 --> 00:02:04,990
Let's see.

27
00:02:05,200 --> 00:02:12,010
Inside here we are going to pass an additional attribute called file filter as a function call.

28
00:02:12,010 --> 00:02:15,280
And is that inside the function?

29
00:02:15,280 --> 00:02:20,650
We have access to the requests, the file and then the callback.

30
00:02:20,890 --> 00:02:28,930
The request contains the request objects and the file contains the file the user is trying to upload.

31
00:02:28,930 --> 00:02:37,330
And the callback is a function that we are going to call if the upload was successful or not.

32
00:02:37,360 --> 00:02:46,400
So for the meantime, let's accept only images with the file extension call JPEG or P and G.

33
00:02:46,420 --> 00:02:50,290
So now how are we going to get it inside this function?

34
00:02:50,290 --> 00:02:54,740
We have access to the file the user is uploading on the file.

35
00:02:54,760 --> 00:03:01,490
So if you console log file like that, let's see what you're going to get.

36
00:03:01,510 --> 00:03:07,810
Now let's get back to Postman and upload a file which is less than one megabytes.

37
00:03:07,810 --> 00:03:15,790
And I get that if I check my terminal, you can see that I have access to the object.

38
00:03:15,820 --> 00:03:22,900
Now we can check whether the file is PDF or PNG or JPEG.

39
00:03:23,470 --> 00:03:26,110
So now back to our function here.

40
00:03:26,470 --> 00:03:31,690
So inside this function, let's say we are accepting.

41
00:03:40,210 --> 00:03:42,070
So how can we implement that?

42
00:03:42,070 --> 00:03:44,560
We are going to use JavaScript to get it done.

43
00:03:44,920 --> 00:03:49,690
Before we continue on, let me show you how we are going to call callback here.

44
00:03:49,720 --> 00:03:52,380
We are going to call callback twice.

45
00:03:52,390 --> 00:04:00,250
First is we are going to call when everything is successful and then we are going to call if there are

46
00:04:00,250 --> 00:04:01,450
some errors.

47
00:04:01,480 --> 00:04:04,150
Now let's start with the success.

48
00:04:04,150 --> 00:04:12,490
One for the success we are going to call callback with the first arguments being null, meaning that

49
00:04:12,490 --> 00:04:13,810
there is no error.

50
00:04:13,840 --> 00:04:20,769
Then then the second arguments are going to be true, meaning proceed to the next module and then if

51
00:04:20,769 --> 00:04:23,680
there is some error, we are going to call callback.

52
00:04:23,680 --> 00:04:29,500
And the first element is going to be a text that we want to specify for this one.

53
00:04:29,500 --> 00:04:39,460
Let's say that please upload image with extension dot P and G.

54
00:04:39,670 --> 00:04:42,430
Now let's see how we are going to implement that.

55
00:04:42,430 --> 00:04:44,950
So let's comment this one.

56
00:04:44,950 --> 00:04:46,990
We will come back to it very soon.

57
00:04:46,990 --> 00:04:51,490
So let's check the conditions here before calling the callback.

58
00:04:51,490 --> 00:04:53,020
So now have a look.

59
00:04:53,380 --> 00:04:58,000
We are going to make use of if statements and how can we get the file.

60
00:04:58,030 --> 00:05:05,740
Remember, we can get the file on this property and we be able to know the extension on what is called

61
00:05:05,740 --> 00:05:07,210
the original name.

62
00:05:07,210 --> 00:05:15,220
We can see that we have dot pdf so now we can make use of this one to check the file type a user is

63
00:05:15,220 --> 00:05:16,030
uploading.

64
00:05:16,030 --> 00:05:21,000
So on the original name we are going to make use of and way.

65
00:05:21,010 --> 00:05:32,530
So file dot original name dot ends with if it ends with with extension dot, p and g, then let's see

66
00:05:32,530 --> 00:05:34,900
how we are going to call the callback.

67
00:05:34,930 --> 00:05:36,730
We are going to call this success.

68
00:05:36,730 --> 00:05:41,080
So our code is one and put it here and remove the comments.

69
00:05:41,080 --> 00:05:43,330
And then this one I start.

70
00:05:44,200 --> 00:05:45,370
What about error?

71
00:05:45,410 --> 00:05:47,160
They're going to make use of errors.

72
00:05:47,170 --> 00:05:54,010
And next is I'll cut this one and then put it inside the errors as that.

73
00:05:54,040 --> 00:05:54,520
Good.

74
00:05:54,520 --> 00:05:56,350
So now let's have a look.

75
00:05:56,560 --> 00:05:58,780
Well, we need to finish up this one.

76
00:05:58,780 --> 00:06:05,050
It takes into argument the first one going to be the message and then the next one going to be as false.

77
00:06:05,050 --> 00:06:08,820
Meaning there is an error, so don't proceed to the next minute.

78
00:06:09,220 --> 00:06:11,530
So now we are done with the implementation.

79
00:06:11,620 --> 00:06:15,610
Let's get back to Postman and now we are sending Pdaf.

80
00:06:15,610 --> 00:06:16,420
So let's see.

81
00:06:16,660 --> 00:06:18,430
Now you can see that.

82
00:06:18,430 --> 00:06:22,870
Please upload image with extension dot p and g.

83
00:06:23,110 --> 00:06:33,280
If we change the extension here to let's say JPEG, let's say JPG, let's select this image that is

84
00:06:33,280 --> 00:06:38,620
dot jpg and let's say we can see that upload successful.

85
00:06:38,620 --> 00:06:46,150
And if I check my folder, you can see that I have all the images being uploaded for this one.

86
00:06:46,150 --> 00:06:51,700
We can add the extension to it, but unless we write some code, get it done.

87
00:06:51,700 --> 00:06:58,060
But like I said, you are going to make the saving or you are going to store the images into cloud nine.

88
00:06:58,060 --> 00:07:03,130
But to be able to understand file upload, you need to know how more thought works.

89
00:07:03,130 --> 00:07:05,410
That is why I'm showing this code.

90
00:07:05,410 --> 00:07:05,980
Good.

91
00:07:05,980 --> 00:07:10,690
So let's say that we also want to support, let's say, PNG and JPEG.

92
00:07:10,690 --> 00:07:23,650
We can make use of all and then provide the file our original name ends with and then let's say P and

93
00:07:23,650 --> 00:07:24,130
G.

94
00:07:24,400 --> 00:07:31,360
So here we can pass in the type of file that you want to support and you'll be good to go.

95
00:07:31,360 --> 00:07:36,070
So this is the basic flow of this, how Malta works.

96
00:07:36,220 --> 00:07:43,540
In the next video, I'll show you how we can make it simpler ordinary to upload file easily as compared

97
00:07:43,540 --> 00:07:44,560
to this one.

98
00:07:44,560 --> 00:07:50,470
But understanding this one will help you how file upload works in express.

