Storage System
All checks were successful
Build and test backend / Build-backend (pull_request) Successful in 2m4s
Build and test backend / Test-backend (pull_request) Successful in 2m0s
Build and test FrontEnd / Build-frontend (pull_request) Successful in 25s

This commit is contained in:
Bartha Maxime 2024-03-10 22:23:20 +01:00
parent 8fdfb470cb
commit 3a39cbee11
7 changed files with 171 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -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<String> 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);
}
}

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>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED); return new ResponseEntity<String>(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,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;
}
}
}

View File

@ -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;
}
}

View File

@ -23,7 +23,8 @@ 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;