0012958: Improve upload handling in filemanager
authorMichael Spahn <m.spahn@metaways.de>
Tue, 11 Apr 2017 15:59:46 +0000 (17:59 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 19 Apr 2017 11:57:09 +0000 (13:57 +0200)
https://forge.tine20.org/view.php?id=12958

Change-Id: Iafef63da89a324e1b499b2fe4e9e949d6f748e02
Reviewed-on: http://gerrit.tine20.com/customers/4540
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Filemanager/js/Model.js
tine20/Filemanager/js/NodeGridPanel.js
tine20/Filemanager/js/NodeTreePanel.js

index 3b52a16..02d9f0e 100644 (file)
@@ -268,7 +268,7 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
                 if(itemData.type == 'folder') {
                     containsFolder = true;
                 }
-            };
+            }
             
             var method = "Filemanager.copyNodes",
                 message = app.i18n._('Copying data .. {0}');
@@ -285,8 +285,7 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
                     method: method
             };
             
-        }
-        else {
+        } else {
             message = app.i18n._('Copying data .. {0}');
             if(params.method == 'Filemanager.moveNodes') {
                 message = app.i18n._('Moving data .. {0}');
@@ -369,6 +368,7 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
      */
     createNode: function(params, uploadKey, addToGridStore) {
         var app = Tine.Tinebase.appMgr.get('Filemanager'),
+            me = this,
             grid = app.getMainScreen().getCenterPanel(),
             gridStore = grid.getStore();
         
@@ -378,10 +378,12 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
         params.addToGridStore = addToGridStore;
         
         var onSuccess = (function(result, request){
-            
+
             var nodeData = Ext.util.JSON.decode(response.responseText),
                 fileRecord = Tine.Tinebase.uploadManager.upload(this.uploadKey);
-            
+
+            fileRecord.on('update', me.onUploadUpdate.createDelegate(me));
+
             if(addToGridStore) {
                 var recordToRemove = gridStore.query('name', fileRecord.get('name'));
                 if(recordToRemove.items[0]) {
@@ -422,34 +424,37 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
      * 
      * @param {} params Request parameters
      * @param [] uploadKeyArray
-     * @param Boolean addToGridStore 
+     * @param Boolean addToGridStore
      */
-    createNodes: function(params, uploadKeyArray, addToGridStore) {
+    createNodes: function (params, uploadKeyArray, addToGridStore) {
         var app = Tine.Tinebase.appMgr.get('Filemanager'),
             grid = app.getMainScreen().getCenterPanel(),
+            me = this,
             gridStore = grid.store;
-        
+
         params.application = 'Filemanager';
         params.method = 'Filemanager.createNodes';
         params.uploadKeyArray = uploadKeyArray;
         params.addToGridStore = addToGridStore;
-        
-        
-        var onSuccess = (function(response, request){
-            
+
+
+        var onSuccess = (function (response, request) {
+
             var nodeData = Ext.util.JSON.decode(response.responseText);
-            
-            for(var i=0; i<this.uploadKeyArray.length; i++) {
+
+            for (var i = 0; i < this.uploadKeyArray.length; i++) {
                 var fileRecord = Tine.Tinebase.uploadManager.upload(this.uploadKeyArray[i]);
-                
-                if(addToGridStore) {
+
+                Tine.Tinebase.uploadManager.getUpload(this.uploadKeyArray[i]).on('update', me.onUploadUpdate.createDelegate(me));
+
+                if (addToGridStore) {
                     fileRecord = Tine.Filemanager.fileRecordBackend.updateNodeRecord(nodeData[i], fileRecord);
                     var nodeRecord = new Tine.Filemanager.Model.Node(nodeData[i]);
-                    
+
                     nodeRecord.fileRecord = fileRecord;
-                    
+
                     var existingRecordIdx = gridStore.find('name', fileRecord.get('name'));
-                    if(existingRecordIdx > -1) {
+                    if (existingRecordIdx > -1) {
                         gridStore.removeAt(existingRecordIdx);
                         gridStore.insert(existingRecordIdx, nodeRecord);
                     } else {
@@ -457,20 +462,20 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
                     }
                 }
             }
-            
+
         }).createDelegate({uploadKeyArray: uploadKeyArray, addToGridStore: addToGridStore});
-        
-        var onFailure = (function(response, request) {
-            
+
+        var onFailure = (function (response, request) {
+
             var nodeData = Ext.util.JSON.decode(response.responseText),
                 request = Ext.util.JSON.decode(request.jsonData);
-            
+
             nodeData.data.uploadKeyArray = this.uploadKeyArray;
             nodeData.data.addToGridStore = this.addToGridStore;
             Tine.Filemanager.fileRecordBackend.handleRequestException(nodeData.data, request);
-            
+
         }).createDelegate({uploadKeyArray: uploadKeyArray, addToGridStore: addToGridStore});
-        
+
         Ext.Ajax.request({
             params: params,
             timeout: 300000, // 5 minutes
@@ -478,10 +483,100 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
             success: onSuccess || Ext.emptyFn,
             failure: onFailure || Ext.emptyFn
         });
-        
-        
     },
-    
+
+    /**
+     * Is fired if there is any change for an uploading file
+     *
+     * Is bind to the record proxy scope
+     */
+    onUploadUpdate: function (change, upload, fileRecord) {
+        var app = Tine.Tinebase.appMgr.get('Filemanager'),
+            grid = app.getMainScreen().getCenterPanel(),
+            rowsToUpdate = grid.store.query('name', fileRecord.get('name'));
+
+        if (change === 'uploadstart') {
+            Tine.Tinebase.uploadManager.onUploadStart();
+        } else if (change === 'uploadfailure') {
+            grid.onUploadFail();
+        }
+
+        if (rowsToUpdate.get(0)) {
+            if (change === 'uploadcomplete') {
+                this.onUploadComplete.call(grid, this, upload, fileRecord);
+            } else if (change === 'uploadfinished') {
+                rowsToUpdate.get(0).set('size', fileRecord.get('size'));
+                rowsToUpdate.get(0).set('contenttype', fileRecord.get('contenttype'));
+            }
+
+            rowsToUpdate.get(0).afterEdit();
+            rowsToUpdate.get(0).commit(false);
+        }
+    },
+
+    /**
+     * updating fileRecord after creating node
+     *
+     * @param response
+     * @param request
+     * @param upload
+     */
+    onNodeCreated: function (response, request, upload) {
+        var record = Ext.util.JSON.decode(response.responseText);
+
+        var fileRecord = upload.fileRecord;
+        fileRecord.beginEdit();
+        fileRecord.set('contenttype', record.contenttype);
+        fileRecord.set('created_by', Tine.Tinebase.registry.get('currentAccount'));
+        fileRecord.set('creation_time', record.creation_time);
+        fileRecord.set('revision', record.revision);
+        fileRecord.set('last_modified_by', record.last_modified_by);
+        fileRecord.set('last_modified_time', record.last_modified_time);
+        fileRecord.set('name', record.name);
+        fileRecord.set('path', record.path);
+        fileRecord.set('status', 'complete');
+        fileRecord.set('progress', 100);
+        fileRecord.commit(false);
+
+        upload.fireEvent('update', 'uploadfinished', upload, fileRecord);
+
+        var allRecordsComplete = true;
+        var storeItems = this.getStore().getRange();
+        for (var i = 0; i < storeItems.length; i++) {
+            if (storeItems[i].get('status') && storeItems[i].get('status') !== 'complete') {
+                allRecordsComplete = false;
+                break;
+            }
+        }
+
+        if (allRecordsComplete) {
+            this.pagingToolbar.refresh.enable();
+        }
+    },
+
+    /**
+     * copies uploaded temporary file to target location
+     *
+     * @param proxy  {Tine.Filemanager.fileRecordBackend}
+     * @param upload  {Ext.ux.file.Upload}
+     * @param file  {Ext.ux.file.Upload.file}
+     */
+    onUploadComplete: function(proxy, upload, file) {
+        Ext.Ajax.request({
+            timeout: 10*60*1000, // Overriding Ajax timeout - important!
+            params: {
+                method: 'Filemanager.createNode',
+                filename: upload.id,
+                type: 'file',
+                tempFileId: file.get('id'),
+                forceOverwrite: true
+            },
+            success: proxy.onNodeCreated.createDelegate(this, [upload], true),
+            failure: proxy.onNodeCreated.createDelegate(this, [upload], true)
+        });
+
+    },
+
     /**
      * exception handler for this proxy
      * 
@@ -498,12 +593,10 @@ Tine.Filemanager.fileRecordBackend =  new Tine.Tinebase.data.RecordProxy({
         
         for(var field in nodeData) {
             nodeRecord.set(field, nodeData[field]);
-        };
+        }
         
         return nodeRecord;
     }
-    
-
 });
 
 
index aafea75..4f1c1e0 100644 (file)
@@ -107,7 +107,6 @@ Tine.Filemanager.NodeGridPanel = Ext.extend(Tine.widgets.grid.GridPanel, {
 
         Tine.Filemanager.NodeGridPanel.superclass.initComponent.call(this);
         this.getStore().on('load', this.onLoad.createDelegate(this));
-        Tine.Tinebase.uploadManager.on('update', this.onUpdate);
     },
 
     /**
@@ -722,21 +721,6 @@ Tine.Filemanager.NodeGridPanel = Ext.extend(Tine.widgets.grid.GridPanel, {
     },
 
     /**
-     * on upload failure
-     *
-     * @private
-     */
-    onUploadFail: function () {
-        Ext.MessageBox.alert(
-            i18n._('Upload Failed'),
-            i18n._('Could not upload file. Filesize could be too big. Please notify your Administrator.')
-        ).setIcon(Ext.MessageBox.ERROR);
-
-        var grid = this;
-        grid.pagingToolbar.refresh.enable();
-    },
-
-    /**
      * on remove handler
      *
      * @param {} button
@@ -772,77 +756,6 @@ Tine.Filemanager.NodeGridPanel = Ext.extend(Tine.widgets.grid.GridPanel, {
     },
 
     /**
-     * copies uploaded temporary file to target location
-     *
-     * @param upload  {Ext.ux.file.Upload}
-     * @param file  {Ext.ux.file.Upload.file}
-     */
-    onUploadComplete: function(upload, file) {
-        var grid = this;
-
-        // check if we are responsible for the upload
-        if (upload.fmDirector != grid) return;
-
-        // $filename, $type, $tempFileId, $forceOverwrite
-        Ext.Ajax.request({
-            timeout: 10*60*1000, // Overriding Ajax timeout - important!
-            params: {
-                method: 'Filemanager.createNode',
-                filename: upload.id,
-                type: 'file',
-                tempFileId: file.get('id'),
-                forceOverwrite: true
-            },
-            success: grid.onNodeCreated.createDelegate(this, [upload], true),
-            failure: grid.onNodeCreated.createDelegate(this, [upload], true)
-        });
-
-    },
-
-    /**
-     * TODO: move to Upload class or elsewhere??
-     * updating fileRecord after creating node
-     *
-     * @param response
-     * @param request
-     * @param upload
-     */
-    onNodeCreated: function(response, request, upload) {
-        var record = Ext.util.JSON.decode(response.responseText);
-
-        var fileRecord = upload.fileRecord;
-        fileRecord.beginEdit();
-        fileRecord.set('contenttype', record.contenttype);
-        fileRecord.set('created_by', Tine.Tinebase.registry.get('currentAccount'));
-        fileRecord.set('creation_time', record.creation_time);
-        fileRecord.set('revision', record.revision);
-        fileRecord.set('last_modified_by', record.last_modified_by);
-        fileRecord.set('last_modified_time', record.last_modified_time);
-        fileRecord.set('name', record.name);
-        fileRecord.set('path', record.path);
-        fileRecord.set('status', 'complete');
-        fileRecord.set('progress', 100);
-        fileRecord.commit(false);
-
-        upload.fireEvent('update', 'uploadfinished', upload, fileRecord);
-
-        var grid = this;
-
-        var allRecordsComplete = true;
-        var storeItems = grid.getStore().getRange();
-        for(var i=0; i<storeItems.length; i++) {
-            if(storeItems[i].get('status') && storeItems[i].get('status') !== 'complete') {
-                allRecordsComplete = false;
-                break;
-            }
-        }
-
-        if(allRecordsComplete) {
-            grid.pagingToolbar.refresh.enable();
-        }
-    },
-
-    /**
      * upload new file and add to store
      *
      * @param {ux.BrowsePlugin} fileSelector
@@ -979,39 +892,6 @@ Tine.Filemanager.NodeGridPanel = Ext.extend(Tine.widgets.grid.GridPanel, {
     },
 
     /**
-     * upload update handler
-     *
-     * @param change {String} kind of change
-     * @param upload {Ext.ux.file.Upload} upload
-     * @param fileRecord {file} fileRecord
-     *
-     */
-    onUpdate: function(change, upload, fileRecord) {
-        var app = Tine.Tinebase.appMgr.get('Filemanager'),
-            grid = app.getMainScreen().getCenterPanel(),
-            rowsToUpdate = grid.getStore().query('name', fileRecord.get('name'));
-
-        if(change == 'uploadstart') {
-            Tine.Tinebase.uploadManager.onUploadStart();
-        }
-        else if(change == 'uploadfailure') {
-            grid.onUploadFail();
-        }
-
-        if(rowsToUpdate.get(0)) {
-            if(change == 'uploadcomplete') {
-                grid.onUploadComplete(upload, fileRecord);
-            }
-            else if(change == 'uploadfinished') {
-                rowsToUpdate.get(0).set('size', fileRecord.get('size'));
-                rowsToUpdate.get(0).set('contenttype', fileRecord.get('contenttype'));
-            }
-            rowsToUpdate.get(0).afterEdit();
-            rowsToUpdate.get(0).commit(false);
-        }
-    },
-
-    /**
      * init grid drop target
      *
      * @TODO DRY cleanup
index 3297ad3..8004b4f 100644 (file)
@@ -70,10 +70,6 @@ Ext.extend(Tine.Filemanager.NodeTreePanel, Tine.widgets.container.TreePanel, {
             this.app = Tine.Tinebase.appMgr.get('Filemanager');
         }
 
-        if (!this.readOnly) {
-            Tine.Tinebase.uploadManager.on('update', this.onUpdate);
-        }
-
         if (this.readOnly) {
             this.hasContextMenu = false;
             this.enableDD = false;
@@ -628,7 +624,7 @@ Ext.extend(Tine.Filemanager.NodeTreePanel, Tine.widgets.container.TreePanel, {
             nodeRecord: newNodeRecord,
             account_grants: nodeData.account_grants,
             id: nodeData.id
-        })
+        });
 
         newNode.attributes.nodeRecord.beginEdit();
         newNode.attributes.nodeRecord.set('path', nodeData.path);
@@ -640,86 +636,6 @@ Ext.extend(Tine.Filemanager.NodeTreePanel, Tine.widgets.container.TreePanel, {
     },
 
     /**
-     * TODO: move to Upload class or elsewhere??
-     * updating fileRecord after creating node
-     *
-     * @param response
-     * @param request
-     * @param upload
-     */
-    onNodeCreated: function(response, request, upload) {
-
-        var app = Tine.Tinebase.appMgr.get('Filemanager'),
-        grid = app.getMainScreen().getCenterPanel();
-
-        var record = Ext.util.JSON.decode(response.responseText);
-
-        var fileRecord = upload.fileRecord;
-        fileRecord.beginEdit();
-        fileRecord.set('contenttype', record.contenttype);
-        fileRecord.set('created_by', Tine.Tinebase.registry.get('currentAccount'));
-        fileRecord.set('creation_time', record.creation_time);
-        fileRecord.set('revision', record.revision);
-        fileRecord.set('last_modified_by', record.last_modified_by);
-        fileRecord.set('last_modified_time', record.last_modified_time);
-        fileRecord.set('status', 'complete');
-        fileRecord.set('progress', 100);
-        fileRecord.set('name', record.name);
-        fileRecord.set('path', record.path);
-        fileRecord.commit(false);
-
-        upload.fireEvent('update', 'uploadfinished', upload, fileRecord);
-
-        grid.pagingToolbar.refresh.enable();
-
-    },
-
-    /**
-     * copies uploaded temporary file to target location
-     *
-     * @param upload    {Ext.ux.file.Upload}
-     * @param file  {Ext.ux.file.Upload.file}
-     */
-    onUploadComplete: function(upload, file) {
-        if (this.readOnly) {
-            return;
-        }
-
-        var app = Tine.Tinebase.appMgr.get('Filemanager'),
-            treePanel = app.getMainScreen().getWestPanel().getContainerTreePanel();
-
-     // check if we are responsible for the upload
-        if (upload.fmDirector != treePanel) return;
-
-        // $filename, $type, $tempFileId, $forceOverwrite
-        Ext.Ajax.request({
-            timeout: 10*60*1000, // Overriding Ajax timeout - important!
-            params: {
-                method: 'Filemanager.createNode',
-                filename: upload.id,
-                type: 'file',
-                tempFileId: file.get('id'),
-                forceOverwrite: true
-            },
-            success: treePanel.onNodeCreated.createDelegate(this, [upload], true),
-            failure: treePanel.onNodeCreated.createDelegate(this, [upload], true)
-        });
-
-    },
-
-    /**
-     * on upload failure
-     *
-     * @private
-     */
-    onUploadFail: function () {
-        Ext.MessageBox.alert(
-            i18n._('Upload Failed'),
-            i18n._('Could not upload file. Filesize could be too big. Please notify your Administrator.')
-        ).setIcon(Ext.MessageBox.ERROR);
-    },
-
-    /**
      * add folder handler
      */
     onFolderAdd: function(nodeData) {
@@ -763,41 +679,6 @@ Ext.extend(Tine.Filemanager.NodeTreePanel, Tine.widgets.container.TreePanel, {
     },
 
     /**
-     * upload update handler
-     *
-     * @param change {String} kind of change
-     * @param upload {Ext.ux.file.Upload} upload
-     * @param fileRecord {file} fileRecord
-     *
-     */
-    onUpdate: function(change, upload, fileRecord) {
-
-        var app = Tine.Tinebase.appMgr.get('Filemanager'),
-            grid = app.getMainScreen().getCenterPanel(),
-            treePanel = app.getMainScreen().getWestPanel().getContainerTreePanel(),
-            rowsToUpdate = grid.getStore().query('name', fileRecord.get('name'));
-
-        if(change == 'uploadstart') {
-            Tine.Tinebase.uploadManager.onUploadStart();
-        }
-        else if(change == 'uploadfailure') {
-            treePanel.onUploadFail();
-        }
-
-        if(rowsToUpdate.get(0)) {
-            if(change == 'uploadcomplete') {
-                treePanel.onUploadComplete(upload, fileRecord);
-            }
-            else if(change == 'uploadfinished') {
-                rowsToUpdate.get(0).set('size', upload.fileSize);
-                rowsToUpdate.get(0).set('contenttype', fileRecord.get('contenttype'));
-            }
-            rowsToUpdate.get(0).afterEdit();
-            rowsToUpdate.get(0).commit(false);
-        }
-    },
-
-    /**
      * handels tree drop of object from outside the browser
      *
      * @param fileSelector