Search in sources :

Example 11 with File

use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method downloadJpeg.

@Test
public void downloadJpeg() throws IOException {
    String imgName = "test.jpeg";
    Path pathTestPng = UPLOAD_PATH.resolve(imgName);
    // file name, where we locate our test.png
    final String fileUuid = "2e20bc55-82df-4978-8e6e-d1d86def7039";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File fileEntity = new File();
    fileEntity.setId(UUID.fromString(fileUuid));
    Resource resultResource = fileService.load(fileEntity);
    assertEquals(fileUuid, resultResource.getFilename());
    Resource perfectResource = new UrlResource(fileUuidPath.toUri());
    assertEquals(perfectResource, resultResource);
}
Also used : Path(java.nio.file.Path) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 12 with File

use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method saveJpegClient.

@Test
public void saveJpegClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("test", "jpeg");
    Long fileSize = file.getSize();
    log.info("FILE SIZE before uploading: " + fileSize + " FILE NAME: " + file.getName());
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    FileLinkDTO fileLinkDTO = fileService.save(file, rajeshClient);
    Path uploadedFile = getUploadedFilePath(fileLinkDTO.getFileUuid());
    assertTrue(Files.exists(uploadedFile));
    Long uploadedFileSize = Files.size(uploadedFile);
    log.info("FILE SIZE after uploading: " + uploadedFileSize + " FILE UUID: " + fileLinkDTO.getFileUuid());
    assertEquals(fileSize, uploadedFileSize);
    verify(fileRepoMock, times(1)).save(any(File.class));
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 13 with File

use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method deleteImgOwner.

@Test
public void deleteImgOwner() throws IOException {
    // prepare
    Long ownerId = 1L;
    User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
    String imgName = "test.jpeg";
    Path pathTestPng = UPLOAD_PATH.resolve(imgName);
    // file name, where we locate our test.png
    final String fileUuid = "24816b73-f989-4c72-89b3-67450141dafc";
    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);
    fileService.delete(fileEntity, howardOwner);
    assertFalse(Files.exists(fileUuidPath));
    verify(fileRepoMock, times(1)).delete(fileEntity);
}
Also used : Path(java.nio.file.Path) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 14 with File

use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method replaceImgAdmin.

@Test
public void replaceImgAdmin() throws IOException {
    // prepare
    Long userId = 1L;
    User sheldonAdmin = UserUtils.adminSheldonCooper(userId);
    String oldImgName = "test.png";
    int oldImgHeight = pictureNamesAndSize.get("test")[1];
    Path pathTestPng = UPLOAD_PATH.resolve(oldImgName);
    final String fileUuid = "7cdb597b-303e-41d2-8bcc-802ccaaa83a5";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    Long oldFileSize = Files.size(fileUuidPath);
    MultipartFile newFile = getImgWithType("testLargeHeight", "jpeg");
    File oldFileEntity = new File(UUID.fromString(fileUuid), FileType.PNG, oldImgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), sheldonAdmin);
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    FileLinkDTO fileLinkDTO = fileService.replace(newFile, oldFileEntity, sheldonAdmin);
    assertEquals(fileUuid, fileLinkDTO.getFileUuid());
    assertTrue(oldFileSize < Files.size(fileUuidPath));
    BufferedImage bufferedImage = ImageIO.read(fileUuidPath.toFile());
    int newImgHeight = bufferedImage.getHeight();
    assertTrue(oldImgHeight < newImgHeight);
    verify(fileRepoMock, times(1)).save(any(File.class));
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 15 with File

use of com.ncedu.fooddelivery.api.v1.entities.File in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceImpl method replace.

@Override
public FileLinkDTO replace(MultipartFile newFile, File oldFile, User authedUser) {
    boolean isAdminOrOwner = checkAdminOrOwner(oldFile, authedUser);
    if (!isAdminOrOwner) {
        log.error(authedUser.getEmail() + " not Admin and not Owner of the file " + oldFile.getId().toString());
        throw new CustomAccessDeniedException();
    }
    try {
        String originalFileName = newFile.getOriginalFilename();
        String newFileName = getFileNameWithoutExt(originalFileName);
        FileType fileType = getFileType(originalFileName);
        Long fileSize = newFile.getSize();
        Path fullPathToFile = createFullPathToFile(oldFile.getId());
        if (authedUser.getRole() == Role.CLIENT || authedUser.getRole() == Role.COURIER) {
            InputStream is = newFile.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(newFile.getInputStream(), fullPathToFile, StandardCopyOption.REPLACE_EXISTING);
        }
        oldFile.setName(newFileName);
        oldFile.setType(fileType);
        oldFile.setSize(fileSize);
        oldFile.setUploadDate(Timestamp.valueOf(LocalDateTime.now()));
        fileRepo.save(oldFile);
        String fileLink = createFileLink(oldFile.getId());
        return new FileLinkDTO(fileLink, oldFile.getId().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) FileType(com.ncedu.fooddelivery.api.v1.entities.FileType) InputStream(java.io.InputStream) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) 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)

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