Storage System (#92)
All checks were successful
Build and test backend / Build-backend (push) Successful in 2m17s
Build and test backend / Test-backend (push) Successful in 1m19s
deploy to production / deploy-frontend (push) Successful in 25s
deploy to production / deploy-backend (push) Successful in 2m20s
Build and test FrontEnd / Build-frontend (push) Successful in 25s

First idea of the storage system

Reviewed-on: #92
Co-authored-by: Bartha Maxime <231026@umons.ac.be>
Co-committed-by: Bartha Maxime <231026@umons.ac.be>
This commit is contained in:
Bartha Maxime 2024-03-11 17:52:08 +01:00 committed by Maxime
parent ce2efb61c8
commit 5325d6e3ae
7 changed files with 172 additions and 2 deletions

View File

@ -0,0 +1,32 @@
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;
import ovh.herisson.Clyde.Tables.FileType;
@RestController
@CrossOrigin(origins = "http://localhost:5173")
public class StorageController {
private final StorageService storageServ;
public StorageController(StorageService storageServ){
this.storageServ= storageServ;
}
@PostMapping("/upload/{fileType}")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) {
String path = storageServ.store(file,fileType);
if (path == null) return new ResponseEntity<>("issue with the file storage", HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(path, HttpStatus.OK);
}
}

View File

@ -31,7 +31,7 @@ public class UserController {
return new ResponseEntity<>(user, HttpStatus.OK); return new ResponseEntity<>(user, HttpStatus.OK);
} }
@PostMapping("/user") @PostMapping("/user") //todo check role
public ResponseEntity<String> postUser(@RequestBody User user){ public ResponseEntity<String> postUser(@RequestBody User user){
userService.save(user); userService.save(user);
return new ResponseEntity<>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED); return new ResponseEntity<>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED);

View File

@ -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<StorageFile,Long> {
}

View File

@ -0,0 +1,56 @@
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.FileType;
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, 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));
Files.copy(file.getInputStream(), destinationFile,StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException e) {
return null;
}
String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
.normalize().toString();
fileRepo.save(new StorageFile(file.getName(),url, fileType));
return url;
}
}

View File

@ -0,0 +1,8 @@
package ovh.herisson.Clyde.Tables;
public enum FileType {
ProfilePicture,
EducationCertificate
}

View File

@ -0,0 +1,63 @@
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;
private FileType fileType;
public StorageFile(String name, String url, FileType fileType){
this.name = name;
this.url = url;
this.fileType = fileType;
}
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;
}
public FileType getFileType() {
return fileType;
}
public void setFileType(FileType fileType) {
this.fileType = fileType;
}
}

View File

@ -23,7 +23,9 @@ public class User {
private String profilePictureUrl; private String profilePictureUrl;
private ovh.herisson.Clyde.Tables.Role role; private ovh.herisson.Clyde.Tables.Role role;
private String password; 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.lastName = lastName;
this.firstName = firstName; this.firstName = firstName;
this.email = email; this.email = email;