Create exception for file deletion.

Add user/inscriptionrequest connection to StorageFile entity.
Create a prototype of the delete for file function
This commit is contained in:
2024-03-11 20:23:45 +01:00
parent d4c48ee9f1
commit ab91a39a63
6 changed files with 57 additions and 11 deletions

View File

@ -3,11 +3,14 @@ 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.Exceptions.CouldntDeleteFileException;
import ovh.herisson.Clyde.Repositories.FileRepository;
import ovh.herisson.Clyde.Tables.FileType;
import ovh.herisson.Clyde.Tables.StorageFile;
import ovh.herisson.Clyde.Tables.*;
import java.io.File;
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;
@ -27,7 +30,7 @@ public class StorageService {
}
public String store(MultipartFile file, FileType fileType) {
public String store(MultipartFile file, FileType fileType, User user, InscriptionRequest request) {
if (file.getOriginalFilename().isEmpty()){return null;}
@ -49,8 +52,20 @@ public class StorageService {
String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
.normalize().toString();
fileRepo.save(new StorageFile(file.getName(),url, fileType));
fileRepo.save(new StorageFile(file.getName(),url, fileType, user, request));
return url;
}
public void delete(StorageFile file) throws CouldntDeleteFileException {
File f = new File(file.getUrl());
//Delete le fichier
try{
f.delete();
} catch (Exception e) {
throw new CouldntDeleteFileException();
}
//Delete l'entité
fileRepo.delete(file);
}
}