From a60a6133b4342ff21089f36bfde0a4b6343afe71 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 16 Oct 2014 17:49:17 +0200 Subject: [PATCH] Add upload support files support --- src/sunstone/public/js/plugins/support-tab.js | 123 +++++++++++++++++- src/sunstone/routes/support.rb | 32 +++++ 2 files changed, 153 insertions(+), 2 deletions(-) diff --git a/src/sunstone/public/js/plugins/support-tab.js b/src/sunstone/public/js/plugins/support-tab.js index 174015ebd1..f2f3aaae74 100644 --- a/src/sunstone/public/js/plugins/support-tab.js +++ b/src/sunstone/public/js/plugins/support-tab.js @@ -15,7 +15,7 @@ //------------------------------------------------------------------------- // var support_interval_function; - +var $upload_support_file; var create_support_request_wizard_html = '
' + '
' + @@ -171,6 +171,12 @@ var support_actions = { } }); } + }, + "Support.upload" : { + type: "single", + call: function() { + $upload_support_file.foundation("reveal", "open"); + } } } @@ -181,10 +187,17 @@ var support_buttons = { text: '', alwaysActive: true }, + "Support.upload" : { + type: "action", + layout: "main", + text: ' '+tr("Upload a file"), + custom_classes: "only-right-info" + }, "Support.signout" : { type: "action", layout: "main", - text: "Sign out of Commercial Support", + text: '', + tip: "Sign out of Commercial Support", alwaysActive: true }, "Support.create_dialog" : { @@ -310,6 +323,105 @@ var support_tab = { } } +function setup_upload_support_file_dialog() { + dialogs_context.append('
'); + $upload_support_file = $('#upload_support_file',dialogs_context); + var dialog = $upload_support_file; + + dialog.html('\ +
\ +
\ +

'+tr("Upload File")+'

\ +
\ +
\ + \ +
\ +
\ + \ +
\ +
\ +
\ +
\ + \ +
\ +
\ + \ + ×\ + '); + + dialog.addClass("reveal-modal").attr("data-reveal", ""); + $vnc_dialog.foundation(); + + var uploader = new Resumable({ + target: '/upload_chunk', + chunkSize: 10*1024*1024, + maxFiles: 1, + testChunks: false, + query: { + csrftoken: csrftoken + } + }); + + uploader.assignBrowse($('#support_file-uploader-input')); + + var fileName = ''; + var file_input = false; + + uploader.on('fileAdded', function(file){ + $(".upload_support_file_form_button").removeAttr("disabled"); + fileName = file.fileName; + file_input = fileName; + }); + + uploader.on('uploadStart', function() { + $(".upload_support_file_form_button").attr("disabled", "disabled"); + $('.support_upload_progress_bars').append('
\ +
\ + '+tr("Uploading...")+'\ +
\ +
\ +
\ + \ +
\ +
'+fileName+'
\ +
\ +
'); + }); + + uploader.on('progress', function() { + $('span.meter', $('div[id="'+fileName+'progressBar"]')).css('width', uploader.progress()*100.0+'%') + }); + + uploader.on('fileSuccess', function(file) { + $('div[id="'+fileName+'-info"]').text(tr('Registering in OpenNebula')); + $.ajax({ + url: '/support/request/' + $("#submit_support_comment").data("request_id") + '/upload', + type: "POST", + data: { + csrftoken: csrftoken, + file: fileName, + tempfile: file.uniqueIdentifier + }, + success: function(){ + notifyMessage("File uploaded correctly"); + $('div[id="'+fileName+'progressBar"]').remove(); + Sunstone.runAction("Support.refresh"); + $upload_support_file.foundation('reveal', 'close'); + }, + error: function(response){ + onError({}, OpenNebula.Error(response)); + $('div[id="'+fileName+'progressBar"]').remove(); + } + }); + }); + + $("#upload_support_file_form").on("submit", function(){ + uploader.upload(); + $upload_support_file.foundation("reveal", "close") + return false; + }) +} + function initialize_create_support_request_dialog() { $('#create_support_request_form_wizard').foundation(); @@ -428,6 +540,11 @@ function updateSupportInfo(request, response){ }) } + html += '
\ +
\ +
\ +
'; + html += '
\
\
\ @@ -498,6 +615,8 @@ $(document).ready(function(){ var tab_name = 'support-tab'; if (Config.isTabEnabled(tab_name)) { + setup_upload_support_file_dialog(); + dataTable_support = $("#dataTable_support", main_tabs_context).dataTable({ "bSortClasses" : false, "bDeferRender": true, diff --git a/src/sunstone/routes/support.rb b/src/sunstone/routes/support.rb index 3b43e77bbf..c0b9375a34 100644 --- a/src/sunstone/routes/support.rb +++ b/src/sunstone/routes/support.rb @@ -197,6 +197,38 @@ post '/support/request/:id/action' do end +post '/support/request/:id/upload' do + check_zendesk_api_gem + + tmpfile = nil + + name = params[:tempfile] + + if !name + [500, OpenNebula::Error.new("There was a problem uploading the file, " \ + "please check the permissions on the file").to_json] + else + tmpfile = File.join(Dir.tmpdir, name) + + zrequest = zendesk_client.requests.find(:id => params[:id]) + # TODO check error + + comment = ZendeskAPI::Request::Comment.new(zendesk_client, {"value" => name}) + comment.uploads << tmpfile + + zrequest.comment = comment + zrequest.save + + one_zrequest = { + "REQUEST" => zrequest_to_one(zrequest) + } + + FileUtils.rm(tmpfile) + + [201, JSON.pretty_generate(one_zrequest)] + end +end + post '/support/credentials' do check_zendesk_api_gem