From ab91a39a63ce37dcc9985423aa6b6be7ffa19b50 Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Mon, 11 Mar 2024 20:23:45 +0100 Subject: [PATCH 1/5] Create exception for file deletion. Add user/inscriptionrequest connection to StorageFile entity. Create a prototype of the delete for file function --- .../Clyde/EndPoints/StorageController.java | 2 +- .../CouldntDeleteFileException.java | 4 +++ .../Clyde/Services/StorageService.java | 23 ++++++++++--- .../Clyde/Tables/InscriptionRequest.java | 3 ++ .../Clyde/Tables/ReinscriptionRequest.java | 2 +- .../herisson/Clyde/Tables/StorageFile.java | 34 ++++++++++++++++--- 6 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java index 2a36657..be58f17 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java @@ -23,7 +23,7 @@ public class StorageController { @PostMapping("/upload/{fileType}") public ResponseEntity 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); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java b/backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java new file mode 100644 index 0000000..ce0d637 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java @@ -0,0 +1,4 @@ +package ovh.herisson.Clyde.Exceptions; + +public class CouldntDeleteFileException extends Exception{ +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index 77dff70..eeea9e3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -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); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java index 2e5bf0d..fa853b1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/InscriptionRequest.java @@ -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; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java index 8a56f88..0a4db64 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java @@ -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(){} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java index a96d0ec..4124231 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java @@ -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; + } } From 8fbfb3695811756250bceed5efc3e5ec66995d27 Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Tue, 12 Mar 2024 10:48:13 +0100 Subject: [PATCH 2/5] Remove the bad link between users and file. Add a delete function for storageFile entities and clean things. --- .../Clyde/EndPoints/MockController.java | 12 +++----- .../Clyde/EndPoints/StorageController.java | 2 +- .../Clyde/Services/StorageService.java | 4 +-- .../herisson/Clyde/Tables/StorageFile.java | 29 +------------------ 4 files changed, 8 insertions(+), 39 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index b140057..787a534 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -1,20 +1,14 @@ package ovh.herisson.Clyde.EndPoints; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Services.TokenService; -import ovh.herisson.Clyde.Tables.Role; -import ovh.herisson.Clyde.Tables.Token; -import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.*; import java.util.ArrayList; import java.util.Arrays; -import java.util.Calendar; import java.util.Date; @RestController @@ -26,6 +20,7 @@ public class MockController { public final UserRepository userRepo; public final TokenRepository tokenRepo; public final TokenService tokenService; + ArrayList mockUsers; @@ -62,3 +57,4 @@ public class MockController { userRepo.deleteAll(mockUsers); } } + diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java index be58f17..2a36657 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java @@ -23,7 +23,7 @@ public class StorageController { @PostMapping("/upload/{fileType}") public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) { - String path = storageServ.store(file,fileType, null, null); + String path = storageServ.store(file,fileType); if (path == null) return new ResponseEntity<>("issue with the file storage", HttpStatus.BAD_REQUEST); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index eeea9e3..36e39ca 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -30,7 +30,7 @@ public class StorageService { } - public String store(MultipartFile file, FileType fileType, User user, InscriptionRequest request) { + public String store(MultipartFile file, FileType fileType) { if (file.getOriginalFilename().isEmpty()){return null;} @@ -52,7 +52,7 @@ public class StorageService { String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid))) .normalize().toString(); - fileRepo.save(new StorageFile(file.getName(),url, fileType, user, request)); + fileRepo.save(new StorageFile(file.getName(),url, fileType)); return url; } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java index 4124231..afa7985 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java @@ -16,22 +16,10 @@ public class StorageFile { private 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){ + public StorageFile(String name, String url, FileType fileType){ this.name = name; this.url = url; this.fileType = fileType; - this.user = user; - this.request = request; } public StorageFile(){} @@ -69,19 +57,4 @@ public class StorageFile { 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; - } } From 66282bce9ff0e386228689a88db78efc34e950c1 Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Tue, 12 Mar 2024 17:53:41 +0100 Subject: [PATCH 3/5] Create new constructor for ReinscriptionRequest so you don't have to worry about the type of the request --- .../ovh/herisson/Clyde/Tables/ReinscriptionRequest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java index 0a4db64..c7c5569 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ReinscriptionRequest.java @@ -31,6 +31,12 @@ public class ReinscriptionRequest { this.type = type; } + public ReinscriptionRequest(User user, Cursus newCursus, RequestState state){ + this.user = user; + this.newCursus = newCursus; + this.state = state; + } + public int getId() { return id; } From 2a58c335f2fa5893d33dae15ae91c7dca046ce35 Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Wed, 13 Mar 2024 15:48:28 +0100 Subject: [PATCH 4/5] Apply les trucs de l'exception --- backend/build.gradle.kts | 1 + .../Clyde/Exceptions/CouldntDeleteFileException.java | 4 ---- .../java/ovh/herisson/Clyde/Services/StorageService.java | 7 ++----- 3 files changed, 3 insertions(+), 9 deletions(-) delete mode 100644 backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index f105250..6557b82 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -41,3 +41,4 @@ tasks.register("run") { tasks.withType { useJUnitPlatform() } + diff --git a/backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java b/backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java deleted file mode 100644 index ce0d637..0000000 --- a/backend/src/main/java/ovh/herisson/Clyde/Exceptions/CouldntDeleteFileException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ovh.herisson.Clyde.Exceptions; - -public class CouldntDeleteFileException extends Exception{ -} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index 36e39ca..cb3ceb0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -1,15 +1,12 @@ 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.*; import java.io.File; import java.io.IOException; -import org.springframework.core.io.Resource; import java.nio.file.Files; import java.nio.file.Path; @@ -57,13 +54,13 @@ public class StorageService { return url; } - public void delete(StorageFile file) throws CouldntDeleteFileException { + public void delete(StorageFile file) throws SecurityException { File f = new File(file.getUrl()); //Delete le fichier try{ f.delete(); } catch (Exception e) { - throw new CouldntDeleteFileException(); + throw new SecurityException(); } //Delete l'entité fileRepo.delete(file); From 5c7147bb176495096ded2014d0165f7baa59ce86 Mon Sep 17 00:00:00 2001 From: Debucquoy Anthony Date: Wed, 13 Mar 2024 18:05:35 +0100 Subject: [PATCH 5/5] fixing the double throw before the merge Signed-off-by: Debucquoy Anthony --- .../java/ovh/herisson/Clyde/Services/StorageService.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index cb3ceb0..c73dd0b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -56,12 +56,8 @@ public class StorageService { public void delete(StorageFile file) throws SecurityException { File f = new File(file.getUrl()); - //Delete le fichier - try{ - f.delete(); - } catch (Exception e) { - throw new SecurityException(); - } + f.delete(); + //Delete l'entité fileRepo.delete(file); }