diff options
author | Vitaly Takmazov | 2021-10-25 07:37:19 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2021-10-25 07:50:44 +0300 |
commit | 3fde3b23af118315b3bebce6e7f4a9e4fad136f9 (patch) | |
tree | b507690e250e555cf5fa0418d5bac59520b0bc91 | |
parent | 40b56cd75189d0b78d93242eb808be89ebca9b6e (diff) |
Avatar upload improvements
-rw-r--r-- | src/main/java/com/juick/service/FileSystemStorageService.java | 13 | ||||
-rw-r--r-- | 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; @@ -51,8 +50,6 @@ import org.springframework.web.util.UriComponentsBuilder; @Component public class WebApp { @Inject - private StorageService storageService; - @Inject private TagService tagService; @Inject private ResourceUrlProvider resourceUrlProvider; |