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));
}
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));
}
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));
}
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));
}
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();
}
}
Aggregations