From 9a715a4a09d2470f8c0fc0ab72ae7473a61294b3 Mon Sep 17 00:00:00 2001 From: dizda13 Date: Sun, 16 Apr 2017 17:22:23 +0200 Subject: [PATCH] event gallery upload/download --- .../storage/FileSystemStorageService.java | 8 ++ .../ba/steleks/storage/StorageException.java | 0 .../storage/StorageFileNotFoundException.java | 0 .../ba/steleks/storage/StorageProperties.java | 0 .../ba/steleks/storage/StorageService.java | 0 .../java/ba/steleks/EventsApplication.java | 4 + .../controller/EventGalleryController.java | 81 +++++++++++++++++++ 7 files changed, 93 insertions(+) rename {users => common}/src/main/java/ba/steleks/storage/FileSystemStorageService.java (88%) rename {users => common}/src/main/java/ba/steleks/storage/StorageException.java (100%) rename {users => common}/src/main/java/ba/steleks/storage/StorageFileNotFoundException.java (100%) rename {users => common}/src/main/java/ba/steleks/storage/StorageProperties.java (100%) rename {users => common}/src/main/java/ba/steleks/storage/StorageService.java (100%) create mode 100644 events/src/main/java/ba/steleks/controller/EventGalleryController.java diff --git a/users/src/main/java/ba/steleks/storage/FileSystemStorageService.java b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java similarity index 88% rename from users/src/main/java/ba/steleks/storage/FileSystemStorageService.java rename to common/src/main/java/ba/steleks/storage/FileSystemStorageService.java index 4906eac..9e91224 100644 --- a/users/src/main/java/ba/steleks/storage/FileSystemStorageService.java +++ b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java @@ -12,6 +12,7 @@ import java.net.MalformedURLException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.FileOwnerAttributeView; import java.util.stream.Stream; @Service @@ -30,6 +31,13 @@ public class FileSystemStorageService implements StorageService { if (file.isEmpty()) { throw new StorageException("Failed to store empty file " + file.getOriginalFilename()); } + + String tempDest=""; + String[] locations=this.rootLocation.resolve(dest).toString().split("/"); + for(int i=0; i serveFile(@PathVariable String filename) { + + Resource file = storageService.loadAsResource("mediaEvent/"+filename); + return ResponseEntity + .ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"") + .body(file); + } + + @PostMapping("/media/{mediaId}/picture") + public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file, + RedirectAttributes redirectAttributes) throws ExternalServiceException { + + List usersInstances = discoveryClient.getInstances("events"); + 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("mediaEvent/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1]; + storageService.store(file, dest); + redirectAttributes.addFlashAttribute("message", + "You successfully uploaded " + file.getOriginalFilename() + "!"); + + Media media = repository.findOne(mediaId); + media.setContentUrl(mediaServiceBase+"/profilePictures/" + dest); + + repository.save(media); + + return "redirect:/"; + } + + @ExceptionHandler(StorageFileNotFoundException.class) + public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) { + return ResponseEntity.notFound().build(); + } +}