1 00:00:00,810 --> 00:00:03,200 It's time to wrap up the controller's method for uploading, 2 00:00:03,200 --> 00:00:06,910 before testing this whole upload functionality in the browser. 3 00:00:06,910 --> 00:00:10,130 Now we'll first need to autowire the service, so I'll do that up top. 4 00:00:12,410 --> 00:00:18,620 Autowired and we'll add a private GifService and I'll call it gifService. 5 00:00:18,620 --> 00:00:19,120 Great. 6 00:00:20,290 --> 00:00:24,090 Then we can return to the add gif method to finish our uploading work. 7 00:00:24,090 --> 00:00:27,875 I'm going to use the structure feature that i showed you earlier, 8 00:00:27,875 --> 00:00:32,470 addGif, and there is that method right there, cool. 9 00:00:32,470 --> 00:00:35,880 To save the GIF entity, we'll invoke the services save method, 10 00:00:35,880 --> 00:00:39,760 passing it the GIF entity, as well as the multipart file object. 11 00:00:39,760 --> 00:00:44,760 As for the GIF entity, we can add it as a parameter here and 12 00:00:44,760 --> 00:00:49,410 Spring will automatically reassemble the form submission data into a GIF entity, so 13 00:00:49,410 --> 00:00:50,560 I'll add that right here. 14 00:00:52,700 --> 00:00:55,150 And now let's call the services save method. 15 00:00:55,150 --> 00:00:56,500 We'll do that right here. 16 00:00:57,770 --> 00:01:00,930 So we'll call gifService.save, 17 00:01:00,930 --> 00:01:04,720 we'll pass the GIF object that this controller method received, and 18 00:01:04,720 --> 00:01:11,060 we'll pass the multipart file object that this controller method received as well. 19 00:01:11,060 --> 00:01:15,090 After we save, we should include a flash message indicating that the save completed 20 00:01:15,090 --> 00:01:16,330 successfully. 21 00:01:16,330 --> 00:01:17,777 So, let me add a comment for that. 22 00:01:17,777 --> 00:01:22,520 We'll add a flash message for success. 23 00:01:22,520 --> 00:01:24,100 So, as I try to do that, 24 00:01:24,100 --> 00:01:27,710 I realize I don't have access to a redirect attributes object. 25 00:01:27,710 --> 00:01:31,845 That's because it's missing from my parameter list here, so let's add that 26 00:01:31,845 --> 00:01:37,315 redirectAttributes and I'll name it the same thing and there we have access to it. 27 00:01:37,315 --> 00:01:45,100 RedirectAttributes.addFlashAttribute, remember we call that attribute flash 28 00:01:45,100 --> 00:01:49,670 and the object will add is a new FlashMessage object. 29 00:01:49,670 --> 00:01:55,050 Here, we'll Type a status of GIF successfully 30 00:01:55,050 --> 00:02:00,290 uploaded, or whatever message you like and don't forget the status. 31 00:02:00,290 --> 00:02:05,098 So that's going to be FlashMessage.Status.SUCCESS, 32 00:02:05,098 --> 00:02:07,870 cool, semi-colon. 33 00:02:07,870 --> 00:02:13,090 As for the re-direct, let's send the browser to the newly uploaded GIF's detail 34 00:02:13,090 --> 00:02:19,030 page, which is /gifs/ whatever the ID happens to be. 35 00:02:19,030 --> 00:02:24,230 Here's how we'll do that, in place of null, I'll use the String.format function 36 00:02:24,230 --> 00:02:30,510 and I want it to redirect to /gifs/ and 37 00:02:30,510 --> 00:02:33,640 then what ever that ID happens to be. 38 00:02:34,910 --> 00:02:40,660 And what is that value that we want placed in place of this placeholder right here? 39 00:02:40,660 --> 00:02:45,720 Well, gif.getId. 40 00:02:45,720 --> 00:02:50,540 We can get the entity's ID by calling it's getId method, and this will return a value 41 00:02:50,540 --> 00:02:56,770 now because it will have been persisted to the database following this save up here. 42 00:02:58,320 --> 00:03:02,830 The hibernate session save method takes care of updating the ID field for 43 00:03:02,830 --> 00:03:06,550 us using it's setter, so we don't have to do that explicitly. 44 00:03:06,550 --> 00:03:10,200 Now there are two more items we need to take care of in the controller. 45 00:03:10,200 --> 00:03:13,740 The first is fully implementing the method for a give detail view. 46 00:03:13,740 --> 00:03:17,360 Since that's exactly where we're redirecting to, 47 00:03:17,360 --> 00:03:22,510 right here we're redirecting to the gif detail view /give/ whatever that 48 00:03:22,510 --> 00:03:27,670 ID happens to be 1, 2, 7, 13, 1834, whatever it happens to be. 49 00:03:28,780 --> 00:03:33,160 So we'll need to code that controller method that handles the detail view. 50 00:03:33,160 --> 00:03:37,620 And the second thing we need to do is to make sure we're able to serve the GIF's 51 00:03:37,620 --> 00:03:42,570 image data so that when it's referenced using an img tag in HTML, 52 00:03:42,570 --> 00:03:48,630 our app can properly serve the image so the GIFs that the users so 53 00:03:48,630 --> 00:03:51,370 enthusiastically uploaded can be displayed. 54 00:03:52,800 --> 00:03:55,560 Let's work on the GIF detail view first, and 55 00:03:55,560 --> 00:03:59,510 that is rendered by the GIF details method up top here. 56 00:04:00,885 --> 00:04:04,105 Now that we have a findById method in our gifService, 57 00:04:04,105 --> 00:04:06,365 we can invoke it here with our autowired service. 58 00:04:06,365 --> 00:04:11,285 So we'll say gifService.findById, and 59 00:04:11,285 --> 00:04:14,735 we'll use that gifId that we received in here, gifId. 60 00:04:16,155 --> 00:04:18,685 Now for the second piece that is the actual image data, 61 00:04:18,685 --> 00:04:23,355 which is right below this one in this gifImage method right here. 62 00:04:24,440 --> 00:04:30,020 We need to fetch the GIF in the same way we just did in the method above. 63 00:04:30,020 --> 00:04:39,170 So let's first grab the GIF entity using the service gifService.findById, 64 00:04:39,170 --> 00:04:44,852 let's use that GIF id that's given as a parameter value there, and gifId. 65 00:04:44,852 --> 00:04:46,100 Cool. 66 00:04:46,100 --> 00:04:50,145 And then, let's return the byte array from the entity using the getBytes method, 67 00:04:50,145 --> 00:04:52,940 .getBytes. 68 00:04:52,940 --> 00:04:54,240 Cool. 69 00:04:54,240 --> 00:04:55,450 And that should do it. 70 00:04:55,450 --> 00:04:59,706 Notice that I have applied the response body annotation here. 71 00:04:59,706 --> 00:05:04,750 What this tells Thymeleaf is that this return value here 72 00:05:04,750 --> 00:05:11,930 constitutes the exact response body that should be sent with the HTTP response. 73 00:05:11,930 --> 00:05:12,870 That is, 74 00:05:12,870 --> 00:05:18,220 we don't want the Thymeleaf template engine to render any sort of view here. 75 00:05:18,220 --> 00:05:24,480 We want Spring to use exactly this return value as the response body, 76 00:05:24,480 --> 00:05:28,890 instead of timely doing extra processing using a template. 77 00:05:28,890 --> 00:05:35,210 This will return the byte array which will then render in the browser view. 78 00:05:35,210 --> 00:05:39,610 Now, if we've been careful enough along the way, we now have uploads working. 79 00:05:39,610 --> 00:05:41,720 So there's only one way to find out. 80 00:05:41,720 --> 00:05:43,290 Let's test it! 81 00:05:43,290 --> 00:05:45,010 So I'll make sure to save all my work here, 82 00:05:46,050 --> 00:05:48,310 make sure you have your database server running. 83 00:05:48,310 --> 00:05:51,373 I'm going to kill my last instance of the app and 84 00:05:51,373 --> 00:05:54,065 I'm going to re-run that Bootrun task. 85 00:05:56,452 --> 00:06:01,053 I've got my toes and fingers crossed for successful compilation, 86 00:06:01,053 --> 00:06:05,380 there we go, and hopefully everything starts up as it should. 87 00:06:05,380 --> 00:06:09,700 The application has started successfully so let's switch to Chrome and 88 00:06:09,700 --> 00:06:10,810 check it out. 89 00:06:10,810 --> 00:06:14,480 We've got our upload form here which is looking good. 90 00:06:14,480 --> 00:06:18,990 So what I'll do here is all upload a GIF that was originally used in Spring Basics. 91 00:06:18,990 --> 00:06:21,570 I'll browse it, it's in my Downloads directory. 92 00:06:21,570 --> 00:06:24,570 Yes, how about this one, android-explosion? 93 00:06:24,570 --> 00:06:26,020 I will open that. 94 00:06:26,020 --> 00:06:27,720 I will call it Android Explosion, 95 00:06:27,720 --> 00:06:32,000 looks like I've used that one before, and let's select a category. 96 00:06:32,000 --> 00:06:33,740 How about Technology? 97 00:06:35,280 --> 00:06:39,500 Cool, let's hit upload and see if it works. 98 00:06:39,500 --> 00:06:41,920 Hey, look at that, it worked! 99 00:06:41,920 --> 00:06:45,790 Now if you're seeing any sort of error in the browser make sure you head back to 100 00:06:45,790 --> 00:06:52,180 your IDE, IntelliJ in my case, to see the full log of errors in your IDE's console. 101 00:06:53,580 --> 00:06:56,600 That was no trivial task that you just accomplished. 102 00:06:56,600 --> 00:07:00,590 Adding a file upload feature to a web application is a common feature, but 103 00:07:00,590 --> 00:07:04,590 the steps needed to do so can sometimes be complex. 104 00:07:04,590 --> 00:07:07,790 So, getting this to work is something you should be proud of. 105 00:07:07,790 --> 00:07:08,340 I know I am. 106 00:07:09,590 --> 00:07:12,960 There are various considerations that need to be made with file uploads, 107 00:07:12,960 --> 00:07:17,830 namely making sure we limit the size of the files we allowed users to upload and 108 00:07:17,830 --> 00:07:20,790 ensuring that malicious files are not uploaded. 109 00:07:20,790 --> 00:07:24,750 And this can be accomplished by checking to see if the file is indeed a GIF. 110 00:07:26,280 --> 00:07:29,970 This kind of data validation requires us to leverage the custom validation 111 00:07:29,970 --> 00:07:35,050 capabilities of the Spring framework, which is outside the scope of this course. 112 00:07:35,050 --> 00:07:38,080 Check the teacher's notes for more information and know that you'll have 113 00:07:38,080 --> 00:07:41,740 a feature that's waiting for you to address when we're all wrapped up here.