Enable adding event pictures as url
parent
7b9c81e14b
commit
2009ceb5ff
|
@ -3,6 +3,7 @@ package ba.steleks.storage;
|
||||||
import ba.steleks.storage.error.exception.StorageException;
|
import ba.steleks.storage.error.exception.StorageException;
|
||||||
import ba.steleks.storage.error.exception.StorageFileNotFoundException;
|
import ba.steleks.storage.error.exception.StorageFileNotFoundException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.io.InputStreamSource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.UrlResource;
|
import org.springframework.core.io.UrlResource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -33,18 +34,27 @@ public class FileSystemStorageService implements StorageService {
|
||||||
throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
|
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 tempDest="";
|
||||||
String[] locations=this.rootLocation.resolve(dest).toString().split("/");
|
String[] locations=this.rootLocation.resolve(dest).toString().split("/");
|
||||||
for(int i=0; i<locations.length-1;i++)
|
for(int i=0; i<locations.length-1;i++)
|
||||||
tempDest=tempDest+"/"+locations[i];
|
tempDest=tempDest+"/"+locations[i];
|
||||||
|
|
||||||
if(!Files.exists(Paths.get(tempDest)))
|
if(!Files.exists(Paths.get(tempDest)))
|
||||||
Files.createDirectory(Paths.get(tempDest));
|
Files.createDirectory(Paths.get(tempDest));
|
||||||
|
|
||||||
Files.copy(file.getInputStream(),
|
Files.copy(inputStreamSource.getInputStream(),
|
||||||
this.rootLocation.resolve(dest));
|
this.rootLocation.resolve(dest));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
|
throw new StorageException("Failed to store file " + inputStreamSource.toString(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ public class StorageProperties {
|
||||||
/**
|
/**
|
||||||
* Folder location for storing files
|
* Folder location for storing files
|
||||||
*/
|
*/
|
||||||
@Value("{default.storage.directory}")
|
@Value("${default.storage.directory}")
|
||||||
private String location;
|
private String location;
|
||||||
|
|
||||||
public String getLocation() {
|
public String getLocation() {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package ba.steleks.storage;
|
package ba.steleks.storage;
|
||||||
|
|
||||||
|
import org.springframework.core.io.InputStreamSource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
@ -12,6 +13,8 @@ public interface StorageService {
|
||||||
|
|
||||||
void store(MultipartFile file, String dest);
|
void store(MultipartFile file, String dest);
|
||||||
|
|
||||||
|
void store(InputStreamSource inputStreamSource, String dest);
|
||||||
|
|
||||||
Stream<Path> loadAll();
|
Stream<Path> loadAll();
|
||||||
|
|
||||||
Path load(String filename);
|
Path load(String filename);
|
||||||
|
|
2
config
2
config
|
@ -1 +1 @@
|
||||||
Subproject commit 932fff95a1eb12371809f7d7e2027154b09fdfad
|
Subproject commit f7492aa6e9e3b1ec809e1088d2b6a3785657970f
|
|
@ -1,15 +1,13 @@
|
||||||
package ba.steleks.controller;
|
package ba.steleks.controller;
|
||||||
|
|
||||||
|
import ba.steleks.controller.storage.MediaStorageHandler;
|
||||||
import ba.steleks.error.exception.ExternalServiceException;
|
import ba.steleks.error.exception.ExternalServiceException;
|
||||||
import ba.steleks.model.Media;
|
import ba.steleks.model.Media;
|
||||||
import ba.steleks.repository.MediaJpaRepository;
|
import ba.steleks.repository.MediaJpaRepository;
|
||||||
|
|
||||||
import ba.steleks.service.Service;
|
|
||||||
import ba.steleks.service.discovery.ServiceDiscoveryClient;
|
import ba.steleks.service.discovery.ServiceDiscoveryClient;
|
||||||
import ba.steleks.storage.StorageService;
|
import ba.steleks.storage.StorageService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.core.io.Resource;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.ResponseEntity;
|
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.multipart.MultipartFile;
|
||||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by admin on 16/04/2017.
|
* Created by admin on 16/04/2017.
|
||||||
*/
|
*/
|
||||||
|
@ -29,12 +24,14 @@ public class EventGalleryController {
|
||||||
private final StorageService storageService;
|
private final StorageService storageService;
|
||||||
private final MediaJpaRepository repository;
|
private final MediaJpaRepository repository;
|
||||||
private final ServiceDiscoveryClient discoveryClient;
|
private final ServiceDiscoveryClient discoveryClient;
|
||||||
|
private final MediaStorageHandler mediaStorageHandler;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public EventGalleryController(StorageService storageService, MediaJpaRepository repository, ServiceDiscoveryClient discoveryClient) {
|
public EventGalleryController(StorageService storageService, MediaJpaRepository repository, ServiceDiscoveryClient discoveryClient, MediaStorageHandler mediaStorageHandler) {
|
||||||
this.storageService = storageService;
|
this.storageService = storageService;
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.discoveryClient=discoveryClient;
|
this.discoveryClient=discoveryClient;
|
||||||
|
this.mediaStorageHandler = mediaStorageHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/eventPictures/{filename:.+}")
|
@GetMapping("/eventPictures/{filename:.+}")
|
||||||
|
@ -48,27 +45,22 @@ public class EventGalleryController {
|
||||||
.body(file);
|
.body(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/media/{mediaId}/picture")
|
@PostMapping("/medias/{mediaId}/picture")
|
||||||
public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file,
|
public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file,
|
||||||
RedirectAttributes redirectAttributes) throws ExternalServiceException {
|
RedirectAttributes redirectAttributes) throws ExternalServiceException {
|
||||||
|
String loadImageEndpoint = mediaStorageHandler.saveMedia(file, mediaId);
|
||||||
|
|
||||||
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);
|
|
||||||
redirectAttributes.addFlashAttribute("message",
|
redirectAttributes.addFlashAttribute("message",
|
||||||
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
||||||
System.out.println(dest);
|
System.out.println(loadImageEndpoint);
|
||||||
Media media = repository.findOne(mediaId);
|
Media media = repository.findOne(mediaId);
|
||||||
if(media==null){
|
if(media==null){
|
||||||
media= new Media();
|
media= new Media();
|
||||||
media.setContentUrl(mediaServiceBase +"/" + dest);
|
media.setContentUrl(loadImageEndpoint);
|
||||||
media.setCreatedById(0);
|
media.setCreatedById(0);
|
||||||
repository.save(media);
|
repository.save(media);
|
||||||
}else
|
} else {
|
||||||
media.setContentUrl(mediaServiceBase +"/" + dest);
|
media.setContentUrl(loadImageEndpoint);
|
||||||
|
}
|
||||||
|
|
||||||
repository.save(media);
|
repository.save(media);
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,13 @@ package ba.steleks.controller;/**
|
||||||
* Created by ensar on 02/04/17.
|
* Created by ensar on 02/04/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import ba.steleks.controller.storage.MediaStorageHandler;
|
||||||
import ba.steleks.error.exception.ExternalServiceException;
|
import ba.steleks.error.exception.ExternalServiceException;
|
||||||
import ba.steleks.model.Event;
|
|
||||||
import ba.steleks.model.Media;
|
import ba.steleks.model.Media;
|
||||||
import ba.steleks.repository.MediaJpaRepository;
|
import ba.steleks.repository.MediaJpaRepository;
|
||||||
import ba.steleks.service.Service;
|
import ba.steleks.service.Service;
|
||||||
import ba.steleks.service.discovery.ServiceDiscoveryClient;
|
import ba.steleks.service.discovery.ServiceDiscoveryClient;
|
||||||
|
import org.apache.http.util.TextUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
import org.springframework.data.rest.webmvc.RepositoryRestController;
|
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.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
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 org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
@RepositoryRestController
|
@RepositoryRestController
|
||||||
public class MediaController {
|
public class MediaController {
|
||||||
|
@ -28,12 +34,14 @@ public class MediaController {
|
||||||
private MediaJpaRepository repository;
|
private MediaJpaRepository repository;
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
private ServiceDiscoveryClient discoveryClient;
|
private ServiceDiscoveryClient discoveryClient;
|
||||||
|
private MediaStorageHandler mediaStorageHandler;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public MediaController(MediaJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient) {
|
public MediaController(MediaJpaRepository repository, RestTemplateBuilder restTemplateBuilder, ServiceDiscoveryClient discoveryClient, MediaStorageHandler mediaStorageHandler) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.restTemplate = restTemplateBuilder.build();
|
this.restTemplate = restTemplateBuilder.build();
|
||||||
this.discoveryClient = discoveryClient;
|
this.discoveryClient = discoveryClient;
|
||||||
|
this.mediaStorageHandler = mediaStorageHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = "/medias", method = RequestMethod.POST)
|
@RequestMapping(path = "/medias", method = RequestMethod.POST)
|
||||||
|
@ -41,8 +49,19 @@ public class MediaController {
|
||||||
|
|
||||||
String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS);
|
String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS);
|
||||||
try {
|
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);
|
Media result = repository.save(media);
|
||||||
|
|
||||||
|
if (mediaImage != null) {
|
||||||
|
String url = mediaStorageHandler.saveMedia(mediaImage, result.getId());
|
||||||
|
result.setContentUrl(url);
|
||||||
|
repository.save(result);
|
||||||
|
}
|
||||||
|
|
||||||
URI location = ServletUriComponentsBuilder
|
URI location = ServletUriComponentsBuilder
|
||||||
.fromCurrentRequest().path("/{id}")
|
.fromCurrentRequest().path("/{id}")
|
||||||
.buildAndExpand(result.getId()).toUri();
|
.buildAndExpand(result.getId()).toUri();
|
||||||
|
|
|
@ -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 "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue