ServiceDiscoveryClient fix in upload/Download
parent
5314f4b944
commit
158828d854
|
@ -1,24 +0,0 @@
|
||||||
package ba.steleks.service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by ensar on 16/04/17.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class ServiceConstants {
|
|
||||||
private static final Logger logger =
|
|
||||||
Logger.getLogger(ServiceConstants.class.getName());
|
|
||||||
|
|
||||||
static {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Value("${users.name}")
|
|
||||||
public static String USERS_SERVICE_NAME;
|
|
||||||
@Value("${events.name}")
|
|
||||||
public static String EVENTS_SERVICE_NAME;
|
|
||||||
@Value("${teams.name}")
|
|
||||||
public static String TEAMS_SERVICE_NAME;
|
|
||||||
}
|
|
2
config
2
config
|
@ -1 +1 @@
|
||||||
Subproject commit 75a784ba6ed8f6e70ee91808d3752443e3a04d1d
|
Subproject commit 0d028147e90627b077c5435b557d6b3d7d19623c
|
|
@ -1,20 +1,16 @@
|
||||||
package ba.steleks;
|
package ba.steleks;
|
||||||
|
|
||||||
|
|
||||||
import ba.steleks.service.ServiceConstants;
|
|
||||||
import ba.steleks.storage.StorageProperties;
|
import ba.steleks.storage.StorageProperties;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
|
@ -4,6 +4,8 @@ import ba.steleks.error.exception.ExternalServiceException;
|
||||||
import ba.steleks.model.Media;
|
import ba.steleks.model.Media;
|
||||||
import ba.steleks.repository.EventsJpaRepository;
|
import ba.steleks.repository.EventsJpaRepository;
|
||||||
import ba.steleks.repository.MediaJpaRepository;
|
import ba.steleks.repository.MediaJpaRepository;
|
||||||
|
import ba.steleks.service.Service;
|
||||||
|
import ba.steleks.service.discovery.ServiceDiscoveryClient;
|
||||||
import ba.steleks.storage.StorageFileNotFoundException;
|
import ba.steleks.storage.StorageFileNotFoundException;
|
||||||
import ba.steleks.storage.StorageService;
|
import ba.steleks.storage.StorageService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -27,10 +29,10 @@ public class EventGalleryController {
|
||||||
|
|
||||||
private final StorageService storageService;
|
private final StorageService storageService;
|
||||||
private final MediaJpaRepository repository;
|
private final MediaJpaRepository repository;
|
||||||
private final DiscoveryClient discoveryClient;
|
private final ServiceDiscoveryClient discoveryClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public EventGalleryController(StorageService storageService, MediaJpaRepository repository, DiscoveryClient discoveryClient) {
|
public EventGalleryController(StorageService storageService, MediaJpaRepository repository, ServiceDiscoveryClient discoveryClient) {
|
||||||
this.storageService = storageService;
|
this.storageService = storageService;
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.discoveryClient=discoveryClient;
|
this.discoveryClient=discoveryClient;
|
||||||
|
@ -51,14 +53,8 @@ public class EventGalleryController {
|
||||||
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 {
|
||||||
|
|
||||||
List<ServiceInstance> 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 = discoveryClient.getServiceUrl(Service.EVENTS);
|
||||||
String mediaServiceBase = usersService.getUri().toString();
|
|
||||||
|
|
||||||
String[] names = file.getOriginalFilename().split("\\.");
|
String[] names = file.getOriginalFilename().split("\\.");
|
||||||
String dest = String.valueOf("mediaEvent/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
String dest = String.valueOf("mediaEvent/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
||||||
|
|
|
@ -3,6 +3,8 @@ package ba.steleks.controller;
|
||||||
import ba.steleks.error.exception.ExternalServiceException;
|
import ba.steleks.error.exception.ExternalServiceException;
|
||||||
import ba.steleks.model.TeamMedia;
|
import ba.steleks.model.TeamMedia;
|
||||||
import ba.steleks.repository.TeamsMediaJpaRepository;
|
import ba.steleks.repository.TeamsMediaJpaRepository;
|
||||||
|
import ba.steleks.service.Service;
|
||||||
|
import ba.steleks.service.discovery.ServiceDiscoveryClient;
|
||||||
import ba.steleks.storage.StorageFileNotFoundException;
|
import ba.steleks.storage.StorageFileNotFoundException;
|
||||||
import ba.steleks.storage.StorageService;
|
import ba.steleks.storage.StorageService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -26,10 +28,10 @@ public class TeamGalleryController {
|
||||||
|
|
||||||
private final StorageService storageService;
|
private final StorageService storageService;
|
||||||
private final TeamsMediaJpaRepository repository;
|
private final TeamsMediaJpaRepository repository;
|
||||||
private final DiscoveryClient discoveryClient;
|
private final ServiceDiscoveryClient discoveryClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public TeamGalleryController(StorageService storageService, TeamsMediaJpaRepository repository, DiscoveryClient discoveryClient) {
|
public TeamGalleryController(StorageService storageService, TeamsMediaJpaRepository repository, ServiceDiscoveryClient discoveryClient) {
|
||||||
this.storageService = storageService;
|
this.storageService = storageService;
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.discoveryClient=discoveryClient;
|
this.discoveryClient=discoveryClient;
|
||||||
|
@ -50,14 +52,8 @@ public class TeamGalleryController {
|
||||||
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 {
|
||||||
|
|
||||||
List<ServiceInstance> 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 = discoveryClient.getServiceUrl(Service.TEAMS);
|
||||||
String mediaServiceBase = usersService.getUri().toString();
|
|
||||||
|
|
||||||
String[] names = file.getOriginalFilename().split("\\.");
|
String[] names = file.getOriginalFilename().split("\\.");
|
||||||
String dest = String.valueOf("teamPictures/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
String dest = String.valueOf("teamPictures/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package ba.steleks.controller;
|
package ba.steleks.controller;
|
||||||
|
|
||||||
|
import ba.steleks.error.exception.ExternalServiceException;
|
||||||
import ba.steleks.model.User;
|
import ba.steleks.model.User;
|
||||||
import ba.steleks.repository.UsersJpaRepository;
|
import ba.steleks.repository.UsersJpaRepository;
|
||||||
|
import ba.steleks.service.Service;
|
||||||
|
import ba.steleks.service.discovery.ServiceDiscoveryClient;
|
||||||
import ba.steleks.storage.StorageFileNotFoundException;
|
import ba.steleks.storage.StorageFileNotFoundException;
|
||||||
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.data.rest.webmvc.RepositoryRestController;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
@ -22,18 +24,20 @@ public class ProfilePictureController {
|
||||||
|
|
||||||
private final StorageService storageService;
|
private final StorageService storageService;
|
||||||
private final UsersJpaRepository repository;
|
private final UsersJpaRepository repository;
|
||||||
|
private final ServiceDiscoveryClient discoveryClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ProfilePictureController(StorageService storageService, UsersJpaRepository repository) {
|
public ProfilePictureController(StorageService storageService, UsersJpaRepository repository, ServiceDiscoveryClient discoveryClient) {
|
||||||
this.storageService = storageService;
|
this.storageService = storageService;
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
|
this.discoveryClient = discoveryClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/profilePictures/{filename:.+}")
|
@GetMapping("/profilePictures/{filename:.+}")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
|
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
|
||||||
|
|
||||||
Resource file = storageService.loadAsResource(filename);
|
Resource file = storageService.loadAsResource("profilePictures/"+ filename);
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.ok()
|
.ok()
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"")
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"")
|
||||||
|
@ -42,7 +46,9 @@ public class ProfilePictureController {
|
||||||
|
|
||||||
@PostMapping("/users/{userId}/profilePicture")
|
@PostMapping("/users/{userId}/profilePicture")
|
||||||
public String handleFileUpload(@PathVariable Long userId, @RequestParam("file") MultipartFile file,
|
public String handleFileUpload(@PathVariable Long userId, @RequestParam("file") MultipartFile file,
|
||||||
RedirectAttributes redirectAttributes) {
|
RedirectAttributes redirectAttributes) throws ExternalServiceException {
|
||||||
|
|
||||||
|
String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS);
|
||||||
|
|
||||||
String[] names = file.getOriginalFilename().split("\\.");
|
String[] names = file.getOriginalFilename().split("\\.");
|
||||||
String dest = String.valueOf("profilePictures/" + userId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
String dest = String.valueOf("profilePictures/" + userId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
||||||
|
@ -51,7 +57,7 @@ public class ProfilePictureController {
|
||||||
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
||||||
|
|
||||||
User user = repository.findOne(userId);
|
User user = repository.findOne(userId);
|
||||||
user.setProfilePictureUrl("http://localhost:8090" + dest);
|
user.setProfilePictureUrl(usersServiceBase + "/" + dest);
|
||||||
repository.save(user);
|
repository.save(user);
|
||||||
|
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
|
|
Reference in New Issue