1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.writer.controllers;
17
18 import org.apache.commons.io.IOUtils;
19 import org.kuali.mobility.writer.entity.Media;
20 import org.kuali.mobility.writer.service.WriterService;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.beans.factory.annotation.Qualifier;
23 import org.springframework.stereotype.Controller;
24 import org.springframework.ui.Model;
25 import org.springframework.web.bind.annotation.*;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32
33
34
35
36
37
38
39 @Controller
40 @RequestMapping("/writer/{instance}/media")
41 public class WriterMediaController {
42
43
44
45
46 @Autowired
47 @Qualifier("writerService")
48 private WriterService service;
49
50
51 @RequestMapping(value = "/{mediaId}", method = RequestMethod.GET)
52 public void getMedia(
53 @PathVariable int mediaId,
54 @RequestParam(value="thumb", required=false, defaultValue="false") boolean thumb,
55 HttpServletRequest request,
56 HttpServletResponse response) throws IOException {
57
58 long dateChanged = request.getDateHeader("If-Modified-Since") / 1000;
59
60 File mediaFile = service.getMediaFile(mediaId, thumb);
61 long mediaChanged = mediaFile.lastModified() / 1000;
62 if (dateChanged == mediaChanged){
63 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
64 return;
65 }
66 Media media = service.getMedia(mediaId);
67 response.setContentType(media.getMimeType());
68 response.setDateHeader("Last-Modified", mediaFile.lastModified());
69
70 int bytesWritten = IOUtils.copy(new FileInputStream(mediaFile), response.getOutputStream());
71 response.setContentLength(bytesWritten);
72
73 }
74
75
76
77
78 @RequestMapping(value = "/view/{mediaId}", method = RequestMethod.GET)
79 public String viewImage(
80 @PathVariable(value="mediaId") long mediaId,
81 @PathVariable("instance") String instance,
82 Model uiModel) {
83
84
85 uiModel.addAttribute("mediaId", mediaId);
86 uiModel.addAttribute("toolInstance", instance);
87 return "writer/viewImage";
88 }
89
90 }