Update of 
/var/cvs/contributions/CMSContainer/cmsc/utilities/src/java/com/finalist/util/http
In directory 
james.mmbase.org:/tmp/cvs-serv30432/cmsc/utilities/src/java/com/finalist/util/http

Modified Files:
        BulkUploadUtil.java 
Log Message:
CMSC-1324 - Uploading zipped images are not unzipped: zip-detection fails; Made 
existing methods stronger too.


See also: 
http://cvs.mmbase.org/viewcvs/contributions/CMSContainer/cmsc/utilities/src/java/com/finalist/util/http
See also: http://www.mmbase.org/jira/browse/CMSC-1324


Index: BulkUploadUtil.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer/cmsc/utilities/src/java/com/finalist/util/http/BulkUploadUtil.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- BulkUploadUtil.java 23 Dec 2008 06:11:01 -0000      1.16
+++ BulkUploadUtil.java 10 Mar 2009 13:06:26 -0000      1.17
@@ -94,7 +94,7 @@
             log.debug("contentType: " + binary.getContentType());
          }
 
-         if (isZipFile(binary)) {
+         if (isZipFile(binary.getContentType(), binary.getOriginalFileName())) 
{
             log.debug("unzipping content");
             nodes.addAll(createNodesInZip(manager, new 
ZipInputStream(binary.getInputStream())));
          } else {
@@ -116,26 +116,24 @@
       return nodes;
    }
 
-   private static boolean isZipFile(FormFile file) {
+   public static boolean isZipFile(String contentType, String fileName) {
 
       for (String element : ZIP_MIME_TYPES) {
-         if (element.equalsIgnoreCase(file.getContentType())) {
+         if (element.equalsIgnoreCase(contentType)) {
             return true;
          }
       }
-      return false;
-   }
 
-   private static boolean isZipFile(BinaryData binary) {
-
-      for (String element : ZIP_MIME_TYPES) {
-         if (element.equalsIgnoreCase(binary.getContentType())) {
+      //Sometimes browsers don't return a nice mime-type (for example 
application/octet-stream)
+      //So checking on extension might be a good idea too.
+      if (getExtension(fileName).equalsIgnoreCase(".zip")) {
             return true;
          }
-      }
+      
       return false;
    }
 
+
    private static Node createNode(NodeManager manager, String fileName, 
InputStream in, long length) {
       if (length > manager.getField("handle").getMaxLength()) {
          return null;
@@ -168,7 +166,7 @@
    private static List<Integer> getNodeList(Integer parentChannel, NodeManager 
manager, FormFile file, Cloud cloud) {
       List<Integer> nodes = null;
       try {
-         if (isZipFile(file)) {
+         if (isZipFile(file.getContentType(), file.getFileName())) {
 
             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
             InputStream in = file.getInputStream();
@@ -220,6 +218,9 @@
                }
                continue;
             }
+            if (log.isDebugEnabled()) {
+               log.debug("reading file (from ZIP): '" + entry.getName() + "'");
+            }
             count++;
             // create temp file for zip entry, create a node from it and
             // remove the temp file
@@ -316,14 +317,21 @@
       }
    }
 
-   private static boolean isImage(String fileName) {
+   public static boolean isImage(String fileName) {
+      if (StringUtils.isBlank(fileName)) {
+         return false;
+      }
       if (supportedImages == null) {
          initSupportedImages();
       }
-      return fileName != null && 
supportedImages.contains(getExtension(fileName).toLowerCase());
+      
+      return supportedImages.contains(getExtension(fileName).toLowerCase());
    }
 
-   private static String getExtension(String fileName) {
+   public static String getExtension(String fileName) {
+      if (StringUtils.isBlank(fileName)) {
+         return null;
+      }
       int index = fileName.lastIndexOf('.');
       if (index < 0) {
          return null;
@@ -358,5 +366,10 @@
       System.out.println(isImage(getExtension("test.bummer")));
       System.out.println(isImage(""));
       System.out.println(isImage(" "));
+
+      //Also test the isZipFile method
+      System.out.println(isZipFile("content","helloworld.zip")); //Should be 
true
+      
System.out.println(isZipFile("application/x-zip-compressed","helloworld.zipper"));
 //Should be true
+      System.out.println(isZipFile("content","helloworld.zipper")); //Should 
be false
    }
 }
\ No newline at end of file
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to