diff --git a/backend/cdn/b97fdd51-c4a7-4c13-b70c-4b91eb0b3179.png b/backend/cdn/b97fdd51-c4a7-4c13-b70c-4b91eb0b3179.png new file mode 100644 index 0000000..0ad7d03 Binary files /dev/null and b/backend/cdn/b97fdd51-c4a7-4c13-b70c-4b91eb0b3179.png 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 new file mode 100644 index 0000000..21fabcc --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/StorageController.java @@ -0,0 +1,30 @@ +package ovh.herisson.Clyde.EndPoints; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import ovh.herisson.Clyde.Services.StorageService; +import org.springframework.core.io.Resource; + +@RestController +public class StorageController { + + private final StorageService storageServ; + + public StorageController(StorageService storageServ){ + this.storageServ= storageServ; + } + + + @PostMapping("/upload") + public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) { + + String path = storageServ.store(file); + + if (path == null) return new ResponseEntity<>("issue with the file storage", HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(path, HttpStatus.OK); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index ba54926..1656d21 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -31,7 +31,7 @@ public class UserController { return new ResponseEntity<>(user, HttpStatus.OK); } - @PostMapping("/user") + @PostMapping("/user") //todo check role public ResponseEntity postUser(@RequestBody User user){ userService.save(user); return new ResponseEntity(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java new file mode 100644 index 0000000..2240c92 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java @@ -0,0 +1,9 @@ +package ovh.herisson.Clyde.Repositories; + +import org.springframework.data.repository.CrudRepository; + +import ovh.herisson.Clyde.Tables.StorageFile; + + +public interface FileRepository extends CrudRepository { +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java new file mode 100644 index 0000000..1fc9b65 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -0,0 +1,78 @@ +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.Repositories.FileRepository; +import ovh.herisson.Clyde.Tables.StorageFile; +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; +import java.nio.file.StandardCopyOption; +import java.util.Objects; +import java.util.UUID; + +@Service +public class StorageService { + + + private final Path rootLocation = Paths.get("cdn/"); + private final FileRepository fileRepo; + + public StorageService(FileRepository filerepo){ + this.fileRepo = filerepo; + } + + + public String store(MultipartFile file) { + UUID uuid = UUID.randomUUID(); + String stringUuid = uuid.toString() + "." + file.getContentType().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;} + + 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)); + + 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/StorageFile.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java new file mode 100644 index 0000000..97c1d30 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/StorageFile.java @@ -0,0 +1,51 @@ +package ovh.herisson.Clyde.Tables; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + + +@Entity +public class StorageFile { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String name; + + private String url; + + public StorageFile(String name, String url){ + this.name = name; + this.url = url; + } + + public StorageFile(){} + + + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } +} 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 95467a4..0752014 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -23,7 +23,8 @@ public class User { private String profilePictureUrl; 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){ + public User(String lastName, String firstName, String email, String address, + String country, Date birthDate, String profilePictureUrl, Role role, String password){ this.lastName = lastName; this.firstName = firstName; this.email = email;