From 2009ceb5ff6d349013cc4b863e4fa682ea9022ae Mon Sep 17 00:00:00 2001 From: esensar Date: Sat, 3 Feb 2018 15:32:24 +0100 Subject: [PATCH] Enable adding event pictures as url --- .../storage/FileSystemStorageService.java | 16 ++++-- .../ba/steleks/storage/StorageProperties.java | 2 +- .../ba/steleks/storage/StorageService.java | 3 ++ config | 2 +- .../controller/EventGalleryController.java | 30 ++++------- .../steleks/controller/MediaController.java | 25 +++++++-- .../storage/MediaStorageHandler.java | 54 +++++++++++++++++++ 7 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 events/src/main/java/ba/steleks/controller/storage/MediaStorageHandler.java diff --git a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java index e906282..8f56a1e 100644 --- a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java +++ b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java @@ -3,6 +3,7 @@ package ba.steleks.storage; import ba.steleks.storage.error.exception.StorageException; import ba.steleks.storage.error.exception.StorageFileNotFoundException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.InputStreamSource; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.stereotype.Service; @@ -33,18 +34,27 @@ public class FileSystemStorageService implements StorageService { throw new StorageException("Failed to store empty file " + file.getOriginalFilename()); } + store((InputStreamSource) file, dest); + } catch (StorageException e) { + throw new StorageException("Failed to store file " + file.getOriginalFilename(), e); + } + } + + @Override + public void store(InputStreamSource inputStreamSource, String dest) { + try { String tempDest=""; String[] locations=this.rootLocation.resolve(dest).toString().split("/"); for(int i=0; i loadAll(); Path load(String filename); diff --git a/config b/config index 932fff9..f7492aa 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit 932fff95a1eb12371809f7d7e2027154b09fdfad +Subproject commit f7492aa6e9e3b1ec809e1088d2b6a3785657970f diff --git a/events/src/main/java/ba/steleks/controller/EventGalleryController.java b/events/src/main/java/ba/steleks/controller/EventGalleryController.java index c5d0316..e0fc62c 100644 --- a/events/src/main/java/ba/steleks/controller/EventGalleryController.java +++ b/events/src/main/java/ba/steleks/controller/EventGalleryController.java @@ -1,15 +1,13 @@ package ba.steleks.controller; +import ba.steleks.controller.storage.MediaStorageHandler; import ba.steleks.error.exception.ExternalServiceException; import ba.steleks.model.Media; import ba.steleks.repository.MediaJpaRepository; -import ba.steleks.service.Service; import ba.steleks.service.discovery.ServiceDiscoveryClient; import ba.steleks.storage.StorageService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; @@ -17,9 +15,6 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; -import java.util.Date; -import java.util.List; - /** * Created by admin on 16/04/2017. */ @@ -29,12 +24,14 @@ public class EventGalleryController { private final StorageService storageService; private final MediaJpaRepository repository; private final ServiceDiscoveryClient discoveryClient; + private final MediaStorageHandler mediaStorageHandler; @Autowired - public EventGalleryController(StorageService storageService, MediaJpaRepository repository, ServiceDiscoveryClient discoveryClient) { + public EventGalleryController(StorageService storageService, MediaJpaRepository repository, ServiceDiscoveryClient discoveryClient, MediaStorageHandler mediaStorageHandler) { this.storageService = storageService; this.repository = repository; this.discoveryClient=discoveryClient; + this.mediaStorageHandler = mediaStorageHandler; } @GetMapping("/eventPictures/{filename:.+}") @@ -48,27 +45,22 @@ public class EventGalleryController { .body(file); } - @PostMapping("/media/{mediaId}/picture") + @PostMapping("/medias/{mediaId}/picture") public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) throws ExternalServiceException { - - - String mediaServiceBase = discoveryClient.getServiceUrl(Service.EVENTS); - System.out.println(mediaServiceBase); - String[] names = file.getOriginalFilename().split("\\."); - String dest = String.valueOf("eventPictures/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1]; - storageService.store(file, dest); + String loadImageEndpoint = mediaStorageHandler.saveMedia(file, mediaId); redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!"); - System.out.println(dest); + System.out.println(loadImageEndpoint); Media media = repository.findOne(mediaId); if(media==null){ media= new Media(); - media.setContentUrl(mediaServiceBase +"/" + dest); + media.setContentUrl(loadImageEndpoint); media.setCreatedById(0); repository.save(media); - }else - media.setContentUrl(mediaServiceBase +"/" + dest); + } else { + media.setContentUrl(loadImageEndpoint); + } repository.save(media); diff --git a/events/src/main/java/ba/steleks/controller/MediaController.java b/events/src/main/java/ba/steleks/controller/MediaController.java index 09cf5e1..166aca9 100644 --- a/events/src/main/java/ba/steleks/controller/MediaController.java +++ b/events/src/main/java/ba/steleks/controller/MediaController.java @@ -2,12 +2,13 @@ package ba.steleks.controller;/** * Created by ensar on 02/04/17. */ +import ba.steleks.controller.storage.MediaStorageHandler; import ba.steleks.error.exception.ExternalServiceException; -import ba.steleks.model.Event; import ba.steleks.model.Media; import ba.steleks.repository.MediaJpaRepository; import ba.steleks.service.Service; import ba.steleks.service.discovery.ServiceDiscoveryClient; +import org.apache.http.util.TextUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.data.rest.webmvc.RepositoryRestController; @@ -18,9 +19,14 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.net.URI; +import java.net.URL; @RepositoryRestController public class MediaController { @@ -28,12 +34,14 @@ public class MediaController { private MediaJpaRepository repository; private RestTemplate restTemplate; private ServiceDiscoveryClient discoveryClient; + private MediaStorageHandler mediaStorageHandler; @Autowired - public MediaController(MediaJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient) { + public MediaController(MediaJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient, MediaStorageHandler mediaStorageHandler) { this.repository = repository; this.restTemplate = restTemplateBuilder.build(); this.discoveryClient = discoveryClient; + this.mediaStorageHandler = mediaStorageHandler; } @RequestMapping(path = "/medias", method = RequestMethod.POST) @@ -41,8 +49,19 @@ public class MediaController { String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS); try { - String response = restTemplate.getForObject(usersServiceBase + "/users/{id}", String.class, media.getCreatedById()); +// String response = restTemplate.getForObject(usersServiceBase + "/users/{id}", String.class, media.getCreatedById()); + BufferedImage mediaImage = null; + if (!TextUtils.isEmpty(media.getContentUrl())) { + mediaImage = ImageIO.read(new URL(media.getContentUrl())); + } Media result = repository.save(media); + + if (mediaImage != null) { + String url = mediaStorageHandler.saveMedia(mediaImage, result.getId()); + result.setContentUrl(url); + repository.save(result); + } + URI location = ServletUriComponentsBuilder .fromCurrentRequest().path("/{id}") .buildAndExpand(result.getId()).toUri(); diff --git a/events/src/main/java/ba/steleks/controller/storage/MediaStorageHandler.java b/events/src/main/java/ba/steleks/controller/storage/MediaStorageHandler.java new file mode 100644 index 0000000..3d25656 --- /dev/null +++ b/events/src/main/java/ba/steleks/controller/storage/MediaStorageHandler.java @@ -0,0 +1,54 @@ +package ba.steleks.controller.storage; + +import ba.steleks.error.exception.ExternalServiceException; +import ba.steleks.service.Service; +import ba.steleks.service.discovery.ServiceDiscoveryClient; +import ba.steleks.storage.StorageService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Date; + +@Component +public class MediaStorageHandler { + + private StorageService storageService; + private ServiceDiscoveryClient discoveryClient; + + @Autowired + public MediaStorageHandler(StorageService storageService, ServiceDiscoveryClient discoveryClient) { + this.storageService = storageService; + this.discoveryClient = discoveryClient; + } + + public String saveMedia(MultipartFile file, long mediaId) throws ExternalServiceException { + String mediaServiceBase = discoveryClient.getServiceUrl(Service.EVENTS); + System.out.println(mediaServiceBase); + String[] names = file.getOriginalFilename().split("\\."); + String dest = String.valueOf("eventPictures/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1]; + storageService.store(file, dest); + return mediaServiceBase + "/" + dest; + } + + public String saveMedia(BufferedImage bufferedImage, long mediaId) throws ExternalServiceException { + try { + String mediaServiceBase = discoveryClient.getServiceUrl(Service.EVENTS); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "jpg", baos); + baos.flush(); + String dest = String.valueOf("eventPictures/" + mediaId + "_" + new Date().getTime()) + ".jpg"; + storageService.store(new ByteArrayResource(baos.toByteArray()), dest); + baos.close(); + return mediaServiceBase + "/" + dest; + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } +}