-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyController.java
99 lines (82 loc) · 3.04 KB
/
MyController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ua.kiev.prog;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView ;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping(value="/")
public class MyController {
private Map<Long, byte[]> photos = new HashMap<Long, byte[]>();
@RequestMapping("/")
public String onIndex() {
return "index";
}
@RequestMapping(value = "/add_photo", method = RequestMethod.POST)
public String onAddPhoto(Model model, @RequestParam MultipartFile photo) {
if (photo.isEmpty())
throw new PhotoErrorException();
try {
long id = System.currentTimeMillis();
photos.put(id, photo.getBytes());
model.addAttribute("photo_id", id);
return "result";
} catch (IOException e) {
throw new PhotoErrorException();
}
}
@RequestMapping(value="/photo/{photo_id}")
public ResponseEntity<byte[]> onPhoto(@PathVariable("photo_id") long id) {
return photoById(id);
}
@RequestMapping(value = "/view", method = RequestMethod.POST)
public ResponseEntity<byte[]> onView(@RequestParam("photo_id") long id) {
return photoById(id);
}
@RequestMapping(value = "/viewall")
public ModelAndView onViewall(ModelAndView mav) {
mav.setViewName("list");
mav.addObject("onlyList", "1");
mav.addObject("viewList", photos.keySet());
return mav;
}
@RequestMapping(value = "/viewlist")
public ModelAndView onViewlist(ModelAndView mav) {
mav.setViewName("list");
mav.addObject("onlyList", "2");
mav.addObject("viewList", photos.keySet());
return mav;
}
@RequestMapping(value="/delete/{photo_id}")
public String onDelete(@PathVariable("photo_id") long id) {
if (photos.remove(id) == null)
throw new PhotoNotFoundException();
else
return "index";
}
@RequestMapping(value="/deleteselected", method = RequestMethod.POST)
public String onDeleteSelected(@RequestParam(value="photo_id", required=false) long[] id) {
if(id == null)
return "index";
for(long photo_id : id){
if (photos.remove(photo_id) == null)
throw new PhotoNotFoundException();
}
return "index";
}
private ResponseEntity<byte[]> photoById(long id) {
byte[] bytes = photos.get(id);
if (bytes == null)
throw new PhotoNotFoundException();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
}
}