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 /src/main/java/com/juick/service/FileSystemStorageService.java | |
parent | 40b56cd75189d0b78d93242eb808be89ebca9b6e (diff) |
Avatar upload improvements
Diffstat (limited to 'src/main/java/com/juick/service/FileSystemStorageService.java')
-rw-r--r-- | src/main/java/com/juick/service/FileSystemStorageService.java | 13 |
1 files changed, 10 insertions, 3 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 { |