Search in sources :

Example 16 with File

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());
    }
}
Also used : Path(java.nio.file.Path) UrlResource(org.springframework.core.io.UrlResource) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) FileStorageException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException) FileDeleteException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException)

Example 17 with File

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();
    }
}
Also used : Path(java.nio.file.Path) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) IOException(java.io.IOException) FileDeleteException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException)

Example 18 with File

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;
}
Also used : FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)

Example 19 with File

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();
    }
}
Also used : Path(java.nio.file.Path) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) InputStream(java.io.InputStream) FileStorageException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) BufferedImage(java.awt.image.BufferedImage) FileStorageException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException) FileDeleteException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) FileType(com.ncedu.fooddelivery.api.v1.entities.FileType) File(com.ncedu.fooddelivery.api.v1.entities.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 20 with File

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);
}
Also used : Path(java.nio.file.Path) User(com.ncedu.fooddelivery.api.v1.entities.User) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

File (com.ncedu.fooddelivery.api.v1.entities.File)24 Path (java.nio.file.Path)22 MultipartFile (org.springframework.web.multipart.MultipartFile)22 Test (org.junit.jupiter.api.Test)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)21 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)21 User (com.ncedu.fooddelivery.api.v1.entities.User)18 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)15 BufferedImage (java.awt.image.BufferedImage)10 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)8 IOException (java.io.IOException)8 BadFileExtensionException (com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException)7 FileDeleteException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException)4 FileStorageException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException)3 MalformedURLException (java.net.MalformedURLException)3 Resource (org.springframework.core.io.Resource)3 UrlResource (org.springframework.core.io.UrlResource)3 FileType (com.ncedu.fooddelivery.api.v1.entities.FileType)2 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)2 InputStream (java.io.InputStream)2