diff --git a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java index f7dea03..e906282 100644 --- a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java +++ b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java @@ -37,7 +37,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 9a33a1e..ea39f2b 100644 --- a/users/src/main/java/ba/steleks/controller/ProfilePictureController.java +++ b/users/src/main/java/ba/steleks/controller/ProfilePictureController.java @@ -43,13 +43,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:/";