Skip to content

Instantly share code, notes, and snippets.

@jittagornp
Last active November 2, 2015 05:17
Show Gist options
  • Save jittagornp/86f188f4c1cfddaedddd to your computer and use it in GitHub Desktop.
Save jittagornp/86f188f4c1cfddaedddd to your computer and use it in GitHub Desktop.
package com.pamarin.api.store;
import com.pamarin.api.util.BufferedImageUtils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.apache.commons.io.FileUtils;
import org.springframework.util.StringUtils;
/**
* @author jittagornp
*/
public class PhotoStore {
private static final String baseFolder = "...";
private static final String storeName = "photos";
private static final String photoName = "photo";
private static File getRootStore() {
File folder = new File(baseFolder, storeName);
if (!folder.exists()) {
folder.mkdirs();
}
return folder;
}
private static File getLocation(Date date, String photoId) {
File folder = new File(
getRootStore(),
PhotoFolderPathGenerator.generate(date, photoId)
);
return folder;
}
private static String toName(PhotoInfo info) {
return ":name.:extenstion"
.replace(":name", photoName)
.replace(":extenstion", info.getExtensionFile());
}
private static String toThumbName(PhotoInfo info) {
return ":name_:thumb.:extenstion"
.replace(":name", photoName)
.replace(":thumb", info.getThumb())
.replace(":extenstion", info.getExtensionFile());
}
private static String toModeName(PhotoInfo info) {
return ":mode_:name_.:extenstion"
.replace(":name", photoName)
.replace(":mode", info.getMode())
.replace(":extenstion", info.getExtensionFile());
}
private static String toThumbAndModeName(PhotoInfo info) {
return ":mode_:name_:thumb.:extenstion"
.replace(":name", photoName)
.replace(":thumb", info.getThumb())
.replace(":mode", info.getMode())
.replace(":extenstion", info.getExtensionFile());
}
private static File defaultFile(PhotoInfo info) {
return new File(getLocation(
info.getCreateDate(),
info.getPhotoId()
), toName(info));
}
private static File modeFile(PhotoInfo info) {
return new File(getLocation(
info.getCreateDate(),
info.getPhotoId()
), toModeName(info));
}
private static File thumbFile(PhotoInfo info) {
return new File(getLocation(
info.getCreateDate(),
info.getPhotoId()
), toThumbName(info));
}
private static File thumbFileAndMode(PhotoInfo info) {
return new File(getLocation(
info.getCreateDate(),
info.getPhotoId()
), toThumbAndModeName(info));
}
/**
* @param info
* @return File
* @throws IOException
*/
public static File readFile(PhotoInfo info) throws IOException {
if (StringUtils.hasText(info.getThumb())) {
if (StringUtils.hasText(info.getMode())) {
return readThumbAndGrayscaleFile(info);
} else {
return readThumbFile(info);
}
} else {
if (StringUtils.hasText(info.getMode())) {
return readGrayscaleFile(info);
} else {
return defaultFile(info);
}
}
}
private static File readThumbAndGrayscaleFile(PhotoInfo info) throws IOException {
File file = thumbFileAndMode(info);
if (!file.exists()) {
File thumbFile = thumbFile(info);
if (!thumbFile.exists()) {
thumbFile = createThumbFile(
info,
defaultFile(info),
thumbFile
);
}
return createGrayscaleFile(
info,
thumbFile,
file
);
}
return file;
}
private static File readGrayscaleFile(PhotoInfo info) throws IOException {
File file = modeFile(info);
if (!file.exists()) {
file = createGrayscaleFile(
info,
defaultFile(info),
file
);
}
return file;
}
private static File readThumbFile(PhotoInfo info) throws IOException {
File file = thumbFile(info);
if (!file.exists()) {
file = createThumbFile(
info,
defaultFile(info),
file
);
}
return file;
}
private static File createThumbFile(PhotoInfo info, File source, File destination) throws IOException {
Size size = SizeExtractor.extract(info.getThumb());
Thumbnails.of(source)
.size(size.getWidth(), size.getHeight())
.crop(Positions.CENTER)
.outputQuality(1.0)
.toFile(destination);
return destination;
}
private static File createGrayscaleFile(PhotoInfo info, File source, File destination) throws IOException {
BufferedImage img = ImageIO.read(source);
BufferedImageUtils.toGrayScale(img);
ImageIO.write(
img,
info.getExtensionFile(),
destination
);
return destination;
}
/**
* @param info
* @param img
* @throws IOException
*/
public static void writeFile(PhotoInfo info, BufferedImage img) throws IOException {
File folder = getLocation(
info.getCreateDate(),
info.getPhotoId()
);
if (!folder.exists()) {
folder.mkdirs();
}
ImageIO.write(
img,
info.getExtensionFile(),
new File(folder, toName(info))
);
}
/**
* @param info
* @throws IOException
*/
public static void deleteFile(PhotoInfo info) throws IOException {
File folder = getLocation(
info.getCreateDate(),
info.getPhotoId()
);
if (folder.exists()) {
deleteHierarchyFolder(folder);
}
}
private static void deleteHierarchyFolder(File folder) throws IOException {
File parent = folder.getParentFile();
FileUtils.deleteDirectory(folder);
if (emptyFolder(parent)) {
deleteHierarchyFolder(parent);
}
}
private static boolean emptyFolder(File file) {
return !storeName.equals(file.getName())
&& isEmpty(file.listFiles());
}
private static boolean isEmpty(Object[] arr) {
return arr == null || arr.length == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment