team Media
parent
edd29d79d8
commit
7050680475
|
@ -36,6 +36,8 @@ public class FileSystemStorageService implements StorageService {
|
||||||
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)))
|
||||||
Files.createDirectory(Paths.get(tempDest));
|
Files.createDirectory(Paths.get(tempDest));
|
||||||
|
|
||||||
Files.copy(file.getInputStream(),
|
Files.copy(file.getInputStream(),
|
||||||
|
|
|
@ -67,7 +67,7 @@ public class EventGalleryController {
|
||||||
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
||||||
|
|
||||||
Media media = repository.findOne(mediaId);
|
Media media = repository.findOne(mediaId);
|
||||||
media.setContentUrl(mediaServiceBase+"/profilePictures/" + dest);
|
media.setContentUrl(mediaServiceBase +"/" + dest);
|
||||||
|
|
||||||
repository.save(media);
|
repository.save(media);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package ba.steleks;
|
package ba.steleks;
|
||||||
|
|
||||||
|
import ba.steleks.storage.StorageProperties;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
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.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;
|
||||||
|
@ -14,6 +16,7 @@ import java.util.List;
|
||||||
|
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableConfigurationProperties(StorageProperties.class)
|
||||||
public class TeamsApplication {
|
public class TeamsApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -1,7 +1,82 @@
|
||||||
package ba.steleks.controller;
|
package ba.steleks.controller;
|
||||||
|
|
||||||
|
import ba.steleks.error.exception.ExternalServiceException;
|
||||||
|
import ba.steleks.model.TeamMedia;
|
||||||
|
import ba.steleks.repository.TeamsMediaJpaRepository;
|
||||||
|
import ba.steleks.storage.StorageFileNotFoundException;
|
||||||
|
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;
|
||||||
|
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.
|
* Created by admin on 16/04/2017.
|
||||||
*/
|
*/
|
||||||
|
@RestController
|
||||||
public class TeamGalleryController {
|
public class TeamGalleryController {
|
||||||
|
|
||||||
|
private final StorageService storageService;
|
||||||
|
private final TeamsMediaJpaRepository repository;
|
||||||
|
private final DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public TeamGalleryController(StorageService storageService, TeamsMediaJpaRepository repository, DiscoveryClient discoveryClient) {
|
||||||
|
this.storageService = storageService;
|
||||||
|
this.repository = repository;
|
||||||
|
this.discoveryClient=discoveryClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/teamPictures/{filename:.+}")
|
||||||
|
@ResponseBody
|
||||||
|
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
|
||||||
|
|
||||||
|
Resource file = storageService.loadAsResource("teamPictures/"+filename);
|
||||||
|
return ResponseEntity
|
||||||
|
.ok()
|
||||||
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"")
|
||||||
|
.body(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/teamMedia/{mediaId}/picture")
|
||||||
|
public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file,
|
||||||
|
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 = usersService.getUri().toString();
|
||||||
|
|
||||||
|
String[] names = file.getOriginalFilename().split("\\.");
|
||||||
|
String dest = String.valueOf("teamPictures/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
||||||
|
storageService.store(file, dest);
|
||||||
|
redirectAttributes.addFlashAttribute("message",
|
||||||
|
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
||||||
|
|
||||||
|
TeamMedia teamMedia = repository.findOne(mediaId);
|
||||||
|
|
||||||
|
teamMedia.setContentUrl(mediaServiceBase +"/"+ dest);
|
||||||
|
|
||||||
|
repository.save(teamMedia);
|
||||||
|
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(StorageFileNotFoundException.class)
|
||||||
|
public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,13 +45,13 @@ public class ProfilePictureController {
|
||||||
RedirectAttributes redirectAttributes) {
|
RedirectAttributes redirectAttributes) {
|
||||||
|
|
||||||
String[] names = file.getOriginalFilename().split("\\.");
|
String[] names = file.getOriginalFilename().split("\\.");
|
||||||
String dest = String.valueOf(userId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
String dest = String.valueOf("profilePictures/" + userId + "_" + new Date().getTime()) + "." + names[names.length - 1];
|
||||||
storageService.store(file, dest);
|
storageService.store(file, dest);
|
||||||
redirectAttributes.addFlashAttribute("message",
|
redirectAttributes.addFlashAttribute("message",
|
||||||
"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/profilePictures/" + dest);
|
user.setProfilePictureUrl("http://localhost:8090" + dest);
|
||||||
repository.save(user);
|
repository.save(user);
|
||||||
|
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
|
|
Reference in New Issue