aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/service')
-rw-r--r--src/main/java/com/juick/service/FileSystemStorageService.java (renamed from src/main/java/com/juick/service/ImagesServiceImpl.java)99
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java6
-rw-r--r--src/main/java/com/juick/service/StorageService.java (renamed from src/main/java/com/juick/service/ImagesService.java)26
3 files changed, 76 insertions, 55 deletions
diff --git a/src/main/java/com/juick/service/ImagesServiceImpl.java b/src/main/java/com/juick/service/FileSystemStorageService.java
index 6687a59b..021e5a9f 100644
--- a/src/main/java/com/juick/service/ImagesServiceImpl.java
+++ b/src/main/java/com/juick/service/FileSystemStorageService.java
@@ -17,9 +17,24 @@
package com.juick.service;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Iterator;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+
import com.juick.model.Attachment;
import com.juick.model.Message;
import com.juick.model.Photo;
+import com.juick.model.User;
+
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.ImageMetadata;
@@ -32,33 +47,29 @@ import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Rotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
-import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.StandardCopyOption;
-import java.util.Iterator;
-
-import javax.imageio.ImageIO;
-import javax.imageio.ImageReader;
-import javax.imageio.stream.ImageInputStream;
-
-public class ImagesServiceImpl implements ImagesService {
+public class FileSystemStorageService implements StorageService {
- private static final Logger logger = LoggerFactory.getLogger(ImagesService.class);
+ private static final Logger logger = LoggerFactory.getLogger(StorageService.class);
+ private final String baseDir;
private final String imgDir;
private final String tmpDir;
+ private final String avatarDir, avatarSmallDir, avatarOriginalDir;
+ private final String fullImageDir, mediumImageDir, smallImageDir, thumbnailImageDir;
- public ImagesServiceImpl(String imgDir, String tmpDir) {
- this.imgDir = imgDir;
+ public FileSystemStorageService(String baseDir, String tmpDir) {
+ this.baseDir = baseDir;
+ this.imgDir = Paths.get(baseDir, "i").toString();
this.tmpDir = tmpDir;
+ this.avatarDir = Paths.get(imgDir, "a").toString();
+ this.avatarOriginalDir = Paths.get(imgDir, "ao").toString();
+ this.avatarSmallDir = Paths.get(imgDir, "as").toString();
+ this.fullImageDir = Paths.get(imgDir, "p").toString();
+ this.mediumImageDir = Paths.get(imgDir, "photos-1024").toString();
+ this.smallImageDir = Paths.get(imgDir, "photos-512").toString();
+ this.thumbnailImageDir = Paths.get(imgDir, "ps").toString();
}
@Override
@@ -82,10 +93,10 @@ public class ImagesServiceImpl implements ImagesService {
if (msg.getRid() > 0) {
imageName = String.format("%s-%s.%s", msg.getMid(), msg.getRid(), msg.getAttachmentType());
}
- File fullImage = Paths.get(imgDir, "p", imageName).toFile();
- File mediumImage = Paths.get(imgDir, "photos-1024", imageName).toFile();
- File smallImage = Paths.get(imgDir, "photos-512", imageName).toFile();
- File thumbnailImage = Paths.get(imgDir, "ps", imageName).toFile();
+ File fullImage = Paths.get(fullImageDir, imageName).toFile();
+ File mediumImage = Paths.get(mediumImageDir, imageName).toFile();
+ File smallImage = Paths.get(smallImageDir, imageName).toFile();
+ File thumbnailImage = Paths.get(thumbnailImageDir, imageName).toFile();
StringBuilder builder = new StringBuilder();
builder.append(baseUrl);
builder.append(msg.getAttachmentType().equals("mp4") ? "video" : "p");
@@ -115,11 +126,6 @@ public class ImagesServiceImpl implements ImagesService {
}
}
- @Override
- public Attachment getImageMetadata(String resourceLocation) throws FileNotFoundException, IOException {
- return getAttachment(ResourceUtils.getFile(resourceLocation));
- }
-
/**
* Returns <code>BufferedImage</code>, same as <code>ImageIO.read()</code> does.
*
@@ -185,7 +191,7 @@ public class ImagesServiceImpl implements ImagesService {
public void saveImageWithPreviews(String tempFilename, String outputFilename) throws IOException {
String ext = FilenameUtils.getExtension(outputFilename);
- Path outputImagePath = Paths.get(imgDir, "p", outputFilename);
+ Path outputImagePath = Paths.get(fullImageDir, outputFilename);
// this throws strange exceptions
// Files.move(Paths.get(tmpDir, tempFilename), outputImagePath);
FileUtils.moveFile(Paths.get(tmpDir, tempFilename).toFile(), outputImagePath.toFile());
@@ -197,22 +203,28 @@ public class ImagesServiceImpl implements ImagesService {
BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage;
BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage;
BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage;
- ImageIO.write(image1024, ext, Paths.get(imgDir, "photos-1024", outputFilename).toFile());
- ImageIO.write(image0512, ext, Paths.get(imgDir, "photos-512", outputFilename).toFile());
- ImageIO.write(image0160, ext, Paths.get(imgDir, "ps", outputFilename).toFile());
+ ImageIO.write(image1024, ext, Paths.get(mediumImageDir, outputFilename).toFile());
+ ImageIO.write(image0512, ext, Paths.get(smallImageDir, outputFilename).toFile());
+ ImageIO.write(image0160, ext, Paths.get(thumbnailImageDir, outputFilename).toFile());
}
- public void saveAvatar(String tempFilename, int uid) throws IOException {
+ private String getAvatarFileName(User user, String targetExtension) {
+ return String.format("%s.%s", user.getUid(), targetExtension);
+ }
+
+ private Path getAvatarPath(User user) {
+ return Paths.get(avatarDir, getAvatarFileName(user, "png"));
+ }
+
+ public void saveAvatar(String tempFilename, User user) throws IOException {
String ext = FilenameUtils.getExtension(tempFilename);
- String originalName = String.format("%s.%s", uid, ext);
- Path originalPath = Paths.get(imgDir, "ao", originalName);
+ String originalName = getAvatarFileName(user, ext);
+ Path originalPath = Paths.get(avatarOriginalDir, originalName);
Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING);
BufferedImage originalImage = ImageIO.read(originalPath.toFile());
-
String targetExt = "png";
- String targetName = String.format("%s.%s", uid, targetExt);
- ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile());
- ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile());
+ ImageIO.write(Scalr.resize(originalImage, 96), targetExt, getAvatarPath(user).toFile());
+ ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(avatarSmallDir, getAvatarFileName(user, targetExt)).toFile());
}
public Attachment getAttachment(File imgFile) throws IOException {
@@ -243,6 +255,15 @@ public class ImagesServiceImpl implements ImagesService {
return attachment;
}
+ public Attachment getAvatarMetadata(User user) throws IOException {
+ return getAttachment(getAvatarPath(user).toFile());
+ }
+
+ @Override
+ public String getBaseDirectory() {
+ return baseDir;
+ }
+
@Override
public String getImageDirectory() {
return imgDir;
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java
index b6a2edb7..a787779b 100644
--- a/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -63,7 +63,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Inject
private SearchService searchService;
@Inject
- private ImagesService imagesService;
+ private StorageService storageService;
@Inject
private WebApp webApp;
@Value("${photos_url:https://i.juick.com/}")
@@ -113,7 +113,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
msg.setUnread(rs.getBoolean(26));
if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
try {
- imagesService.setAttachmentMetadata(baseImagesUrl, msg);
+ storageService.setAttachmentMetadata(baseImagesUrl, msg);
} catch (Exception e) {
logger.warn("exception reading images for mid {} rid {}", msg.getMid(), msg.getRid(), e);
}
@@ -434,7 +434,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
msg.setReplyToUri(URI.create(Optional.ofNullable(rs.getString(15)).orElse(StringUtils.EMPTY)));
if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
try {
- imagesService.setAttachmentMetadata(baseImagesUrl, msg);
+ storageService.setAttachmentMetadata(baseImagesUrl, msg);
} catch (Exception e) {
logger.warn("exception reading images for mid {} rid {}", msg.getMid(), msg.getRid(), e);
}
diff --git a/src/main/java/com/juick/service/ImagesService.java b/src/main/java/com/juick/service/StorageService.java
index 0891dca9..f1f72a60 100644
--- a/src/main/java/com/juick/service/ImagesService.java
+++ b/src/main/java/com/juick/service/StorageService.java
@@ -17,13 +17,14 @@
package com.juick.service;
+import java.io.File;
+import java.io.IOException;
+
import com.juick.model.Attachment;
import com.juick.model.Message;
+import com.juick.model.User;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-
-public interface ImagesService {
+public interface StorageService {
void setAttachmentMetadata(String baseUrl, Message msg) throws Exception;
/**
@@ -38,18 +39,17 @@ public interface ImagesService {
* Save new avatar in all required sizes.
*
* @param tempFilename Name of the image file in the temp folder.
- * @param uid User id that is used to build image file names.
+ * @param user User hat is used to build image file names.
*/
- void saveAvatar(String tempFilename, int uid) throws IOException;
+ void saveAvatar(String tempFilename, User user) throws IOException;
+
+ public Attachment getAttachment(File imgFile) throws IOException;
+
+ public Attachment getAvatarMetadata(User user) throws IOException;
+
+ String getBaseDirectory();
String getTemporaryDirectory();
String getImageDirectory();
-
- /**
- * Get image metadata
- * @param resource URL
- * @return image metadata
- */
- Attachment getImageMetadata(String resourceUrl) throws FileNotFoundException, IOException;
}