Storage System #92
Binary file not shown.
@ -7,8 +7,10 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import ovh.herisson.Clyde.Services.StorageService;
|
import ovh.herisson.Clyde.Services.StorageService;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
import ovh.herisson.Clyde.Tables.FileType;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@CrossOrigin(origins = "http://localhost:5173")
|
||||||
public class StorageController {
|
public class StorageController {
|
||||||
|
|
||||||
private final StorageService storageServ;
|
private final StorageService storageServ;
|
||||||
@ -18,10 +20,10 @@ public class StorageController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/upload")
|
@PostMapping("/upload/{fileType}")
|
||||||
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
|
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);
|
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.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import ovh.herisson.Clyde.Repositories.FileRepository;
|
import ovh.herisson.Clyde.Repositories.FileRepository;
|
||||||
|
import ovh.herisson.Clyde.Tables.FileType;
|
||||||
import ovh.herisson.Clyde.Tables.StorageFile;
|
import ovh.herisson.Clyde.Tables.StorageFile;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
@ -26,53 +27,30 @@ public class StorageService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String store(MultipartFile file) {
|
public String store(MultipartFile file, FileType fileType) {
|
||||||
UUID uuid = UUID.randomUUID();
|
|
||||||
String stringUuid = uuid.toString() + "." + file.getContentType().split("/",2)[1];
|
|
||||||
|
|
||||||
|
if (file.getOriginalFilename().isEmpty()){return null;}
|
||||||
|
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
String stringUuid = uuid + "." + file.getOriginalFilename().split("\\.",2)[1];
|
||||||
try {
|
try {
|
||||||
if (file.isEmpty()) {
|
if (file.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Path destinationFile = this.rootLocation.resolve(Paths.get(stringUuid)).toAbsolutePath();
|
Path destinationFile = this.rootLocation.resolve(Paths.get(stringUuid));
|
||||||
|
|
||||||
if (!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())) {
|
|
||||||
return null;}
|
|
||||||
|
|
||||||
Files.copy(file.getInputStream(), destinationFile,StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(file.getInputStream(), destinationFile,StandardCopyOption.REPLACE_EXISTING);
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
|
String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
|
||||||
.normalize().toAbsolutePath().toString();
|
.normalize().toString();
|
||||||
System.out.println(url);
|
|
||||||
fileRepo.save(new StorageFile(file.getName(),url));
|
fileRepo.save(new StorageFile(file.getName(),url, fileType));
|
||||||
|
|
||||||
return 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package ovh.herisson.Clyde.Tables;
|
||||||
|
|
||||||
|
public enum FileType {
|
||||||
|
|
||||||
|
ProfilePicture,
|
||||||
|
|
||||||
|
EducationCertificate
|
||||||
|
}
|
@ -17,9 +17,13 @@ public class StorageFile {
|
|||||||
|
|
||||||
private String url;
|
private String url;
|
||||||
|
|
||||||
public StorageFile(String name, String url){
|
private FileType fileType;
|
||||||
|
|
||||||
|
|
||||||
|
public StorageFile(String name, String url, FileType fileType){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
this.fileType = fileType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StorageFile(){}
|
public StorageFile(){}
|
||||||
@ -48,4 +52,12 @@ public class StorageFile {
|
|||||||
public void setUrl(String url) {
|
public void setUrl(String url) {
|
||||||
this.url = 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 ovh.herisson.Clyde.Tables.Role role;
|
||||||
private String password;
|
private String password;
|
||||||
public User(String lastName, String firstName, String email, String address,
|
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.lastName = lastName;
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
|
Loading…
Reference in New Issue
Block a user