correction anthoniesque
This commit is contained in:
parent
3a39cbee11
commit
7766299eb7
Binary file not shown.
Before Width: | Height: | Size: 11 KiB |
@ -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<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
|
||||
@PostMapping("/upload/{fileType}")
|
||||
public ResponseEntity<String> 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);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
package ovh.herisson.Clyde.Tables;
|
||||
|
||||
public enum FileType {
|
||||
|
||||
ProfilePicture,
|
||||
|
||||
EducationCertificate
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user