FEAT : upload en local /opt/photo-app/uploads + visualisation photo
This commit is contained in:
@@ -2,6 +2,7 @@ package local.epul4a.fotosharing.controller;
|
||||
|
||||
import local.epul4a.fotosharing.model.Photo;
|
||||
import local.epul4a.fotosharing.model.Utilisateur;
|
||||
import local.epul4a.fotosharing.repository.PhotoRepository;
|
||||
import local.epul4a.fotosharing.security.CustomUserDetails;
|
||||
import local.epul4a.fotosharing.service.PhotoService;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -16,15 +17,18 @@ import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Controller
|
||||
public class PhotoController {
|
||||
|
||||
private final PhotoService photoService;
|
||||
private final PhotoRepository photoRepository;
|
||||
|
||||
public PhotoController(PhotoService photoService) {
|
||||
public PhotoController(PhotoService photoService, PhotoRepository photoRepository) {
|
||||
this.photoService = photoService;
|
||||
this.photoRepository = photoRepository;
|
||||
}
|
||||
|
||||
@GetMapping("/upload")
|
||||
@@ -35,12 +39,13 @@ public class PhotoController {
|
||||
@PostMapping("/upload")
|
||||
public String doUpload(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(value="visibilite", defaultValue = "PRIVATE") String visibilite,
|
||||
@AuthenticationPrincipal CustomUserDetails user,
|
||||
Authentication authentication,
|
||||
Model model) {
|
||||
try {
|
||||
Photo p = photoService.store(file, visibilite, user.getUsername());
|
||||
String email = authentication.getName(); // l'email de l'utilisateur connecté
|
||||
Photo p = photoService.store(file, visibilite, email);
|
||||
model.addAttribute("message", "Upload OK : " + p.getId());
|
||||
return "redirect:/"; // ou page d'affichage
|
||||
return "redirect:/";
|
||||
} catch (Exception e) {
|
||||
model.addAttribute("error", e.getMessage());
|
||||
return "upload";
|
||||
@@ -48,22 +53,38 @@ public class PhotoController {
|
||||
}
|
||||
|
||||
@GetMapping("/photo/{id}/raw")
|
||||
public ResponseEntity<Resource> rawPhoto(@PathVariable("id") String idOrUuid) {
|
||||
// idOrUuid peut être uuid stocké ou id numeric ; ici on assume uuid
|
||||
Path p = photoService.loadAsPath(idOrUuid);
|
||||
public ResponseEntity<Resource> rawPhoto(@PathVariable("id") Long id) {
|
||||
Photo photo = photoRepository.findById(id).orElse(null);
|
||||
if (photo == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
Path p = photoService.loadAsPath(photo.getUuidFichier());
|
||||
Resource r = new PathResource(p);
|
||||
|
||||
if (!r.exists()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
String contentType = "application/octet-stream";
|
||||
try {
|
||||
// tentative de détection basique
|
||||
contentType = java.nio.file.Files.probeContentType(p);
|
||||
contentType = Files.probeContentType(p);
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType(contentType != null ? contentType : "application/octet-stream"))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + p.getFileName().toString() + "\"")
|
||||
.contentType(MediaType.parseMediaType(contentType))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"inline; filename=\"" + photo.getNomFichierOriginal() + "\"")
|
||||
.body(r);
|
||||
}
|
||||
|
||||
@GetMapping("/mes-photos")
|
||||
public String mesPhotos(Model model, Authentication authentication) {
|
||||
String email = authentication.getName();
|
||||
model.addAttribute("photos", photoService.listByOwner(email));
|
||||
return "mes-photos";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user