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

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-ui.git


The following commit(s) were added to refs/heads/master by this push:
     new 94ad2d04 support for more binary archives
94ad2d04 is described below

commit 94ad2d049317329c1b45c26246fa2f0c89bef495
Author: Alex Heneveld <[email protected]>
AuthorDate: Mon Jun 17 17:37:06 2024 +0100

    support for more binary archives
---
 .../utils/catalog-uploader/catalog-uploader.js     | 62 ++++++++++++----------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/ui-modules/utils/catalog-uploader/catalog-uploader.js 
b/ui-modules/utils/catalog-uploader/catalog-uploader.js
index d7480a35..f9141de9 100644
--- a/ui-modules/utils/catalog-uploader/catalog-uploader.js
+++ b/ui-modules/utils/catalog-uploader/catalog-uploader.js
@@ -139,25 +139,36 @@ export function catalogUploaderDirective($compile, 
brooklynCatalogUploader) {
  * Encapsulate the logic to validate files to upload to the catalog.
  */
 export function catalogUploaderService($q, catalogApi) {
-    let extensions = {
-        'bom': {
+    function getFileTypeProperties(fn) {
+      if (!fn) return null;
+      const fnl = fn.toLowerCase();
+      if (["bom","yml","yaml"].find(ext => fnl.endsWith("."+ext))) {
+        return {
+          http: {
             headers: {'Content-Type': 'application/yaml'}
-        },
-        'yml': {
-            headers: {'Content-Type': 'application/yaml'}
-        },
-        'yaml': {
-            headers: {'Content-Type': 'application/yaml'}
-        },
-        'zip' : {
-            headers: {'Content-Type': 'application/x-zip'},
-            transformRequest: angular.identity
-        },
-        'jar' : {
+          }
+        };
+      }
+      if (fnl.endsWith(".jar")) {
+        return {
+          binary: true,
+          http: {
             headers: {'Content-Type': 'application/x-jar'},
-            transformRequest: angular.identity
-        }
-    };
+            transformRequest: angular.identity 
+          }
+        };
+      }
+      if (fnl.endsWith(".zip") || fnl.endsWith("ar")) {  // support other 
archive types, tar, csar, etc
+        return {
+          binary: true,
+          http: { 
+            headers: {'Content-Type': 'application/x-zip'},
+            transformRequest: angular.identity 
+          }
+        };
+      }
+      return null;
+    }
 
     return {
         /**
@@ -181,19 +192,15 @@ export function catalogUploaderService($q, catalogApi) {
     function upload(file) {
         let defer = $q.defer();
 
-        if (new RegExp('^.*\.(' + Object.keys(extensions).join('|') + 
')$').test(file.name)) {
-            Object.keys(extensions).forEach((extension)=> {
-                if (!new RegExp('^.*\.(' + extension + ')$').test(file.name)) {
-                    return;
-                }
+        const options = getFileTypeProperties(file.name);
 
-                let options = extensions[extension];
+        if (options!=null) {
                 let reader = new FileReader();
                 reader.addEventListener('load', ()=> {
                     try {
                         let rawData = new Uint8Array(reader.result);
-                        let data = ['zip', 'jar'].indexOf(extension) > -1 ? 
rawData : String.fromCharCode.apply(null, rawData);
-                        catalogApi.create(data, {}, options).then((response)=> 
{
+                        let data = options.binary ? rawData : 
String.fromCharCode.apply(null, rawData);
+                        catalogApi.create(data, {}, 
options.http).then((response)=> {
                             defer.resolve(response);
                         }).catch((response)=> {
                             defer.reject('Cannot upload item to the catalog: ' 
+ response.error.message);
@@ -203,9 +210,8 @@ export function catalogUploaderService($q, catalogApi) {
                     }
                 }, false);
                 reader.readAsArrayBuffer(file);
-            });
         } else {
-            defer.reject('Unsupported file type. Please upload only files with 
the following extensions: ' + Object.keys(extensions).map((extension)=>('*.' + 
extension)).join(', '));
+            defer.reject("Unsupported file type. Supported types include BOM, 
YAML, and ZIP. The extension is significant.");
         }
 
         return defer.promise;
@@ -225,4 +231,4 @@ export function customOnChangeDirective() {
             });
         }
     };
-}
\ No newline at end of file
+}

Reply via email to