Search in sources :

Example 1 with FileType

use of com.ncedu.fooddelivery.api.v1.entities.FileType 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)

Example 2 with FileType

use of com.ncedu.fooddelivery.api.v1.entities.FileType 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)

Aggregations

FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)2 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 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 Path (java.nio.file.Path)2 File (com.ncedu.fooddelivery.api.v1.entities.File)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1