team Media

master
dizda13 2017-04-16 18:00:55 +02:00
parent edd29d79d8
commit 7050680475
5 changed files with 84 additions and 4 deletions

View File

@ -36,7 +36,9 @@ public class FileSystemStorageService implements StorageService {
String[] locations=this.rootLocation.resolve(dest).toString().split("/");
for(int i=0; i<locations.length-1;i++)
tempDest=tempDest+"/"+locations[i];
Files.createDirectory(Paths.get(tempDest));
if(!Files.exists(Paths.get(tempDest)))
Files.createDirectory(Paths.get(tempDest));
Files.copy(file.getInputStream(),
this.rootLocation.resolve(dest));

View File

@ -67,7 +67,7 @@ public class EventGalleryController {
"You successfully uploaded " + file.getOriginalFilename() + "!");
Media media = repository.findOne(mediaId);
media.setContentUrl(mediaServiceBase+"/profilePictures/" + dest);
media.setContentUrl(mediaServiceBase +"/" + dest);
repository.save(media);

View File

@ -1,8 +1,10 @@
package ba.steleks;
import ba.steleks.storage.StorageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@ -14,6 +16,7 @@ import java.util.List;
@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class TeamsApplication {
public static void main(String[] args) {

View File

@ -1,7 +1,82 @@
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.
*/
@RestController
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();
}
}

View File

@ -45,13 +45,13 @@ public class ProfilePictureController {
RedirectAttributes redirectAttributes) {
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);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
User user = repository.findOne(userId);
user.setProfilePictureUrl("http://localhost:8090/profilePictures/" + dest);
user.setProfilePictureUrl("http://localhost:8090" + dest);
repository.save(user);
return "redirect:/";