FEAT : Mise en place de l'architecture + instription d'un utilisateur
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package local.epul4a.fotosharing.controller;
|
||||
|
||||
import local.epul4a.fotosharing.model.Photo;
|
||||
import local.epul4a.fotosharing.security.CustomUserDetails;
|
||||
import local.epul4a.fotosharing.service.PhotoService;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.PathResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Controller
|
||||
public class PhotoController {
|
||||
|
||||
private final PhotoService photoService;
|
||||
|
||||
public PhotoController(PhotoService photoService) {
|
||||
this.photoService = photoService;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String home() {
|
||||
return "home"; // créer une page home.html simple
|
||||
}
|
||||
|
||||
@GetMapping("/upload")
|
||||
public String uploadForm() {
|
||||
return "upload";
|
||||
}
|
||||
|
||||
@PostMapping("/upload")
|
||||
public String doUpload(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(value="visibilite", defaultValue = "PRIVATE") String visibilite,
|
||||
@AuthenticationPrincipal CustomUserDetails user,
|
||||
Model model) {
|
||||
try {
|
||||
Photo p = photoService.store(file, visibilite, user.getUsername());
|
||||
model.addAttribute("message", "Upload OK : " + p.getId());
|
||||
return "redirect:/"; // ou page d'affichage
|
||||
} catch (Exception e) {
|
||||
model.addAttribute("error", e.getMessage());
|
||||
return "upload";
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
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);
|
||||
} catch (Exception ignored) {}
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType(contentType != null ? contentType : "application/octet-stream"))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + p.getFileName().toString() + "\"")
|
||||
.body(r);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user