Search in sources :

Example 6 with FileLinkDTO

use of com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method replaceJpgToLargeHeightClient.

@Test
public void replaceJpgToLargeHeightClient() throws IOException {
    // prepare
    Long userId = 1L;
    User pennyClient = UserUtils.clientPennyTeller(userId);
    String oldImgName = "test.jpeg";
    Path pathTestJpeg = UPLOAD_PATH.resolve(oldImgName);
    final String fileUuid = "e9365637-bb5b-4792-9f25-ad36776fccf3";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestJpeg, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    Long oldFileSize = Files.size(fileUuidPath);
    File oldFileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, oldImgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), pennyClient);
    MultipartFile newFile = getImgWithType("testLargeHeight", "jpeg");
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    FileLinkDTO fileLinkDTO = fileService.replace(newFile, oldFileEntity, pennyClient);
    assertEquals(fileUuid, fileLinkDTO.getFileUuid());
    assertTrue(oldFileSize < Files.size(fileUuidPath));
    BufferedImage bufferedImage = ImageIO.read(fileUuidPath.toFile());
    int newImgHeight = bufferedImage.getHeight();
    assertEquals(CLIENT_IMAGE_HEIGHT, 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 7 with FileLinkDTO

use of com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method saveJpegLargeHeightAdmin.

@Test
public void saveJpegLargeHeightAdmin() throws IOException {
    // prepare
    Long userId = 1L;
    User sheldonAdmin = UserUtils.adminSheldonCooper(userId);
    MultipartFile file = getImgWithType("testLargeHeight", "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, sheldonAdmin);
    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 8 with FileLinkDTO

use of com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO 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 9 with FileLinkDTO

use of com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO 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 10 with FileLinkDTO

use of com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO 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

FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)15 Path (java.nio.file.Path)14 File (com.ncedu.fooddelivery.api.v1.entities.File)13 MultipartFile (org.springframework.web.multipart.MultipartFile)13 User (com.ncedu.fooddelivery.api.v1.entities.User)12 Test (org.junit.jupiter.api.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)12 BufferedImage (java.awt.image.BufferedImage)10 FileType (com.ncedu.fooddelivery.api.v1.entities.FileType)2 BadFileExtensionException (com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException)2 FileDeleteException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException)2 FileStorageException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException)2 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2