Author: hansbak
Date: Mon Mar 14 07:16:36 2011 New Revision: 1081272 URL: http://svn.apache.org/viewvc?rev=1081272&view=rev Log: patch to scale main image product content and additional image product content: a contribution by Eric de Maulde: https://issues.apache.org/jira/browse/OFBIZ-4212 Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductServices.java 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=1081272&r1=1081271&r2=1081272&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 Mon Mar 14 07:16:36 2011 @@ -107,35 +107,41 @@ public class ScaleImage { String imgName = filenameToUse.substring(0, index - 1); String imgExtension = filenameToUse.substring(index + 1); // paths - String mainFilenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.format"); String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context); String imageUrlPrefix = UtilProperties.getPropertyValue("catalog", "image.url.prefix"); - - String id = null; + + FlexibleStringExpander filenameExpander; + String fileLocation = null; String type = null; + String id = null; if (viewType.toLowerCase().contains("main")) { - type = "original"; + String filenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.format"); + 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")) { - type = "additional"; - id = imgName + "_View_" + viewNumber; + String filenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.additionalviewsize.format"); + filenameExpander = FlexibleStringExpander.getInstance(filenameFormat); + id = (String) context.get("productId"); + if (filenameFormat.endsWith("${id}")) { + id = id + "_View_" + viewNumber; + } else { + viewType = "additional" + viewNumber; + } + fileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "viewtype", viewType, "sizetype", "original")); } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "ProductImageViewType", UtilMisc.toMap("viewType", type), locale)); + return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductImageViewType", UtilMisc.toMap("viewType", type), locale)); } - FlexibleStringExpander mainFilenameExpander = FlexibleStringExpander.getInstance(mainFilenameFormat); - String fileLocation = mainFilenameExpander.expandString(UtilMisc.toMap("location", "products", "id", context.get("productId"), "type", type)); + String filePathPrefix = ""; if (fileLocation.lastIndexOf("/") != -1) { filePathPrefix = fileLocation.substring(0, fileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing slash } - if (context.get("contentId") != null){ - resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + context.get("productId") + "/" + context.get("clientFileName"), locale)); - } else { - /* get original BUFFERED IMAGE */ - resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + filePathPrefix + filenameToUse, locale)); - } + /* get original BUFFERED IMAGE */ + resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + fileLocation + "." + imgExtension, locale)); + if (resultBufImgMap.containsKey("responseMessage") && resultBufImgMap.get("responseMessage").equals("success")) { bufImg = (BufferedImage) resultBufImgMap.get("bufferedImage"); @@ -149,13 +155,6 @@ public class ScaleImage { return result; } - // new Filename Format - FlexibleStringExpander addFilenameExpander = mainFilenameExpander; - if (viewType.toLowerCase().contains("additional")) { - String addFilenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.additionalviewsize.format"); - addFilenameExpander = FlexibleStringExpander.getInstance(addFilenameFormat); - } - /* scale Image for each Size Type */ Iterator<String> sizeIter = sizeTypeList.iterator(); while (sizeIter.hasNext()) { @@ -169,10 +168,11 @@ public class ScaleImage { String newFileLocation = null; filenameToUse = sizeType + filenameToUse.substring(filenameToUse.lastIndexOf(".")); if (viewType.toLowerCase().contains("main")) { - newFileLocation = mainFilenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "type", sizeType)); + newFileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "type", sizeType)); } else if (viewType.toLowerCase().contains("additional")) { - newFileLocation = addFilenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "viewtype", viewType, "sizetype", sizeType)); + 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 @@ -191,7 +191,7 @@ public class ScaleImage { // write new image try { - ImageIO.write(bufNewImg, imgExtension, new File(imageServerPath + "/" + newFilePathPrefix + filenameToUse)); + ImageIO.write(bufNewImg, imgExtension, new File(imageServerPath + "/" + newFileLocation + "." + imgExtension)); } catch (IllegalArgumentException e) { String errMsg = UtilProperties.getMessage(resource, "ScaleImage.one_parameter_is_null", locale) + e.toString(); Debug.logError(errMsg, module); @@ -205,7 +205,7 @@ public class ScaleImage { } /* write Return Result */ - String imageUrl = imageUrlPrefix + "/" + newFilePathPrefix + filenameToUse; + String imageUrl = imageUrlPrefix + "/" + newFileLocation + "." + imgExtension; imgUrlMap.put(sizeType, imageUrl); } // scaleImgMap 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=1081272&r1=1081271&r2=1081272&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 Mon Mar 14 07:16:36 2011 @@ -980,13 +980,19 @@ public class ProductServices { Locale locale = (Locale) context.get("locale"); if (UtilValidate.isNotEmpty(context.get("_uploadedFile_fileName"))) { - String imageFilenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.format"); + String imageFilenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.additionalviewsize.format"); String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context); String imageUrlPrefix = UtilProperties.getPropertyValue("catalog", "image.url.prefix"); FlexibleStringExpander filenameExpander = FlexibleStringExpander.getInstance(imageFilenameFormat); - String id = productId + "_View_" + productContentTypeId.charAt(productContentTypeId.length() - 1); - String fileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "type", "additional", "id", id)); + String viewNumber = String.valueOf(productContentTypeId.charAt(productContentTypeId.length() - 1)); + String viewType = "additional" + viewNumber; + String id = productId; + if (imageFilenameFormat.endsWith("${id}")) { + id = productId + "_View_" + viewNumber; + viewType = "additional"; + } + String fileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "viewtype", viewType, "sizetype", "original")); String filePathPrefix = ""; String filenameToUse = fileLocation; if (fileLocation.lastIndexOf("/") != -1) { @@ -1007,8 +1013,18 @@ public class ProductServices { filenameToUse += "." + extension.getString("fileExtensionId"); } - File file = new File(imageServerPath + "/" + filePathPrefix + filenameToUse); - + /* 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); + } + } + File file = new File(imageServerPath + "/" + fileLocation + "." + extension.getString("fileExtensionId")); try { RandomAccessFile out = new RandomAccessFile(file, "rw"); out.write(imageData.array()); @@ -1024,7 +1040,6 @@ public class ProductServices { } /* scale Image in different sizes */ - String viewNumber = String.valueOf(productContentTypeId.charAt(productContentTypeId.length() - 1)); Map<String, Object> resultResize = FastMap.newInstance(); try { resultResize.putAll(ScaleImage.scaleImageInAllSize(context, filenameToUse, "additional", viewNumber)); @@ -1038,7 +1053,7 @@ public class ProductServices { "ProductImageViewParsingError", UtilMisc.toMap("errorString", e.toString()), locale)); } - String imageUrl = imageUrlPrefix + "/" + filePathPrefix + filenameToUse; + String imageUrl = imageUrlPrefix + "/" + fileLocation + "." + extension.getString("fileExtensionId"); /* store the imageUrl version of the image, for backwards compatibility with code that does not use scaled versions */ Map<String, Object> result = addImageResource(dispatcher, delegator, context, imageUrl, productContentTypeId); |
Free forum by Nabble | Edit this page |