From 7050680475bf4d35c3572b74f7c85c748669b4d2 Mon Sep 17 00:00:00 2001 From: dizda13 Date: Sun, 16 Apr 2017 18:00:55 +0200 Subject: [PATCH] team Media --- .../storage/FileSystemStorageService.java | 4 +- .../controller/EventGalleryController.java | 2 +- .../java/ba/steleks/TeamsApplication.java | 3 + .../controller/TeamGalleryController.java | 75 +++++++++++++++++++ .../controller/ProfilePictureController.java | 4 +- 5 files changed, 84 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java index 9e91224..750d496 100644 --- a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java +++ b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java @@ -36,7 +36,9 @@ public class FileSystemStorageService implements StorageService { String[] locations=this.rootLocation.resolve(dest).toString().split("/"); for(int i=0; i serveFile(@PathVariable String filename) { + + Resource file = storageService.loadAsResource("teamPictures/"+filename); + return ResponseEntity + .ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"") + .body(file); + } + + @PostMapping("/teamMedia/{mediaId}/picture") + public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file, + RedirectAttributes redirectAttributes) throws ExternalServiceException { + + List usersInstances = discoveryClient.getInstances("teams"); + if(usersInstances == null || usersInstances.size() == 0) { + System.err.print("Users service not found!"); + throw new ExternalServiceException(); + } + + ServiceInstance usersService = usersInstances.get(0); + String mediaServiceBase = usersService.getUri().toString(); + + String[] names = file.getOriginalFilename().split("\\."); + String dest = String.valueOf("teamPictures/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1]; + storageService.store(file, dest); + redirectAttributes.addFlashAttribute("message", + "You successfully uploaded " + file.getOriginalFilename() + "!"); + + TeamMedia teamMedia = repository.findOne(mediaId); + + teamMedia.setContentUrl(mediaServiceBase +"/"+ dest); + + repository.save(teamMedia); + + return "redirect:/"; + } + + @ExceptionHandler(StorageFileNotFoundException.class) + public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) { + return ResponseEntity.notFound().build(); + } + } diff --git a/users/src/main/java/ba/steleks/controller/ProfilePictureController.java b/users/src/main/java/ba/steleks/controller/ProfilePictureController.java index fadc321..18eac2a 100644 --- a/users/src/main/java/ba/steleks/controller/ProfilePictureController.java +++ b/users/src/main/java/ba/steleks/controller/ProfilePictureController.java @@ -45,13 +45,13 @@ public class ProfilePictureController { RedirectAttributes redirectAttributes) { String[] names = file.getOriginalFilename().split("\\."); - String dest = String.valueOf(userId + "_" + new Date().getTime()) + "." + names[names.length - 1]; + String dest = String.valueOf("profilePictures/" + userId + "_" + new Date().getTime()) + "." + names[names.length - 1]; storageService.store(file, dest); redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!"); User user = repository.findOne(userId); - user.setProfilePictureUrl("http://localhost:8090/profilePictures/" + dest); + user.setProfilePictureUrl("http://localhost:8090" + dest); repository.save(user); return "redirect:/";