Create exception for file deletion.
Add user/inscriptionrequest connection to StorageFile entity. Create a prototype of the delete for file function
This commit is contained in:
parent
d4c48ee9f1
commit
ab91a39a63
@ -23,7 +23,7 @@ public class StorageController {
|
||||
@PostMapping("/upload/{fileType}")
|
||||
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) {
|
||||
|
||||
String path = storageServ.store(file,fileType);
|
||||
String path = storageServ.store(file,fileType, null, null);
|
||||
|
||||
if (path == null) return new ResponseEntity<>("issue with the file storage", HttpStatus.BAD_REQUEST);
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
package ovh.herisson.Clyde.Exceptions;
|
||||
|
||||
public class CouldntDeleteFileException extends Exception{
|
||||
}
|
@ -3,11 +3,14 @@ package ovh.herisson.Clyde.Services;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import ovh.herisson.Clyde.Exceptions.CouldntDeleteFileException;
|
||||
import ovh.herisson.Clyde.Repositories.FileRepository;
|
||||
import ovh.herisson.Clyde.Tables.FileType;
|
||||
import ovh.herisson.Clyde.Tables.StorageFile;
|
||||
import ovh.herisson.Clyde.Tables.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@ -27,7 +30,7 @@ public class StorageService {
|
||||
}
|
||||
|
||||
|
||||
public String store(MultipartFile file, FileType fileType) {
|
||||
public String store(MultipartFile file, FileType fileType, User user, InscriptionRequest request) {
|
||||
|
||||
if (file.getOriginalFilename().isEmpty()){return null;}
|
||||
|
||||
@ -49,8 +52,20 @@ public class StorageService {
|
||||
String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
|
||||
.normalize().toString();
|
||||
|
||||
fileRepo.save(new StorageFile(file.getName(),url, fileType));
|
||||
fileRepo.save(new StorageFile(file.getName(),url, fileType, user, request));
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
public void delete(StorageFile file) throws CouldntDeleteFileException {
|
||||
File f = new File(file.getUrl());
|
||||
//Delete le fichier
|
||||
try{
|
||||
f.delete();
|
||||
} catch (Exception e) {
|
||||
throw new CouldntDeleteFileException();
|
||||
}
|
||||
//Delete l'entité
|
||||
fileRepo.delete(file);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,10 @@ import jakarta.persistence.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
@Entity
|
||||
public class InscriptionRequest {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
private String firstName;
|
||||
|
@ -20,7 +20,7 @@ public class ReinscriptionRequest {
|
||||
|
||||
//Permet de différencier les demandes de changement et une réinscription dans le même cursus
|
||||
//Pour la réinscription on va le mettre a 0
|
||||
private boolean type;
|
||||
private boolean type = false;
|
||||
|
||||
public ReinscriptionRequest(){}
|
||||
|
||||
|
@ -1,9 +1,6 @@
|
||||
package ovh.herisson.Clyde.Tables;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
|
||||
@Entity
|
||||
@ -20,10 +17,21 @@ public class StorageFile {
|
||||
private FileType fileType;
|
||||
|
||||
|
||||
public StorageFile(String name, String url, FileType fileType){
|
||||
//Pour lier un user ou une demande d'inscription au fichier
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "user")
|
||||
private User user;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "inscriptionrequest")
|
||||
private InscriptionRequest request;
|
||||
|
||||
public StorageFile(String name, String url, FileType fileType, User user, InscriptionRequest request){
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
this.fileType = fileType;
|
||||
this.user = user;
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public StorageFile(){}
|
||||
@ -60,4 +68,20 @@ public class StorageFile {
|
||||
public void setFileType(FileType fileType) {
|
||||
this.fileType = fileType;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setRequest(InscriptionRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public InscriptionRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user