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