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 ++++++++++--- src/main/java/com/juick/www/WebApp.java | 3 --- 2 files changed, 10 insertions(+), 6 deletions(-) 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 { diff --git a/src/main/java/com/juick/www/WebApp.java b/src/main/java/com/juick/www/WebApp.java index 69de1507..cebedb9b 100644 --- a/src/main/java/com/juick/www/WebApp.java +++ b/src/main/java/com/juick/www/WebApp.java @@ -33,7 +33,6 @@ import javax.inject.Inject; import com.juick.model.Message; import com.juick.model.Tag; import com.juick.model.User; -import com.juick.service.StorageService; import com.juick.service.TagService; import com.mitchellbosecke.pebble.PebbleEngine; import com.mitchellbosecke.pebble.template.PebbleTemplate; @@ -50,8 +49,6 @@ import org.springframework.web.util.UriComponentsBuilder; */ @Component public class WebApp { - @Inject - private StorageService storageService; @Inject private TagService tagService; @Inject -- cgit v1.2.3