From d53335bbbfb0c4b95ca4758e227083ff19e006ea Mon Sep 17 00:00:00 2001 From: nathangray Date: Tue, 18 Apr 2017 09:34:22 -0600 Subject: [PATCH] File upload bugfixes: - Javascript error uploading files with ' in their name - uploading the same file twice to a customfield did not show the file uploaded properly --- api/js/etemplate/et2_widget_file.js | 12 ++++----- api/js/etemplate/et2_widget_link.js | 2 +- api/js/etemplate/et2_widget_vfs.js | 38 ++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/api/js/etemplate/et2_widget_file.js b/api/js/etemplate/et2_widget_file.js index 1f0948e627..3b0a19f40e 100644 --- a/api/js/etemplate/et2_widget_file.js +++ b/api/js/etemplate/et2_widget_file.js @@ -544,7 +544,7 @@ var et2_file = (function(){ "use strict"; return et2_inputWidget.extend( if(this.progress) { var fileName = file.fileName || 'file'; - var status = jQuery("
  • "+fileName + var status = jQuery("
  • "+fileName +"

  • ") .appendTo(this.progress); jQuery("div.remove",status).on('click', file, jQuery.proxy(this.cancel,this)); @@ -561,7 +561,7 @@ var et2_file = (function(){ "use strict"; return et2_inputWidget.extend( _fileProgress: function(file) { if(this.progress) { - jQuery("li[data-file='"+file.fileName+"'] > span.progressBar > p").css("width", Math.ceil(file.progress()*100)+"%"); + jQuery("li[data-file='"+file.fileName.replace(/'/g, '"')+"'] > span.progressBar > p").css("width", Math.ceil(file.progress()*100)+"%"); } return true; @@ -584,7 +584,7 @@ var et2_file = (function(){ "use strict"; return et2_inputWidget.extend( if(typeof response.response[0].data[key] == "string") { // Message from server - probably error - jQuery("[data-file='"+name+"']",this.progress) + jQuery("[data-file='"+name.replace(/'/g, '"')+"']",this.progress) .addClass("error") .css("display", "block") .text(response.response[0].data[key]); @@ -599,14 +599,14 @@ var et2_file = (function(){ "use strict"; return et2_inputWidget.extend( } if(this.progress) { - jQuery("[data-file='"+name+"']",this.progress).addClass("message success"); + jQuery("[data-file='"+name.replace(/'/g, '"')+"']",this.progress).addClass("message success"); } } } } else if (this.progress) { - jQuery("[data-file='"+name+"']",this.progress) + jQuery("[data-file='"+name.replace(/'/g, '"')+"']",this.progress) .addClass("ui-state-error") .css("display", "block") .text(this.egw().lang("Server error")); @@ -640,7 +640,7 @@ var et2_file = (function(){ "use strict"; return et2_inputWidget.extend( if(this.options.value[key].name == file.fileName) { delete this.options.value[key]; - jQuery('[data-file="'+file.fileName+'"]',this.node).remove(); + jQuery('[data-file="'+file.fileName.replace(/'/g, '"')+'"]',this.node).remove(); return; } } diff --git a/api/js/etemplate/et2_widget_link.js b/api/js/etemplate/et2_widget_link.js index ddf65b9c0a..235922e19a 100644 --- a/api/js/etemplate/et2_widget_link.js +++ b/api/js/etemplate/et2_widget_link.js @@ -291,7 +291,7 @@ var et2_link_to = (function(){ "use strict"; return et2_inputWidget.extend( id: file, name: self.file_upload.options.value[file].name, type: self.file_upload.options.value[file].type, - remark: jQuery("li[file='"+self.file_upload.options.value[file].name+"'] > input", self.file_upload.progress) + remark: jQuery("li[file='"+self.file_upload.options.value[file].name.replace(/'/g, '"')+"'] > input", self.file_upload.progress) .filter(function() { return jQuery(this).attr("placeholder") != jQuery(this).val();}).val() }); } diff --git a/api/js/etemplate/et2_widget_vfs.js b/api/js/etemplate/et2_widget_vfs.js index 6cf90130d8..34f6b6b75c 100644 --- a/api/js/etemplate/et2_widget_vfs.js +++ b/api/js/etemplate/et2_widget_vfs.js @@ -720,7 +720,7 @@ var et2_vfsUpload = (function(){ "use strict"; return et2_file.extend( if(sender !== this && sender._type.indexOf('vfs') >= 0 ) { var value = sender.getValue && sender.getValue() || sender.options.value || {}; - var row = jQuery('[data-path="'+(value.path)+'"]',this.list); + var row = jQuery("[data-path='"+(value.path.replace(/'/g, '"'))+"']",this.list); if(sender._type === 'vfs-mime') { return jQuery('.icon',row).get(0) || null; @@ -750,9 +750,41 @@ var et2_vfsUpload = (function(){ "use strict"; return et2_file.extend( return extra; }, + /** + * A file upload is finished, update the UI + */ + finishUpload: function(file, response) { + var result = this._super.apply(this, arguments); + + if(typeof response == 'string') response = jQuery.parseJSON(response); + if(response.response[0] && typeof response.response[0].data.length == 'undefined') { + for(var key in response.response[0].data) { + var value = response.response[0].data[key]; + if(value && value.path) + { + this._addFile(value); + jQuery("[data-file='"+file.fileName.replace(/'/g, '"')+"']",this.progress).hide(); + } + } + } + return result; + }, + _addFile: function(file_data) { + if(jQuery("[data-path='"+file_data.path.replace(/'/g, '"')+"']").remove().length) + { + for(var child_index = this._children.length-1; child_index >= 0; child_index--) + { + var child = this._children[child_index]; + if(child.options.value.path === file_data.path) + { + this.removeChild(child); + child.free(); + } + } + } var row = jQuery(document.createElement("tr")) - .attr("data-path", file_data.path) + .attr("data-path", file_data.path.replace(/'/g, '"')) .attr("draggable", "true") .appendTo(this.list); var mime = jQuery(document.createElement("td")) @@ -782,7 +814,7 @@ var et2_vfsUpload = (function(){ "use strict"; return et2_file.extend( { egw.json("filemanager_ui::ajax_action", [ 'delete', - [row.attr('data-path')], + [row.attr('data-path').replace(/"/g, "'")], '' ], function(data) {