This is an automated email from the ASF dual-hosted git repository.

mbrohl pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new e87b199483 Improved: Refactored DataResourceWorker getMimeType 
(OFBIZ-9403)
e87b199483 is described below

commit e87b1994836ca88c3ba04fadef33f7bb8e828511
Author: Cheng Hu Shan <cheng-hu.s...@ecomify.de>
AuthorDate: Thu Jul 27 16:22:49 2023 +0200

    Improved: Refactored DataResourceWorker getMimeType (OFBIZ-9403)
---
 .../ofbiz/content/data/DataResourceWorker.java     | 67 ++++++++++++++++------
 1 file changed, 48 insertions(+), 19 deletions(-)

diff --git 
a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
 
b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
index a606f72820..18bd81734b 100644
--- 
a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
+++ 
b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
@@ -107,6 +107,7 @@ public class DataResourceWorker implements 
org.apache.ofbiz.widget.content.DataR
 
     private static final String MODULE = DataResourceWorker.class.getName();
     private static final String ERR_RESOURCE = "ContentErrorUiLabels";
+    private static final String PROPERTY_RESOURCE = "content";
 
     /**
      * Traverses the DataCategory parent/child structure and put it in 
categoryNode. Returns non-null error string if there is an error.
@@ -377,35 +378,63 @@ public class DataResourceWorker implements 
org.apache.ofbiz.widget.content.DataR
         return b;
     }
 
+    /**
+     * Gets the MIME-Type from a given data resource, using the default value 
set in properties as fallback.
+     * @param dataResource
+     * @return MIME-Type
+     */
     public static String getMimeType(GenericValue dataResource) {
+        String defaultMimeType = 
EntityUtilProperties.getPropertyValue(PROPERTY_RESOURCE, "defaultMimeType", 
"application/octet-stream",
+                dataResource.getDelegator());
+        return getMimeType(dataResource, defaultMimeType);
+    }
+
+    /**
+     * Gets the MIME-Type from a given data resource.
+     * @param dataResource
+     * @param defaultMimeTypeId
+     * @return MIME-Type
+     */
+    public static String getMimeType(GenericValue dataResource, String 
defaultMimeTypeId) {
         String mimeTypeId = null;
         if (dataResource != null) {
             mimeTypeId = (String) dataResource.get("mimeTypeId");
             if (UtilValidate.isEmpty(mimeTypeId)) {
                 String fileName = (String) dataResource.get("objectInfo");
-                if (fileName != null && fileName.indexOf('.') > -1) {
-                    String fileExtension = 
fileName.substring(fileName.lastIndexOf('.') + 1);
-                    if (UtilValidate.isNotEmpty(fileExtension)) {
-                        GenericValue ext = null;
-                        try {
-                            ext = 
dataResource.getDelegator().findOne("FileExtension",
-                                    UtilMisc.toMap("fileExtensionId", 
fileExtension), false);
-                        } catch (GenericEntityException e) {
-                            Debug.logError(e, MODULE);
-                        }
-                        if (ext != null) {
-                            mimeTypeId = ext.getString("mimeTypeId");
-                        }
-                    }
-                }
+                mimeTypeId = getMimeType(dataResource.getDelegator(), 
fileName, defaultMimeTypeId);
+            }
+        }
+        return mimeTypeId;
+    }
+
+    /**
+     * Gets the MIME-Type from a given filename.
+     * @param delegator
+     * @param fileName
+     * @param defaultMimeTypeId
+     * @return MIME-Type
+     */
+    public static String getMimeType(Delegator delegator, String fileName, 
String defaultMimeTypeId) {
+        String mimeTypeId = null;
 
-                // check one last time
-                if (UtilValidate.isEmpty(mimeTypeId)) {
-                    // use a default mime type
-                    mimeTypeId = "application/octet-stream";
+        if (UtilValidate.isNotEmpty(fileName) && fileName.indexOf('.') > -1) {
+            String fileExtension = 
fileName.substring(fileName.lastIndexOf('.') + 1);
+            if (UtilValidate.isNotEmpty(fileExtension)) {
+                GenericValue ext = null;
+                try {
+                    ext = delegator.findOne("FileExtension", true, 
"fileExtensionId", fileExtension);
+                    if (ext != null) {
+                        mimeTypeId = ext.getString("mimeTypeId");
+                    }
+                } catch (GenericEntityException e) {
+                    Debug.logError(e, MODULE);
                 }
             }
         }
+        // check one last time, if we have to return a default mime type
+        if (UtilValidate.isEmpty(mimeTypeId) && 
UtilValidate.isNotEmpty(defaultMimeTypeId)) {
+            mimeTypeId = defaultMimeTypeId;
+        }
         return mimeTypeId;
     }
 

Reply via email to