Author: alevkovsky
Date: 2009-03-26 14:10:54 -0400 (Thu, 26 Mar 2009)
New Revision: 13229
Modified:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java
Log:
Loading images from image.jar
Modified:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java
===================================================================
---
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java 2009-03-26
17:33:47 UTC (rev 13228)
+++
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java 2009-03-26
18:10:54 UTC (rev 13229)
@@ -20,356 +20,422 @@
*/
package org.richfaces.realworld.manager;
+import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.AutoCreate;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.richfaces.realworld.service.Constants;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.ImageWriter;
+import javax.imageio.stream.ImageInputStream;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.List;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
-import javax.imageio.ImageIO;
-import javax.imageio.ImageReader;
-import javax.imageio.ImageWriter;
-import javax.imageio.stream.ImageInputStream;
-
-import org.jboss.seam.Component;
-import org.jboss.seam.ScopeType;
-import org.jboss.seam.annotations.AutoCreate;
-import org.jboss.seam.annotations.Create;
-import org.jboss.seam.annotations.Name;
-import org.jboss.seam.annotations.Scope;
-import org.richfaces.realworld.service.Constants;
-
@Name("fileManager")
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class FileManager {
- private static final String _SMALL160 = "_small160";
- private static final String _SMALL120 = "_small120";
- private static final String _SMALL80 = "_small80";
- private static final String _SMALL200 = "_small200";
- private static final String _MEDIUM = "_medium";
- private static final String _MINI = "_mini";
-
- static final String UPLOAD_ROOT_COMPONENT_NAME = "uploadRoot";
- static final String UPLOAD_ROOT_PATH_COMPONENT_NAME = "uploadRootPath";
+ private static final String _SMALL160 = "_small160";
+ private static final String _SMALL120 = "_small120";
+ private static final String _SMALL80 = "_small80";
+ private static final String _SMALL200 = "_small200";
+ private static final String _MEDIUM = "_medium";
+ private static final String _MINI = "_mini";
- private File uploadRoot;
- private String uploadRootPath;
+ private static final String UPLOAD_ROOT_COMPONENT_NAME = "uploadRoot";
+ private static final String UPLOAD_ROOT_PATH_COMPONENT_NAME =
"uploadRootPath";
- //TODO nick - i suggest to hide this from external services at all
- public File getUploadRoot() {
- return uploadRoot;
- }
+ private static final String IMAGE_JAR = "images.jar";
- @Create
- public void create() {
- uploadRoot = (File)Component.getInstance(UPLOAD_ROOT_COMPONENT_NAME,
ScopeType.APPLICATION);
- uploadRootPath = (String)Component.getInstance(UPLOAD_ROOT_PATH_COMPONENT_NAME,
ScopeType.APPLICATION);
- }
-
- public void setUploadRoot(String uploadRootPath) throws IOException {
- if (uploadRootPath != null) {
- this.uploadRoot = new File(uploadRootPath);
- for (String f : uploadRoot.list()) {
- File temp = new File(uploadRoot, f);
- temp.delete();
- }
- uploadRoot.delete();
- uploadRoot.mkdirs();
- this.uploadRootPath = this.uploadRoot.getCanonicalPath()
- + File.separator;
- } else {
- this.uploadRoot = null;
- this.uploadRootPath = null;
- }
- }
+ private File uploadRoot;
+ private String uploadRootPath;
-
+ //TODO nick - i suggest to hide this from external services at all
+ public File getUploadRoot() {
+ return uploadRoot;
+ }
- private File getFileByPath(String path) {
- if (this.uploadRoot != null) {
- File result = new File(this.uploadRoot, path);
+ @Create
+ public void create() {
+ uploadRoot = (File) Component.getInstance(UPLOAD_ROOT_COMPONENT_NAME,
ScopeType.APPLICATION);
+ if (!uploadRoot.exists() && !uploadRoot.mkdir()) {
+ logError("Can't create directory: " + uploadRoot.getName(),
null);
+ }
- try {
- String resultCanonicalPath = result.getCanonicalPath();
- if (!resultCanonicalPath.startsWith(this.uploadRootPath)) {
- // TODO log error
- result = null;
- }
+ uploadRootPath = (String) Component.getInstance(UPLOAD_ROOT_PATH_COMPONENT_NAME,
ScopeType.APPLICATION);
- return result;
- } catch (IOException e) {
- // TODO: handle exception
- result = null;
- }
+ final JarFile jarFile;
+ try {
+ jarFile = new JarFile(this.getClass().getClassLoader()
+ .getResource("WEB-INF/lib/" + IMAGE_JAR).getPath());
+ } catch (IOException e) {
+ logError(IMAGE_JAR + " not found, please check it in WEB-INF/lib ",
e);
+ return;
+ }
- return result;
- }
+ extract(jarFile);
+ }
- return null;
- }
+ private String joinFiles(String... files) {
+ final StringBuilder res = new StringBuilder();
+ for (String file : files) {
+ res.append(file).append(File.pathSeparatorChar);
+ }
- public boolean isDirectoryPresent(String directory) {
- File file = getFileByPath(directory);
- return file.exists() && file.isDirectory();
- }
+ return res.substring(0, res.length() - 1);
+ }
- public void deleteDirectory(String... directories) {
- String directory = new String();
- for (String chunk : directories) {
- directory += chunk + File.separator;
- }
- File file = getFileByPath(directory);
- if (file.exists()) {
- for (String f : file.list()) {
- File temp = new File(file, f);
- temp.delete();
- }
- file.delete();
- }
- }
+ private void logError(String s, Exception e) {
+ if (s != null) {
+ System.out.println(s);
+ }
- public boolean renameDirectory(String directoryOld, String directoryNew) {
- File fileOld = getFileByPath(directoryOld);
- File fileNew = getFileByPath(directoryNew);
+ if (e != null) {
+ e.printStackTrace();
+ }
+ }
- createDirectoryIfNotExist(directoryNew);
- if (fileNew.exists()) {
- if (fileNew.isDirectory()) {
- return false;
- } else {
- fileNew.delete();
- }
- }
- fileOld.renameTo(fileNew);
- return true;
- }
+ public void extract(JarFile jarFile) {
+ final Enumeration<JarEntry> entries = jarFile.entries();
+ while (entries.hasMoreElements()) {
+ final JarEntry jarEntry = entries.nextElement();
+ System.out.println("Extracting file : " + jarEntry.getName());
+ final File tmpFile = new File(joinFiles(uploadRootPath,
jarEntry.getName()));
+ if (jarEntry.isDirectory()) {
+ if (!tmpFile.mkdir()) {
+ logError("Can't create directory: " +
tmpFile.getName(), null);
+ return;
+ }
+ } else {
+ extractEntryFile(jarFile, jarEntry, tmpFile);
+ }
+ }
+ }
- public void addDirectory(String... directories) {
- String directory = new String();
- for (String chunk : directories) {
- directory += chunk;
- }
- File file = getFileByPath(directory);
- file.mkdirs();
- }
+ private void extractEntryFile(JarFile jarFile, ZipEntry jarEntry, File tmpFile) {
+ try {
+ final InputStream in = jarFile.getInputStream(jarEntry);
+ final FileOutputStream out = new FileOutputStream(tmpFile);
- public boolean addImage(String fileName, String tempFilePath) {
- createDirectoryIfNotExist(fileName);
- writeFile(fileName, tempFilePath, _SMALL80, 80, true);
- writeFile(fileName, tempFilePath, _SMALL120, 120, true);
- writeFile(fileName, tempFilePath, _SMALL160, 160, true);
- writeFile(fileName, tempFilePath, _SMALL200, 200, true);
- writeFile(fileName, tempFilePath, _MINI, 150, true);
- writeFile(fileName, tempFilePath, _MEDIUM, 600, true);
- return true;
- }
+ int readedBytes = in.read();
+ while (readedBytes != -1) {
+ out.write(readedBytes);
+ readedBytes = in.read();
+ }
- public static BufferedImage bitmapToImage(String data, String format) throws IOException
{
- InputStream inb = new FileInputStream(data);
- ImageReader rdr = (ImageReader)
ImageIO.getImageReadersByFormatName(format).next();
- ImageInputStream imageInput = ImageIO.createImageInputStream(inb);
- rdr.setInput(imageInput);
- BufferedImage image = rdr.read(0);
- inb.close();
- return image;
- }
+ out.close();
+ in.close();
+ } catch (FileNotFoundException e) {
+ logError(e.getMessage(), e);
+ } catch (IOException e) {
+ logError(e.getMessage(), e);
+ }
+ }
- public static void imageToBitmap(BufferedImage image, String data, String format)
throws IOException {
- OutputStream inb = new FileOutputStream(data);
- ImageWriter wrt = (ImageWriter)
ImageIO.getImageWritersByFormatName(format).next();
- ImageInputStream imageInput = ImageIO.createImageOutputStream(inb);
- wrt.setOutput(imageInput);
- wrt.write(image);
- inb.close();
- }
+ public void setUploadRoot(String uploadRootPath) throws IOException {
+ if (uploadRootPath != null) {
+ this.uploadRoot = new File(uploadRootPath);
+ for (String f : uploadRoot.list()) {
+ final File temp = new File(uploadRoot, f);
+ temp.delete();
+ }
+ uploadRoot.delete();
+ uploadRoot.mkdirs();
+ this.uploadRootPath = this.uploadRoot.getCanonicalPath() + File.separator;
+ } else {
+ this.uploadRoot = null;
+ this.uploadRootPath = null;
+ }
+ }
- /**
- * Convenience method that returns a scaled instance of the
- * provided {@code BufferedImage}.
- *
- * @param img the original image to be scaled
- * @param targetWidth the desired width of the scaled instance,
- * in pixels
- * @param targetHeight the desired height of the scaled instance,
- * in pixels
- * @param hint one of the rendering hints that corresponds to
- * {@code RenderingHints.KEY_INTERPOLATION} (e.g.
- * {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
- * {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
- * {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
- * @param higherQuality if true, this method will use a multi-step
- * scaling technique that provides higher quality than the usual
- * one-step technique (only useful in downscaling cases, where
- * {@code targetWidth} or {@code targetHeight} is
- * smaller than the original dimensions, and generally only when
- * the {@code BILINEAR} hint is specified)
- * @return a scaled version of the original {@code BufferedImage}
- */
- public static BufferedImage getScaledInstance(BufferedImage img,
- int targetWidth,
- int targetHeight,
- Object hint,
- boolean higherQuality)
- {
- int type = (img.getTransparency() == Transparency.OPAQUE) ?
- BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
- BufferedImage ret = (BufferedImage)img;
- int w, h;
- if (higherQuality) {
- // Use multi-step technique: start with original size, then
- // scale down in multiple passes with drawImage()
- // until the target size is reached
- w = img.getWidth();
- h = img.getHeight();
- } else {
- // Use one-step technique: scale directly from original
- // size to target size with a single drawImage() call
- w = targetWidth;
- h = targetHeight;
- }
- do {
- if (higherQuality && w > targetWidth) {
- w /= 2;
- if (w < targetWidth) {
- w = targetWidth;
- }
- }
+ private File getFileByPath(String path) {
+ if (this.uploadRoot != null) {
+ File result = new File(this.uploadRoot, path);
- if (higherQuality && h > targetHeight) {
- h /= 2;
- if (h < targetHeight) {
- h = targetHeight;
- }
- }
+ try {
+ final String resultCanonicalPath = result.getCanonicalPath();
+ if (!resultCanonicalPath.startsWith(this.uploadRootPath)) {
+ // TODO log error
+ result = null;
+ }
+ return result;
+ } catch (IOException e) {
+ // TODO: handle exception
+ result = null;
+ }
- BufferedImage tmp = new BufferedImage(w, h, type);
- Graphics2D g2 = tmp.createGraphics();
- g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
- g2.drawImage(ret, 0, 0, w, h, null);
- g2.dispose();
+ return result;
+ }
- ret = tmp;
- } while (w != targetWidth || h != targetHeight);
+ return null;
+ }
- return ret;
- }
-
- public void writeFile(String newFileName, String fileName,String pattern, int size,
boolean includeUploadRoot) {
- BufferedImage bsrc =null;
- try {
- bsrc = bitmapToImage(fileName, "JPG");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- int i = bsrc.getWidth() > bsrc.getHeight()? bsrc.getWidth(): bsrc.getHeight();
- double d = (double) size
- / i;
- Double yy = ((Double)d*bsrc.getWidth());
- int width = yy.intValue();
- yy = ((Double)d*bsrc.getHeight());
- int height= yy.intValue();
- BufferedImage bdest = getScaledInstance(bsrc, width, height,
RenderingHints.VALUE_INTERPOLATION_BICUBIC, true);
- String dest = includeUploadRoot? this.uploadRootPath + transformPath(newFileName,
pattern) : transformPath(newFileName, pattern);
- try {
- imageToBitmap(bdest, dest, "JPG");
- } catch (IOException ex) {
- // TODO Auto-generated catch block
- ex.printStackTrace();
- }
- }
+ public boolean isDirectoryPresent(String directory) {
+ final File file = getFileByPath(directory);
+ return file.exists() && file.isDirectory();
+ }
- public String transformPath(String target, String substitute) {
- String begin = target.substring(0, target.lastIndexOf(Constants.DOT));
- String end = target.substring(target.lastIndexOf(Constants.DOT));
- return begin + substitute + end;
- }
+ public void deleteDirectory(String... directories) {
+ final File file = getFileByPath(joinFiles(directories).toString());
+ if (file.exists()) {
+ for (String f : file.list()) {
+ final File temp = new File(file, f);
+ temp.delete();
+ }
+ file.delete();
+ }
+ }
- public void deleteImage(String fileName) {
- deleteImage(getFileByPath(fileName));
- deleteImage(getFileByPath(transformPath(fileName, _MINI)));
- deleteImage(getFileByPath(transformPath(fileName, _MEDIUM)));
- deleteImage(getFileByPath(transformPath(fileName, _SMALL80)));
- deleteImage(getFileByPath(transformPath(fileName, _SMALL120)));
- deleteImage(getFileByPath(transformPath(fileName, _SMALL160)));
- }
+ public boolean renameDirectory(String directoryOld, String directoryNew) {
+ final File fileOld = getFileByPath(directoryOld);
+ final File fileNew = getFileByPath(directoryNew);
- private void deleteImage(File file) {
- if (file.exists()) {
- file.delete();
- }
- }
+ createDirectoryIfNotExist(directoryNew);
+ if (fileNew.exists()) {
+ if (fileNew.isDirectory()) {
+ return false;
+ } else {
+ fileNew.delete();
+ }
+ }
+ fileOld.renameTo(fileNew);
+ return true;
+ }
- public String getPathOfImage(String path) {
- int i = path.lastIndexOf(Constants.SLASH);
- return path.substring(i);
- }
+ public void addDirectory(String... directories) {
+ final StringBuilder directory = new StringBuilder();
+ for (String chunk : directories) {
+ directory.append(chunk);
+ }
- public void renameImage(String fileNameOld, String fileNameNew) {
- createDirectoryIfNotExist(fileNameNew);
- renameImage(getFileByPath(fileNameNew), getFileByPath(fileNameOld));
-
- renameImage(getFileByPath(transformPath(fileNameNew, _MINI)),
- getFileByPath(transformPath(fileNameOld, _MINI)));
-
- renameImage(getFileByPath(transformPath(fileNameNew, _MEDIUM)),
- getFileByPath(transformPath(fileNameOld, _MEDIUM)));
-
- renameImage(getFileByPath(transformPath(fileNameNew, _SMALL80)),
- getFileByPath(transformPath(fileNameOld, _SMALL80)));
-
- renameImage(getFileByPath(transformPath(fileNameNew, _SMALL120)),
- getFileByPath(transformPath(fileNameOld, _SMALL120)));
-
- renameImage(getFileByPath(transformPath(fileNameNew, _SMALL160)),
- getFileByPath(transformPath(fileNameOld, _SMALL160)));
- }
+ final File file = getFileByPath(directory.toString());
+ file.mkdirs();
+ }
- private void renameImage(File fileNew, File fileOld) {
- if (fileNew.exists()) {
- fileNew.delete();
- }
- fileOld.renameTo(fileNew);
- }
+ public boolean addImage(String fileName, String tempFilePath) {
+ createDirectoryIfNotExist(fileName);
+ writeFile(fileName, tempFilePath, _SMALL80, 80, true);
+ writeFile(fileName, tempFilePath, _SMALL120, 120, true);
+ writeFile(fileName, tempFilePath, _SMALL160, 160, true);
+ writeFile(fileName, tempFilePath, _MINI, 150, true);
+ writeFile(fileName, tempFilePath, _MEDIUM, 600, true);
+ return true;
+ }
- private void createDirectoryIfNotExist(String fileNameNew) {
- int lastIndexOf = fileNameNew.lastIndexOf("/");
- if (lastIndexOf > 0) {
- String directory = fileNameNew.substring(0, lastIndexOf);
- File file = getFileByPath(directory);
- if (!file.exists()) {
- file.mkdirs();
- }
- }
- }
+ public static BufferedImage bitmapToImage(String data, String format) throws
IOException {
+ final InputStream inb = new FileInputStream(data);
+ final ImageReader rdr = ImageIO.getImageReadersByFormatName(format).next();
+ final ImageInputStream imageInput = ImageIO.createImageInputStream(inb);
+ rdr.setInput(imageInput);
+ final BufferedImage image = rdr.read(0);
+ inb.close();
+ return image;
+ }
- public boolean isImagePresent(String fileName) {
- return getFileByPath(fileName) != null && getFileByPath(fileName).exists()
&& !getFileByPath(fileName).isDirectory();
- }
+ public static void imageToBitmap(BufferedImage image, String data, String format)
throws IOException {
+ final OutputStream inb = new FileOutputStream(data);
+ final ImageWriter wrt = ImageIO.getImageWritersByFormatName(format).next();
+ final ImageInputStream imageInput = ImageIO.createImageOutputStream(inb);
+ wrt.setOutput(imageInput);
+ wrt.write(image);
+ inb.close();
+ }
- public File getImage(String fileName) {
- if (isImagePresent(fileName)) {
- return getFileByPath(fileName);
- }
- return null;
- }
-
- public File getImageFile(String fileName) {
- return new File(fileName);
- }
+ /**
+ * Convenience method that returns a scaled instance of the
+ * provided {@code BufferedImage}.
+ *
+ * @param img the original image to be scaled
+ * @param targetWidth the desired width of the scaled instance,
+ * in pixels
+ * @param targetHeight the desired height of the scaled instance,
+ * in pixels
+ * @param hint one of the rendering hints that corresponds to
+ * {@code RenderingHints.KEY_INTERPOLATION} (e.g.
+ * {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
+ * {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
+ * {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
+ * @param higherQuality if true, this method will use a multi-step
+ * scaling technique that provides higher quality than the
usual
+ * one-step technique (only useful in downscaling cases, where
+ * {@code targetWidth} or {@code targetHeight} is
+ * smaller than the original dimensions, and generally only
when
+ * the {@code BILINEAR} hint is specified)
+ * @return a scaled version of the original {@code BufferedImage}
+ */
+ public static BufferedImage getScaledInstance(BufferedImage img,
+ int targetWidth,
+ int targetHeight,
+ Object hint,
+ boolean higherQuality) {
+ final int type = img.getTransparency() == Transparency.OPAQUE ?
+ BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
+ BufferedImage ret = (BufferedImage) img;
+ int w;
+ int h;
+ if (higherQuality) {
+ // Use multi-step technique: start with original size, then
+ // scale down in multiple passes with drawImage()
+ // until the target size is reached
+ w = img.getWidth();
+ h = img.getHeight();
+ } else {
+ // Use one-step technique: scale directly from original
+ // size to target size with a single drawImage() call
+ w = targetWidth;
+ h = targetHeight;
+ }
- public void deleteDirectories(List<String> directoriesToDelete) {
- for (String directory : directoriesToDelete) {
- deleteDirectory(directory);
- }
- }
-}
+ do {
+ if (higherQuality && w > targetWidth) {
+ w /= 2;
+ if (w < targetWidth) {
+ w = targetWidth;
+ }
+ }
+
+ if (higherQuality && h > targetHeight) {
+ h /= 2;
+ if (h < targetHeight) {
+ h = targetHeight;
+ }
+ }
+
+ final BufferedImage tmp = new BufferedImage(w, h, type);
+ final Graphics2D g2 = tmp.createGraphics();
+ g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
+ g2.drawImage(ret, 0, 0, w, h, null);
+ g2.dispose();
+
+ ret = tmp;
+ } while (w != targetWidth || h != targetHeight);
+
+ return ret;
+ }
+
+ public void writeFile(String newFileName, String fileName, String pattern, int size,
boolean includeUploadRoot) {
+ BufferedImage bsrc = null;
+ try {
+ bsrc = bitmapToImage(fileName, "JPG");
+ } catch (IOException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ final int i = bsrc.getWidth() > bsrc.getHeight() ? bsrc.getWidth() :
bsrc.getHeight();
+ final double d = ((double) size) / i;
+ final int width = (int) d * bsrc.getWidth();
+ final int height = (int) d * bsrc.getHeight();
+ final BufferedImage bdest = getScaledInstance(bsrc, width, height,
RenderingHints.VALUE_INTERPOLATION_BICUBIC, true);
+ final String dest = includeUploadRoot ? this.uploadRootPath +
transformPath(newFileName, pattern) : transformPath(newFileName, pattern);
+ try {
+ imageToBitmap(bdest, dest, "JPG");
+ } catch (IOException ex) {
+ // TODO Auto-generated catch block
+ ex.printStackTrace();
+ }
+ }
+
+ public String transformPath(String target, String substitute) {
+ final String begin = target.substring(0, target.lastIndexOf(Constants.DOT));
+ final String end = target.substring(target.lastIndexOf(Constants.DOT));
+ return begin + substitute + end;
+ }
+
+ public void deleteImage(String fileName) {
+ deleteImage(getFileByPath(fileName));
+ deleteImage(getFileByPath(transformPath(fileName, _MINI)));
+ deleteImage(getFileByPath(transformPath(fileName, _MEDIUM)));
+ deleteImage(getFileByPath(transformPath(fileName, _SMALL80)));
+ deleteImage(getFileByPath(transformPath(fileName, _SMALL120)));
+ deleteImage(getFileByPath(transformPath(fileName, _SMALL160)));
+ }
+
+ private void deleteImage(File file) {
+ if (file.exists()) {
+ file.delete();
+ }
+ }
+
+ public String getPathOfImage(String path) {
+ final int i = path.lastIndexOf(Constants.SLASH);
+ return path.substring(i);
+ }
+
+ public void renameImage(String fileNameOld, String fileNameNew) {
+ createDirectoryIfNotExist(fileNameNew);
+ renameImage(getFileByPath(fileNameNew), getFileByPath(fileNameOld));
+
+ renameImage(getFileByPath(transformPath(fileNameNew, _MINI)),
+ getFileByPath(transformPath(fileNameOld, _MINI)));
+
+ renameImage(getFileByPath(transformPath(fileNameNew, _MEDIUM)),
+ getFileByPath(transformPath(fileNameOld, _MEDIUM)));
+
+ renameImage(getFileByPath(transformPath(fileNameNew, _SMALL80)),
+ getFileByPath(transformPath(fileNameOld, _SMALL80)));
+
+ renameImage(getFileByPath(transformPath(fileNameNew, _SMALL120)),
+ getFileByPath(transformPath(fileNameOld, _SMALL120)));
+
+ renameImage(getFileByPath(transformPath(fileNameNew, _SMALL160)),
+ getFileByPath(transformPath(fileNameOld, _SMALL160)));
+ }
+
+ private void renameImage(File fileNew, File fileOld) {
+ if (fileNew.exists()) {
+ fileNew.delete();
+ }
+ fileOld.renameTo(fileNew);
+ }
+
+ private void createDirectoryIfNotExist(String fileNameNew) {
+ final int lastIndexOf = fileNameNew.lastIndexOf('/');
+ if (lastIndexOf > 0) {
+ final String directory = fileNameNew.substring(0, lastIndexOf);
+ final File file = getFileByPath(directory);
+ if (!file.exists()) {
+ file.mkdirs();
+ }
+ }
+ }
+
+ public boolean isImagePresent(String fileName) {
+ return getFileByPath(fileName) != null &&
getFileByPath(fileName).exists() && !getFileByPath(fileName).isDirectory();
+ }
+
+ public File getImage(String fileName) {
+ if (isImagePresent(fileName)) {
+ return getFileByPath(fileName);
+ }
+ return null;
+ }
+
+ public File getImageFile(String fileName) {
+ return new File(fileName);
+ }
+
+ public void deleteDirectories(Iterable<String> directoriesToDelete) {
+ for (String directory : directoriesToDelete) {
+ deleteDirectory(directory);
+ }
+ }
+}
\ No newline at end of file