diff --git a/backend/cdn/b97fdd51-c4a7-4c13-b70c-4b91eb0b3179.png b/backend/cdn/b97fdd51-c4a7-4c13-b70c-4b91eb0b3179.png deleted file mode 100644 index 0ad7d03..0000000 Binary files a/backend/cdn/b97fdd51-c4a7-4c13-b70c-4b91eb0b3179.png and /dev/null differ 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 21fabcc..2a36657 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java @@ -7,8 +7,10 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import ovh.herisson.Clyde.Services.StorageService; import org.springframework.core.io.Resource; +import ovh.herisson.Clyde.Tables.FileType; @RestController +@CrossOrigin(origins = "http://localhost:5173") public class StorageController { private final StorageService storageServ; @@ -18,10 +20,10 @@ public class StorageController { } - @PostMapping("/upload") - public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) { + @PostMapping("/upload/{fileType}") + public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) { - String path = storageServ.store(file); + 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 1fc9b65..77dff70 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -4,6 +4,7 @@ import org.springframework.core.io.UrlResource; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import ovh.herisson.Clyde.Repositories.FileRepository; +import ovh.herisson.Clyde.Tables.FileType; import ovh.herisson.Clyde.Tables.StorageFile; import java.io.IOException; import org.springframework.core.io.Resource; @@ -26,53 +27,30 @@ public class StorageService { } - public String store(MultipartFile file) { - UUID uuid = UUID.randomUUID(); - String stringUuid = uuid.toString() + "." + file.getContentType().split("/",2)[1]; + public String store(MultipartFile file, FileType fileType) { + if (file.getOriginalFilename().isEmpty()){return null;} + + UUID uuid = UUID.randomUUID(); + String stringUuid = uuid + "." + file.getOriginalFilename().split("\\.",2)[1]; try { if (file.isEmpty()) { return null; } - Path destinationFile = this.rootLocation.resolve(Paths.get(stringUuid)).toAbsolutePath(); - - if (!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())) { - return null;} + Path destinationFile = this.rootLocation.resolve(Paths.get(stringUuid)); Files.copy(file.getInputStream(), destinationFile,StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { - e.printStackTrace(); return null; } String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid))) - .normalize().toAbsolutePath().toString(); - System.out.println(url); - fileRepo.save(new StorageFile(file.getName(),url)); + .normalize().toString(); + + fileRepo.save(new StorageFile(file.getName(),url, fileType)); return url; } - - public Path load(String filename) { - return rootLocation.resolve(filename); - } - - public Resource loadAsResource(String filename) { - try { - Path file = load(filename); - Resource resource = new UrlResource(file.toUri()); - if (resource.exists() || resource.isReadable()) { - return resource; - } - else {return null; - } - } - catch (Exception e) { - e.printStackTrace(); - return null; - } - } - } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/FileType.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/FileType.java new file mode 100644 index 0000000..18ce1be --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/FileType.java @@ -0,0 +1,8 @@ +package ovh.herisson.Clyde.Tables; + +public enum FileType { + + ProfilePicture, + + EducationCertificate +} 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 97c1d30..a96d0ec 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java @@ -17,9 +17,13 @@ public class StorageFile { private String url; - public StorageFile(String name, String url){ + private FileType fileType; + + + public StorageFile(String name, String url, FileType fileType){ this.name = name; this.url = url; + this.fileType = fileType; } public StorageFile(){} @@ -48,4 +52,12 @@ public class StorageFile { public void setUrl(String url) { this.url = url; } + + public FileType getFileType() { + return fileType; + } + + public void setFileType(FileType fileType) { + this.fileType = fileType; + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java index 0752014..101ec9e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -24,7 +24,8 @@ public class User { private ovh.herisson.Clyde.Tables.Role role; private String password; public User(String lastName, String firstName, String email, String address, - String country, Date birthDate, String profilePictureUrl, Role role, String password){ + String country, Date birthDate, String profilePictureUrl, Role role, String password) + { this.lastName = lastName; this.firstName = firstName; this.email = email;