Advertisement
rizkyshelvi

UploadController

Apr 22nd, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package com.avrist.avrist.controller;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.UUID;
  7.  
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.core.io.Resource;
  10. import org.springframework.http.HttpHeaders;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.web.bind.annotation.CrossOrigin;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
  22.  
  23. import com.avrist.avrist.models.UploadFile;
  24. import com.avrist.avrist.service.StorageService;
  25. import org.apache.commons.io.FilenameUtils;
  26.  
  27. @CrossOrigin(origins = "*", maxAge = 3600, allowedHeaders = "*")
  28. @RestController
  29. @RequestMapping("/api")
  30. public class UploadController {
  31. @Autowired
  32. StorageService storageService;
  33.  
  34. List<String> files = new ArrayList<String>();
  35.  
  36. @PostMapping("/image")
  37. public UploadFile handleFileUpload(@RequestParam("file") MultipartFile file, @RequestParam("url") String url) {
  38.  
  39. try {
  40.  
  41. String filename = file.getOriginalFilename();
  42. UUID randomName = UUID.randomUUID();
  43. String hashName = randomName + "."+FilenameUtils.getExtension(filename);
  44.  
  45. files.add(file.getOriginalFilename());
  46. String Name = "/"+ url + "/" + hashName;
  47. String fileName = file.getOriginalFilename();
  48. String fileType = file.getContentType();
  49. Long fileSize = file.getSize();
  50. String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
  51. .path("/api/image/"+ url + "/")
  52. .path(hashName)
  53. .toUriString();
  54. storageService.store(file, url, hashName);
  55. return new UploadFile(Name, fileDownloadUri, fileType, fileSize);
  56. } catch (Exception e) {
  57. return new UploadFile("", "", "", file.getSize());
  58. }
  59. }
  60.  
  61. @GetMapping("/image/{folder}/{filename:.+}")
  62. @ResponseBody
  63. public ResponseEntity<Resource> getFile(@PathVariable String filename, @PathVariable String folder) {
  64. Resource file = storageService.loadFile(folder, filename);
  65. return ResponseEntity.ok()
  66. .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
  67. .body(file);
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement