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
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:
@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user