From 3fde3b23af118315b3bebce6e7f4a9e4fad136f9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 25 Oct 2021 07:37:19 +0300 Subject: Avatar upload improvements --- .../java/com/juick/service/FileSystemStorageService.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/main/java/com/juick/service') diff --git a/src/main/java/com/juick/service/FileSystemStorageService.java b/src/main/java/com/juick/service/FileSystemStorageService.java index f7420f84..5b462bd7 100644 --- a/src/main/java/com/juick/service/FileSystemStorageService.java +++ b/src/main/java/com/juick/service/FileSystemStorageService.java @@ -20,6 +20,7 @@ package com.juick.service; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -220,11 +221,17 @@ public class FileSystemStorageService implements StorageService { String ext = FilenameUtils.getExtension(tempFilename); String originalName = getAvatarFileName(user, ext); Path originalPath = Paths.get(avatarOriginalDir, originalName); - Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING); + Path tmpPath = Paths.get(tmpDir, tempFilename); + CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE }; + Files.move(tmpPath, originalPath,copyOptions); BufferedImage originalImage = ImageIO.read(originalPath.toFile()); String targetExt = "png"; - ImageIO.write(Scalr.resize(originalImage, 96), targetExt, getAvatarPath(user).toFile()); - ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(avatarSmallDir, getAvatarFileName(user, targetExt)).toFile()); + if (ImageIO.write(Scalr.resize(originalImage, 96), targetExt, tmpPath.toFile())) { + Files.move(tmpPath, getAvatarPath(user), copyOptions); + }; + if (ImageIO.write(Scalr.resize(originalImage, 32), targetExt, tmpPath.toFile())) { + Files.move(tmpPath, Paths.get(avatarSmallDir, getAvatarFileName(user, targetExt)), copyOptions); + }; } public Attachment getAttachment(File imgFile) throws IOException { -- cgit v1.2.3