use of com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException 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();
}
}
use of com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException 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();
}
}
Aggregations