use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceImpl method load.
@Override
public Resource load(File file) {
try {
Path fullFilePath = createFullPathToFile(file.getId());
Resource resource = new UrlResource(fullFilePath.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
}
throw new NotFoundEx(file.getId().toString());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new NotFoundEx(file.getId().toString());
}
}
use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceImpl method delete.
@Override
public void delete(File file, User authedUser) {
boolean isAdminOrOwner = checkAdminOrOwner(file, authedUser);
if (!isAdminOrOwner) {
log.error(authedUser.getEmail() + " not Admin and not Owner of the file " + file.getId().toString());
throw new CustomAccessDeniedException();
}
try {
fileRepo.delete(file);
Path fullFilePath = createFullPathToFile(file.getId());
Files.deleteIfExists(fullFilePath);
Path fileParentDirPath = fullFilePath.getParent();
log.debug("PARENT DIR PATH: " + fileParentDirPath + "\n");
boolean isParentDirEmpty = checkParentDirEmpty(fileParentDirPath);
if (isParentDirEmpty) {
Files.delete(fileParentDirPath);
}
} catch (IOException e) {
throw new FileDeleteException();
}
}
use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.
the class FileController method uploadFile.
@PostMapping("/api/v1/file")
public FileLinkDTO uploadFile(@RequestParam("file") MultipartFile file, @AuthenticationPrincipal User authedUser) {
FileLinkDTO fileLinkDTO = fileService.save(file, authedUser);
log.debug("Created file link: " + fileLinkDTO.getLink());
return fileLinkDTO;
}
use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceImpl method save.
@Override
public FileLinkDTO save(MultipartFile file, User owner) {
try {
String originalFileName = file.getOriginalFilename();
String fileName = getFileNameWithoutExt(originalFileName);
FileType fileType = getFileType(originalFileName);
Long fileSize = file.getSize();
UUID fileUuid = UUID.randomUUID();
Path fullPathToFile = createFullPathToFile(fileUuid);
if (owner.getRole() == Role.CLIENT || owner.getRole() == Role.COURIER) {
InputStream is = file.getInputStream();
BufferedImage bufferedImage = ImageIO.read(is);
boolean isExceedImageSize = checkExceedImageSize(bufferedImage);
if (isExceedImageSize) {
bufferedImage = resizeImage(bufferedImage);
}
if (fileType == FileType.PNG) {
bufferedImage = convertPNGtoJPG(bufferedImage);
}
ImageIO.write(bufferedImage, "jpg", fullPathToFile.toFile());
fileSize = Files.size(fullPathToFile);
fileType = FileType.JPEG;
} else {
Files.copy(file.getInputStream(), fullPathToFile, StandardCopyOption.REPLACE_EXISTING);
}
File fileEntity = new File(fileUuid, fileType, fileName, fileSize, Timestamp.valueOf(LocalDateTime.now()), owner);
fileRepo.save(fileEntity);
String fileLink = createFileLink(fileUuid);
return new FileLinkDTO(fileLink, fileUuid.toString());
} catch (BadFileExtensionException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new FileStorageException();
}
}
use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceTest method deleteImgNotAdminNotOwner.
@Test
public void deleteImgNotAdminNotOwner() throws IOException {
// prepare
Long ownerId = 1L;
User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
Long notOwnerId = 2L;
User leonardNotOwner = UserUtils.moderatorLeonardHofstadter(notOwnerId);
String imgName = "test.jpeg";
Path pathTestPng = UPLOAD_PATH.resolve(imgName);
// file name, where we locate our test.png
final String fileUuid = "55420882-1e23-4559-b84e-03c3f4d597af";
Path fileUuidPath = createFileUuidPath(fileUuid);
Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
File fileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, imgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), howardOwner);
doNothing().when(fileRepoMock).delete(fileEntity);
Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
fileService.delete(fileEntity, leonardNotOwner);
});
String perfectMessage = new CustomAccessDeniedException().getMessage();
String resultMessage = exception.getMessage();
assertEquals(perfectMessage, resultMessage);
verify(fileRepoMock, never()).delete(fileEntity);
}
Aggregations