WEBVTT 00:05.380 --> 00:09.730 In this video you're going to create the calculate size F function. 00:09.730 --> 00:19.060 So this function is going to be similar in structure to the one we used for for this so we'll just copy 00:19.060 --> 00:22.510 the structure like that and we'll change everywhere. 00:22.510 --> 00:27.070 It says calculate size D to calculate the size F 00:30.910 --> 00:35.620 so let's delete the contents of the. 00:35.620 --> 00:42.090 Function will only leave return this array now. 00:42.430 --> 00:46.870 And now let's check what parameter we're gonna need for this. 00:46.870 --> 00:47.260 Function 00:56.580 --> 01:03.980 so to calculate the size of a file we will use the FS module of no Jesus. 01:04.410 --> 01:15.470 So all we need is the start variable which we created earlier and we stored value inside item details 01:15.660 --> 01:17.630 dot stats. 01:17.780 --> 01:21.290 So that's the value we're going to pass to the function 01:25.400 --> 01:27.540 like this. 01:27.830 --> 01:31.830 So let's call the variable starts 01:36.470 --> 01:44.030 and let's start with File Size Bytes. 01:44.330 --> 01:52.020 So this one can be obtained easily by going to starts and then size. 01:52.030 --> 02:01.210 So this will give us the size in bytes directly so just one thing. 02:01.210 --> 02:14.450 If you look at the required files we will need to require the new file which is calculates slice F. 02:14.820 --> 02:21.650 So now we've got the size in bytes and we need to convert that to the file size in human readable format. 02:21.750 --> 02:24.180 You will need to write some code to do that. 02:24.180 --> 02:33.160 So to make things easy I'm going to create a new file in the playground and let's call it human size. 02:33.180 --> 02:45.010 Those tiers and here we're going to play with a function called Math dot log 10. 02:45.200 --> 02:51.230 So the description says that this will return the base 10 logarithm of a number. 02:52.010 --> 02:52.700 So what does it mean. 02:52.700 --> 02:59.220 So if you try this on 1000 for example and you can so 03:02.460 --> 03:03.600 log the results 03:07.620 --> 03:08.670 let's see what you get. 03:08.670 --> 03:15.550 So I'm gonna stop the up then I'm going to go to the playground human size. 03:15.810 --> 03:16.290 And here we go. 03:16.290 --> 03:20.250 We get 3 so this means that's 03:23.140 --> 03:30.300 10 to the power three is this number 1000. 03:30.420 --> 03:33.630 Now let's try with another number 03:36.420 --> 03:43.600 10000 and we get for. 03:43.740 --> 03:53.200 So this means again that's 10 to the power for is that number. 03:53.200 --> 03:56.900 All right so why do we do this. 03:56.950 --> 04:02.780 Well the moments we of course the size of our file in bytes. 04:02.840 --> 04:04.070 Right. 04:04.070 --> 04:12.600 So let's say that the file size is 10000 for example 04:15.570 --> 04:16.540 and that's invites. 04:16.560 --> 04:21.170 And you want to convert this to a human readable format. 04:21.460 --> 04:22.410 It's Make things easy. 04:22.410 --> 04:29.020 I'm going to create a string as we did before we got a quality units. 04:29.040 --> 04:35.380 And here we're going to store all the different possible units bytes gigabytes megabytes gigabytes and 04:35.480 --> 04:36.850 database. 04:37.450 --> 04:45.480 So now you will need to decide what units you're going to use for your file size. 04:45.480 --> 04:53.700 Now if you file size is ten thousand then the first thing you will check is whether it's greater than 04:53.700 --> 04:54.950 1000 or not. 04:54.990 --> 04:56.300 Right. 04:56.340 --> 05:02.190 If it's less than 1000 then you will just keep it in bytes isn't it. 05:02.190 --> 05:09.980 If it's greater than 1000 but it's smaller than 1 million then you know that you're going to use kilobytes 05:09.990 --> 05:12.860 right okay. 05:13.110 --> 05:18.930 So we will need to write some code which will follow the same logic. 05:18.930 --> 05:21.870 So up to 1000 05:28.460 --> 05:34.340 you use bytes and then up to 1 million you use kilobytes. 05:34.340 --> 05:35.130 Right. 05:35.180 --> 05:38.510 And then up to 1 billion 05:42.810 --> 05:47.430 you use megabytes and you carry on like that. 05:47.610 --> 05:55.030 So how is log 10 going to help us. 05:55.040 --> 06:01.070 Well if you check log 10 of any number and they're 1000 06:04.040 --> 06:14.620 you will get a number which is smaller than three if you do that's for any number between 1000 and 1 06:14.620 --> 06:15.480 million. 06:15.970 --> 06:20.060 Then you will get a number which is between 3 and 6. 06:20.110 --> 06:21.040 Right. 06:21.120 --> 06:27.980 Then if you carry on you will get a number between six and nine. 06:28.120 --> 06:28.380 All right. 06:28.390 --> 06:30.570 So let's try to understand the logic. 06:30.580 --> 06:37.310 So when you try look in on 1000 you get 06:41.250 --> 06:46.930 now if you divide these results by three you will get something consistence here. 06:47.340 --> 06:48.000 Basically 06:50.620 --> 06:51.610 log 10 06:55.140 --> 06:59.320 the file size divided by three. 06:59.350 --> 07:06.610 This will give you one two three and so on. 07:07.590 --> 07:20.490 So the result of this between 4 between 0 and 1000 bytes is going to be between 0 and 1 and the results 07:20.490 --> 07:28.220 between 1000 bytes and 1 million bytes is going to be 1 2 2. 07:28.240 --> 07:29.330 All right. 07:29.330 --> 07:44.630 This means that if you apply month dot floor to this number then you know the results for each of these 07:44.630 --> 07:45.490 intervals. 07:45.560 --> 07:47.410 You know that is going to be zero. 07:47.420 --> 07:50.700 There is going to be one there. 07:50.780 --> 07:54.500 There three there and so on. 07:54.500 --> 07:56.840 Now what does this mean. 07:56.840 --> 08:07.930 Well if we are in that range we're going to be using bytes because it's less than 1000 bytes and the 08:07.930 --> 08:11.410 result of this is going to be zero 08:14.220 --> 08:20.930 so this actually matches with the index of bytes isn't this. 08:20.940 --> 08:35.220 That's great now if we are in this range this results is going to be one which corresponds to the index 08:35.280 --> 08:35.820 of gay. 08:35.820 --> 08:38.930 And that's exactly the units we need in this range. 08:39.570 --> 08:43.650 So now we know how to get the correct index. 08:43.650 --> 08:44.060 Right. 08:44.070 --> 08:50.100 So let's talk us inside a variable that we gonna call index. 08:50.140 --> 08:51.450 Now as you know the index. 08:51.480 --> 09:00.410 How are you going to convert the file size from bytes to the correct value using the correct units. 09:00.720 --> 09:03.830 So let's start with bytes. 09:03.900 --> 09:15.940 So if you've got 700 bytes you will just keep it as it is right let's move to kilobytes. 09:15.940 --> 09:27.670 So let's say you've got ten thousand bytes you will just divide 10000 by 1000 This will give you the 09:27.670 --> 09:31.600 correct value in kilobytes. 09:31.600 --> 09:32.650 Now if you had 09:35.830 --> 09:36.460 10 million 09:39.940 --> 09:50.890 bytes then you would be in that range and you would need to use megabytes and you're going to divide 09:50.890 --> 09:58.050 by 1000 twice so divided by 1000 and then divide by 1000. 09:58.140 --> 10:05.070 Does dividing by 1000 to the power to get and it will carry on like that. 10:05.490 --> 10:12.450 So the next range you're going to divide by 1000 to the power 3 and so on. 10:12.450 --> 10:12.900 So 10:15.880 --> 10:18.210 if you look at the first one you're not doing anything. 10:18.210 --> 10:24.640 And that's similar to dividing by 1000 to the power zero which is equal to 1. 10:25.570 --> 10:29.440 And this one is equal to 1000 to the power one. 10:29.510 --> 10:46.560 OK so the formula is going to be file size which is about divide by month not power of 1000 to the power 10:46.560 --> 10:47.850 that you choose. 10:47.850 --> 10:53.090 And this actually much is the index 10:57.350 --> 10:59.130 so just make sure you got this right. 10:59.570 --> 11:01.010 I'm going to go again through this. 11:01.010 --> 11:09.710 So the index follows this calculation and we found that four elements in that range. 11:09.710 --> 11:16.090 We get an index of 0 elements in that range we get an index of 1 and so on. 11:16.700 --> 11:21.370 And also we notice that four elements in that range. 11:21.560 --> 11:31.090 The calculation follows this logic and we find our index right that had four elements in that range. 11:31.100 --> 11:37.970 We follow this logic and our index appears right that which matches with the correct index. 11:37.970 --> 11:46.000 So this is our formula for calculating the correct value which we're going to be use in the human readable 11:46.000 --> 11:47.010 formats. 11:47.210 --> 11:50.420 So the human readable format is going to be asked following 11:56.080 --> 11:57.820 so let's call this 12:01.450 --> 12:03.400 while size human for example 12:09.220 --> 12:11.310 is going to be this 12:14.830 --> 12:22.210 followed by the units which is gonna be the index or units 12:27.890 --> 12:33.920 arts the index index. 12:34.170 --> 12:53.780 So let's console that look this and see what we get. 12:53.840 --> 13:00.490 So we need math that look that instead of log so let's try again. 13:00.530 --> 13:10.740 So here we've got 10 kilobytes or that file size right and we don't need this space. 13:10.740 --> 13:12.440 Now let's try another file size. 13:12.450 --> 13:15.440 Let's try something like that's 13:20.220 --> 13:32.160 only gets five point seventy five megabytes now to avoid long numbers like this we can limit the number 13:32.160 --> 13:37.720 of digits in the decimal past to only 2 or 1. 13:37.740 --> 13:38.280 So 13:49.130 --> 14:01.170 you can add two fixed and choose the number of digits thus you won't say let's go for one see and this 14:01.170 --> 14:01.970 looks better. 14:01.980 --> 14:10.230 So in our final code we will set the file size variable to this value. 14:10.260 --> 14:15.630 So this copy this code into calculate size F 14:18.890 --> 14:20.980 so let's just add some comments. 14:21.020 --> 14:30.200 So we started here with the size in bytes and then we moving here to the size in human 14:33.660 --> 14:38.850 readable format so it is deletes any lines of code. 14:38.970 --> 14:43.410 Thus we don't need we don't need those. 14:43.590 --> 14:44.700 We don't need those either. 14:46.710 --> 14:50.280 Instead of filed size we're going to use the file size bytes 14:59.280 --> 15:02.920 so let's replace all these instances. 15:02.990 --> 15:03.960 So that's one 15:09.080 --> 15:09.380 that's what 15:15.260 --> 15:20.330 so that's the index. 15:20.600 --> 15:23.100 That's the value. 15:23.510 --> 15:34.850 We can also define the units like this. 15:35.210 --> 15:37.520 The final value we want is this one 15:41.760 --> 15:52.580 we're going to set the file size to that value and instead of units index we're gonna just use the units 15:52.850 --> 15:53.500 very well. 15:53.600 --> 15:59.460 And it makes our code easier to read. 15:59.510 --> 15:59.950 Let's just. 15:59.980 --> 16:00.400 Okay. 16:00.400 --> 16:04.420 I make sure that everything works okay. 16:04.450 --> 16:09.610 So we need to start this up again because we haven't subsidies 16:15.120 --> 16:15.380 okay. 16:15.390 --> 16:21.050 And now we're getting the correct size for each of our files. 16:21.240 --> 16:29.570 So now this works for files and for all this right now let's go back to our respond those days file 16:30.050 --> 16:33.860 and see which step we're gonna go to. 16:33.860 --> 16:42.340 So now we finished the code corresponding to the directory scenario. 16:42.340 --> 16:46.670 So now we're gonna move to another scenario where we found something. 16:46.670 --> 16:48.240 But it's not a directory. 16:48.340 --> 16:52.510 So we didn't execute that code but it's not a file either. 16:52.510 --> 17:03.710 So we will just check if starts is file checking the opposite of source is file 17:06.550 --> 17:09.520 so if that's the case we will just send a city's code 17:13.430 --> 17:15.380 which is for a 1 17:18.510 --> 17:19.510 0 4 0 1. 17:19.530 --> 17:24.190 Here means that we found something but access is denied. 17:24.210 --> 17:31.970 All right so we can't access advanced content and then we're going to write something like 4 0 1. 17:32.170 --> 17:33.840 Access denied 17:37.050 --> 17:42.180 you can also log something to the console like not a file 17:45.080 --> 17:50.290 and you just return response bots and 17:53.410 --> 18:00.040 now if you go to the directory if statements you need to make sure that at the end you've got a return 18:00.130 --> 18:01.330 statements about. 18:01.780 --> 18:09.100 So that's if you fall in that scenario you don't execute any code that comes after that. 18:09.130 --> 18:14.410 So instead of respond those and we're going gonna use return respond. 18:14.440 --> 18:18.320 And so it just means that we will. 18:18.490 --> 18:25.810 And the response here and also we are returning or using a return statements meaning that's our code 18:25.840 --> 18:27.220 is going to stop right there. 18:28.460 --> 18:29.090 Okay. 18:29.240 --> 18:36.180 So let's test our code and check but it is still working fourfold is perfect. 18:36.380 --> 18:36.750 All right. 18:36.750 --> 18:43.980 In the next video you're going to move to the file scenario and then write code which will serve files 18:43.980 --> 18:44.790 to your users.