Search in sources :

Example 11 with FileLinkDTO

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

@Test
public void saveJpegLargeWidthClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("testLargeWidth", "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);
    verify(fileRepoMock, times(1)).save(any(File.class));
    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());
    // uploaded size less than income because convert from jpeg to jpeg with small resolution
    assertTrue(fileSize > uploadedFileSize);
    BufferedImage bufferedImage = ImageIO.read(uploadedFile.toFile());
    assertEquals(CLIENT_IMAGE_WIDTH, bufferedImage.getWidth());
}
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 14 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 savePngLargeHeightClient.

@Test
public void savePngLargeHeightClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("testLargeHeight", "png");
    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);
    verify(fileRepoMock, times(1)).save(any(File.class));
    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());
    // uploaded size more than income because convert from png to jpeg
    assertTrue(fileSize < uploadedFileSize);
    BufferedImage bufferedImage = ImageIO.read(uploadedFile.toFile());
    assertEquals(CLIENT_IMAGE_HEIGHT, bufferedImage.getHeight());
}
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 FileLinkDTO

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

the class FileServiceTest method saveJpegLargeHeightClient.

@Test
public void saveJpegLargeHeightClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(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, rajeshClient);
    verify(fileRepoMock, times(1)).save(any(File.class));
    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());
    // uploaded size less than income because convert from jpeg to jpeg with small resolution
    assertTrue(fileSize > uploadedFileSize);
    BufferedImage bufferedImage = ImageIO.read(uploadedFile.toFile());
    assertEquals(CLIENT_IMAGE_HEIGHT, bufferedImage.getHeight());
}
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)

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