You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ha...@apache.org on 2011/03/25 08:57:47 UTC

svn commit: r1085275 - in /ofbiz/trunk: applications/product/config/ applications/product/src/org/ofbiz/product/image/ applications/product/src/org/ofbiz/product/product/ applications/product/webapp/catalog/WEB-INF/actions/product/ framework/common/src...

Author: hansbak
Date: Fri Mar 25 07:57:46 2011
New Revision: 1085275

URL: http://svn.apache.org/viewvc?rev=1085275&view=rev
Log:
patch provided by Eric de Maulde OFBIZ-4217: Scaling image for every size type from ImageProperties.xml

Modified:
    ofbiz/trunk/applications/product/config/ImageProperties.xml
    ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java
    ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java
    ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductContent.groovy
    ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java

Modified: ofbiz/trunk/applications/product/config/ImageProperties.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/config/ImageProperties.xml?rev=1085275&r1=1085274&r2=1085275&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/config/ImageProperties.xml (original)
+++ ofbiz/trunk/applications/product/config/ImageProperties.xml Fri Mar 25 07:57:46 2011
@@ -17,8 +17,12 @@ KIND, either express or implied.  See th
 specific language governing permissions and limitations
 under the License.
 -->
+
+<!-- 
+    # The uploaded image is scaled in each size tye , you can descrease or increase size types ; size type name doesn't have to contain space
+    # Just one configured dimension on two is allowed to restrict just one image dimension, whatever its proportions 
+-->
 <imageSize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-    <format name="extension" value="jpg"/>
     <size name="small">
         <dimension name="height" value="50"/>
         <dimension name="width" value="50"/>

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java?rev=1085275&r1=1085274&r2=1085275&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java Fri Mar 25 07:57:46 2011
@@ -23,6 +23,8 @@ import java.awt.image.ImagingOpException
 import java.awt.image.RenderedImage;
 import java.io.File;
 import java.io.IOException;
+import java.lang.NullPointerException;
+import java.lang.SecurityException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
@@ -118,7 +120,6 @@ public class ScaleImage {
             filenameExpander = FlexibleStringExpander.getInstance(filenameFormat);
             id = (String) context.get("productId");
             fileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "type", "original"));
-            Debug.logWarning("fileLocation for view type = MAIN : " + fileLocation,module);
         } else if (viewType.toLowerCase().contains("additional") && viewNumber != null && !viewNumber.equals("0")) {
             String filenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.additionalviewsize.format");
             filenameExpander = FlexibleStringExpander.getInstance(filenameFormat);
@@ -138,7 +139,7 @@ public class ScaleImage {
         }
         
         /* get original BUFFERED IMAGE */
-            resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + fileLocation + "." + imgExtension, locale));
+        resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + fileLocation + "." + imgExtension, locale));
 
         if (resultBufImgMap.containsKey("responseMessage") && resultBufImgMap.get("responseMessage").equals("success")) {
             bufImg = (BufferedImage) resultBufImgMap.get("bufferedImage");
@@ -153,16 +154,18 @@ public class ScaleImage {
                 return result;
             }
 
-            /* scale Image for each Size Type */
-            Iterator<String> sizeIter = sizeTypeList.iterator();
-            while (sizeIter.hasNext()) {
-                String sizeType = sizeIter.next();
+            /* Scale image for each size from ImageProperties.xml */
+            for (Map.Entry<String, Map<String, String>> entry : imgPropertyMap.entrySet()) {
+                String sizeType = entry.getKey();
+                
+                // Scale
                 resultScaleImgMap.putAll(ImageTransform.scaleImage(bufImg, imgHeight, imgWidth, imgPropertyMap, sizeType, locale));
 
+                /* Write the new image file */
                 if (resultScaleImgMap.containsKey("responseMessage") && resultScaleImgMap.get("responseMessage").equals("success")) {
                     bufNewImg = (BufferedImage) resultScaleImgMap.get("bufferedImage");
 
-                    // write the New Scaled Image
+                    // Build full path for the new scaled image
                     String newFileLocation = null;
                     filenameToUse = sizeType + filenameToUse.substring(filenameToUse.lastIndexOf("."));
                     if (viewType.toLowerCase().contains("main")) {
@@ -170,21 +173,38 @@ public class ScaleImage {
                     } else if (viewType.toLowerCase().contains("additional")) {
                         newFileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "viewtype", viewType, "sizetype", sizeType));
                     }
-
                     String newFilePathPrefix = "";
                     if (newFileLocation.lastIndexOf("/") != -1) {
                         newFilePathPrefix = newFileLocation.substring(0, newFileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing slash
-                    }
-
+                    }     
+                    // Directory
                     String targetDirectory = imageServerPath + "/" + newFilePathPrefix;
-                    File targetDir = new File(targetDirectory);
-                    if (!targetDir.exists()) {
-                        boolean created = targetDir.mkdirs();
-                        if (!created) {
-                            String errMsg = UtilProperties.getMessage(resource, "ScaleImage.unable_to_create_target_directory", locale) + " - " + targetDirectory;
-                            Debug.logFatal(errMsg, module);
-                            return ServiceUtil.returnError(errMsg);
+                    try {
+                        // Create the new directory
+                        File targetDir = new File(targetDirectory);
+                        if (!targetDir.exists()) {
+                            boolean created = targetDir.mkdirs();
+                            if (!created) {
+                                String errMsg = UtilProperties.getMessage(resource, "ScaleImage.unable_to_create_target_directory", locale) + " - " + targetDirectory;
+                                Debug.logFatal(errMsg, module);
+                                return ServiceUtil.returnError(errMsg);
+                            }
+                        // Delete existing image files
+                        // Images aren't ordered by productId (${location}/${viewtype}/${sizetype}/${id}) !!! BE CAREFUL !!!
+                        } else if (newFileLocation.endsWith("/" + id)) {
+                            try {
+                                File[] files = targetDir.listFiles(); 
+                                for(File file : files) {
+                                    if (file.isFile() && file.getName().startsWith(id)) {
+                                        file.delete();
+                                    }    
+                                }
+                            } catch (SecurityException e) {
+                                Debug.logError(e,module);
+                            }
                         }
+                    } catch (NullPointerException e) {
+                        Debug.logError(e,module);
                     }
 
                     // write new image
@@ -202,12 +222,14 @@ public class ScaleImage {
                         return result;
                     }
 
-                    /* write Return Result */
-                    String imageUrl = imageUrlPrefix + "/" + newFileLocation + "." + imgExtension;
-                    imgUrlMap.put(sizeType, imageUrl);
+                    // Save each Url
+                    if (sizeTypeList.contains(sizeType)) {
+                        String imageUrl = imageUrlPrefix + "/" + newFileLocation + "." + imgExtension;
+                        imgUrlMap.put(sizeType, imageUrl);
+                    }
 
                 } // scaleImgMap
-            } // sizeIter
+            } // Loop over sizeType
 
             result.put("responseMessage", "success");
             result.put("imageUrlMap", imgUrlMap);

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java?rev=1085275&r1=1085274&r2=1085275&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java Fri Mar 25 07:57:46 2011
@@ -22,6 +22,8 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.lang.NullPointerException;
+import java.lang.SecurityException;
 import java.math.BigDecimal;
 import java.nio.ByteBuffer;
 import java.sql.Timestamp;
@@ -1015,28 +1017,59 @@ public class ProductServices {
 
             /* Write the new image file */
             String targetDirectory = imageServerPath + "/" + filePathPrefix;
-            File targetDir = new File(targetDirectory);
-            if (!targetDir.exists()) {
-                boolean created = targetDir.mkdirs();
-                if (!created) {
-                    String errMsg = UtilProperties.getMessage(resource, "ScaleImage.unable_to_create_target_directory", locale) + " - " + targetDirectory;
-                    Debug.logFatal(errMsg, module);
-                    return ServiceUtil.returnError(errMsg);
+            try {
+                File targetDir = new File(targetDirectory);
+                // Create the new directory
+                if (!targetDir.exists()) {
+                    boolean created = targetDir.mkdirs();
+                    if (!created) {
+                        String errMsg = UtilProperties.getMessage(resource, "ScaleImage.unable_to_create_target_directory", locale) + " - " + targetDirectory;
+                        Debug.logFatal(errMsg, module);
+                        return ServiceUtil.returnError(errMsg);
+                    }
+                // Delete existing image files
+                // Images are ordered by productId (${location}/${id}/${viewtype}/${sizetype})
+                } else if (!filenameToUse.contains(productId)) {
+                    try {
+                        File[] files = targetDir.listFiles(); 
+                        for(File file : files) {
+                            if (file.isFile()) file.delete(); 
+                        }
+                    } catch (SecurityException e) {
+                        Debug.logError(e,module);
+                    }
+                // Images aren't ordered by productId (${location}/${viewtype}/${sizetype}/${id})
+                } else {
+                    try {
+                        File[] files = targetDir.listFiles(); 
+                        for(File file : files) {
+                            if (file.isFile() && file.getName().startsWith(productId + "_View_" + viewNumber)) file.delete();
+                        }
+                    } catch (SecurityException e) {
+                        Debug.logError(e,module);
+                    }
                 }
+            } catch (NullPointerException e) {
+                Debug.logError(e,module);
             }
-            File file = new File(imageServerPath + "/" + fileLocation + "." +  extension.getString("fileExtensionId"));
+            // Write
             try {
-                RandomAccessFile out = new RandomAccessFile(file, "rw");
-                out.write(imageData.array());
-                out.close();
-            } catch (FileNotFoundException e) {
-                Debug.logError(e, module);
-                return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
-                        "ProductImageViewUnableWriteFile", UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
-            } catch (IOException e) {
-                Debug.logError(e, module);
-                return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
-                        "ProductImageViewUnableWriteBinaryData", UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
+            File file = new File(imageServerPath + "/" + fileLocation + "." +  extension.getString("fileExtensionId"));
+                try {
+                    RandomAccessFile out = new RandomAccessFile(file, "rw");
+                    out.write(imageData.array());
+                    out.close();
+                } catch (FileNotFoundException e) {
+                    Debug.logError(e, module);
+                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
+                            "ProductImageViewUnableWriteFile", UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
+                } catch (IOException e) {
+                    Debug.logError(e, module);
+                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, 
+                            "ProductImageViewUnableWriteBinaryData", UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
+                }
+            } catch (NullPointerException e) {
+                Debug.logError(e,module);
             }
 
             /* scale Image in different sizes */

Modified: ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductContent.groovy
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductContent.groovy?rev=1085275&r1=1085274&r2=1085275&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductContent.groovy (original)
+++ ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductContent.groovy Fri Mar 25 07:57:46 2011
@@ -110,7 +110,21 @@ if (fileType) {
             file = new File(imageServerPath + "/" + filePathPrefix, defaultFileName);
             file1 = new File(imageServerPath + "/" + filePathPrefix, filenameToUse);
             try {
-                file1.delete();
+                // Delete existing image files
+                File targetDir = new File(imageServerPath + "/" + filePathPrefix);
+                // Images are ordered by productId (${location}/${id}/${viewtype}/${sizetype})
+                if (!filenameToUse.startsWith(productId + ".")) {
+                    File[] files = targetDir.listFiles(); 
+                    for(File file : files) {
+                        if (file.isFile() && !file.getName().equals(defaultFileName)) file.delete();
+                    } 
+                // Images aren't ordered by productId (${location}/${viewtype}/${sizetype}/${id}) !!! BE CAREFUL !!!
+                } else {
+                    File[] files = targetDir.listFiles(); 
+                    for(File file : files) {
+                        if (file.isFile() && !file.getName().equals(defaultFileName)  && file.getName().startsWith(productId + ".")) file.delete();
+                    }
+                }
             } catch (Exception e) {
                 System.out.println("error deleting existing file (not neccessarily a problem)");
             }

Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java?rev=1085275&r1=1085274&r2=1085275&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java (original)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/image/ImageTransform.java Fri Mar 25 07:57:46 2011
@@ -117,8 +117,17 @@ public class ImageTransform {
         Map<String, Object> result = FastMap.newInstance();
 
         /* DIMENSIONS from ImageProperties */
-        defaultHeight = Double.parseDouble(dimensionMap.get(sizeType).get("height").toString());
-        defaultWidth = Double.parseDouble(dimensionMap.get(sizeType).get("width").toString());
+        // A missed dimension is authorized
+        if (dimensionMap.get(sizeType).containsKey("height")) {
+            defaultHeight = Double.parseDouble(dimensionMap.get(sizeType).get("height").toString());
+        } else {
+            defaultHeight = -1;
+        }
+        if (dimensionMap.get(sizeType).containsKey("width")) {
+            defaultWidth = Double.parseDouble(dimensionMap.get(sizeType).get("width").toString());
+        } else {
+            defaultWidth = -1;
+        }
         if (defaultHeight == 0.0 || defaultWidth == 0.0) {
             String errMsg = UtilProperties.getMessage(resource, "ImageTransform.one_default_dimension_is_null", locale) + " : defaultHeight = " + defaultHeight + " ; defaultWidth = " + defaultWidth;
             Debug.logError(errMsg, module);
@@ -128,7 +137,23 @@ public class ImageTransform {
 
         /* SCALE FACTOR */
         // find the right Scale Factor related to the Image Dimensions
-        if (imgHeight > imgWidth) {
+        if (defaultHeight == -1) {
+            scaleFactor = defaultWidth / imgWidth;
+            if (scaleFactor == 0.0) {
+                String errMsg = UtilProperties.getMessage(resource, "ImageTransform.width_scale_factor_is_null", locale) + "  (defaultWidth = " + defaultWidth + "; imgWidth = " + imgWidth;
+                Debug.logError(errMsg, module);
+                result.put("errorMessage", errMsg);
+                return result;
+            }
+        } else if (defaultWidth == -1) {
+            scaleFactor = defaultHeight / imgHeight;
+            if (scaleFactor == 0.0) {
+                String errMsg = UtilProperties.getMessage(resource, "ImageTransform.height_scale_factor_is_null", locale) + "  (defaultHeight = " + defaultHeight + "; imgHeight = " + imgHeight;
+                Debug.logError(errMsg, module);
+                result.put("errorMessage", errMsg);
+                return result;
+            }
+        } else if (imgHeight > imgWidth) {
             scaleFactor = defaultHeight / imgHeight;
             if (scaleFactor == 0.0) {
                 String errMsg = UtilProperties.getMessage(resource, "ImageTransform.height_scale_factor_is_null", locale) + "  (defaultHeight = " + defaultHeight + "; imgHeight = " + imgHeight;