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(); + } +}