diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2016-04-27 22:28:56 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2016-04-28 22:22:40 +0300 |
commit | abca7a4b80d95a01edb0701bdf5fe395820820d8 (patch) | |
tree | 92d5379334bf3b850fcad07255882120a1b92748 | |
parent | f1e564f31a4b310fbd2bf773f675582a6fa6dc01 (diff) |
Fix attachment download/download to owncloud files
-rw-r--r-- | js/app.js | 1 | ||||
-rw-r--r-- | js/controller/messagecontroller.js | 96 | ||||
-rw-r--r-- | js/models/attachment.js | 7 | ||||
-rw-r--r-- | js/service/attachmentservice.js | 66 | ||||
-rw-r--r-- | js/templates/message-attachment.html | 2 | ||||
-rw-r--r-- | js/templates/message-attachments.html | 2 | ||||
-rw-r--r-- | js/views/app.js | 15 | ||||
-rw-r--r-- | js/views/message.js | 3 | ||||
-rw-r--r-- | js/views/messageattachment.js | 41 | ||||
-rw-r--r-- | js/views/messageattachments.js | 36 | ||||
-rwxr-xr-x | lib/controller/messagescontroller.php | 2 |
11 files changed, 194 insertions, 77 deletions
@@ -37,6 +37,7 @@ define(function(require) { require('controller/foldercontroller'); require('controller/messagecontroller'); require('service/accountservice'); + require('service/attachmentservice'); require('service/folderservice'); require('service/messageservice'); require('notification'); diff --git a/js/controller/messagecontroller.js b/js/controller/messagecontroller.js index 26e8394dd..67bed573a 100644 --- a/js/controller/messagecontroller.js +++ b/js/controller/messagecontroller.js @@ -126,66 +126,54 @@ define(function(require) { } /** + * @param {Account} account + * @param {Folder} folder * @param {number} messageId * @param {number} attachmentId - * @returns {undefined} + * @returns {Promise} */ - function saveAttachment(messageId, attachmentId) { + function saveAttachmentToFiles(account, folder, messageId, attachmentId) { + var defer = $.Deferred(); + var saveAll = _.isUndefined(attachmentId); + OC.dialogs.filepicker( t('mail', 'Choose a folder to store the attachment in'), function(path) { - // Loading feedback - var saveToFilesBtnSelector = '.attachment-save-to-cloud'; - if (typeof attachmentId !== 'undefined') { - saveToFilesBtnSelector = 'li[data-attachment-id="' + - attachmentId + '"] ' + saveToFilesBtnSelector; - } - $(saveToFilesBtnSelector) - .removeClass('icon-folder') - .addClass('icon-loading-small') - .prop('disabled', true); - - $.ajax( - OC.generateUrl( - 'apps/mail/accounts/{accountId}/' + - 'folders/{folderId}/messages/{messageId}/' + - 'attachment/{attachmentId}', - { - accountId: require('state').currentAccount.get('accountId'), - folderId: require('state').currentFolder.get('id'), - messageId: messageId, - attachmentId: attachmentId - }), { - data: { - targetPath: path - }, - type: 'POST', - success: function() { - if (typeof attachmentId === 'undefined') { - Radio.ui.trigger('error:show', t('mail', 'Attachments saved to Files.')); - } else { - Radio.ui.trigger('error:show', t('mail', 'Attachment saved to Files.')); - } - }, - error: function() { - if (typeof attachmentId === 'undefined') { - Radio.ui.trigger('error:show', t('mail', 'Error while saving attachments to Files.')); - } else { - Radio.ui.trigger('error:show', t('mail', 'Error while saving attachment to Files.')); - } - }, - complete: function() { - // Remove loading feedback again - $('.attachment-save-to-cloud') - .removeClass('icon-loading-small') - .addClass('icon-folder') - .prop('disabled', false); + var savingToFiles = Radio.message.request('save:cloud', account, + folder, messageId, attachmentId, path); + $.when(savingToFiles).done(function() { + if (saveAll) { + Radio.ui.trigger('error:show', t('mail', 'Attachments saved to Files.')); + } else { + Radio.ui.trigger('error:show', t('mail', 'Attachment saved to Files.')); } + defer.resolve(); }); - }, - false, - 'httpd/unix-directory', - true - ); + $.when(savingToFiles).fail(function() { + if (saveAll) { + Radio.ui.trigger('error:show', t('mail', 'Error while saving attachments to Files.')); + } else { + Radio.ui.trigger('error:show', t('mail', 'Error while saving attachment to Files.')); + } + defer.reject(); + }); + }, false, 'httpd/unix-directory', true); + + return defer.promise(); } -});
\ No newline at end of file + + /** + * @param {Account} account + * @param {Folder} folder + * @param {number} messageId + * @returns {Promise} + */ + function saveAttachmentsToFiles(account, folder, messageId) { + return saveAttachmentToFiles(account, folder, messageId); + } + + return { + saveAttachmentToFiles: saveAttachmentToFiles, + saveAttachmentsToFiles: saveAttachmentsToFiles + }; +}); diff --git a/js/models/attachment.js b/js/models/attachment.js index 86bbefdea..01f1519d4 100644 --- a/js/models/attachment.js +++ b/js/models/attachment.js @@ -5,20 +5,23 @@ * later. See the COPYING file. * * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @copyright Christoph Wurst 2015 + * @copyright Christoph Wurst 2015, 2016 */ define(function(require) { 'use strict'; var Backbone = require('backbone'); + var _ = require('underscore'); /** * @class Attachment */ var Attachment = Backbone.Model.extend({ initialize: function() { - this.set('id', _.uniqueId()); + if (_.isUndefined(this.get('id'))) { + this.set('id', _.uniqueId()); + } var s = this.get('fileName'); if (s.charAt(0) === '/') { diff --git a/js/service/attachmentservice.js b/js/service/attachmentservice.js new file mode 100644 index 000000000..a231e9289 --- /dev/null +++ b/js/service/attachmentservice.js @@ -0,0 +1,66 @@ +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * ownCloud - Mail + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +define(function(require) { + 'use strict'; + + var $ = require('jquery'); + var OC = require('OC'); + var Radio = require('radio'); + + Radio.message.reply('save:cloud', saveToFiles); + + /** + * @param {Account} account + * @param {Folder} folder + * @param {number} messageId + * @param {number} attachmentId + * @param {string} path + * @returns {Promise} + */ + function saveToFiles(account, folder, messageId, attachmentId, path) { + var defer = $.Deferred(); + var url = OC.generateUrl( + 'apps/mail/accounts/{accountId}/' + + 'folders/{folderId}/messages/{messageId}/' + + 'attachment/{attachmentId}', { + accountId: account.get('accountId'), + folderId: folder.get('id'), + messageId: messageId, + attachmentId: attachmentId + }); + + var options = { + data: { + targetPath: path + }, + type: 'POST', + success: function() { + defer.resolve(); + }, + error: function() { + defer.reject(); + } + }; + + $.ajax(url, options); + return defer.promise(); + } + +}); diff --git a/js/templates/message-attachment.html b/js/templates/message-attachment.html index fcba1ad3d..9f902ca28 100644 --- a/js/templates/message-attachment.html +++ b/js/templates/message-attachment.html @@ -4,5 +4,5 @@ {{/if}} <img class="attachment-icon" src="{{mimeUrl}}" /> <span class="attachment-name" title="{{fileName}} ({{humanFileSize size}})">{{fileName}} <span class="attachment-size">({{humanFileSize size}})</span></span> -<a class="button icon-download attachment-download" href="{{downloadUrl}}" title="{{ t 'Download attachment' }}"></a> +<button class="button icon-download attachment-download" title="{{ t 'Download attachment' }}"></button> <button class="icon-folder attachment-save-to-cloud" title="{{ t 'Save to Files' }}"></button>
\ No newline at end of file diff --git a/js/templates/message-attachments.html b/js/templates/message-attachments.html index 48855fe4c..90ff0b949 100644 --- a/js/templates/message-attachments.html +++ b/js/templates/message-attachments.html @@ -3,6 +3,6 @@ </div> {{#if moreThanOne}} <p> - <button data-message-id="{{id}}" class="icon-folder attachments-save-to-cloud">{{ t 'Save all to Files' }}</button> + <button class="icon-folder attachments-save-to-cloud">{{ t 'Save all to Files' }}</button> </p> {{/if}}
\ No newline at end of file diff --git a/js/views/app.js b/js/views/app.js index 0fe289044..2ea08ec2d 100644 --- a/js/views/app.js +++ b/js/views/app.js @@ -67,21 +67,6 @@ define(function(require) { Radio.message.trigger('forward'); }); - // TODO: create marionette view and encapsulate events - $(document).on('click', '#mail-message .attachment-save-to-cloud', function(event) { - event.stopPropagation(); - var messageId = $(this).parent().data('messageId'); - var attachmentId = $(this).parent().data('attachmentId'); - Radio.message.trigger('attachment:save', messageId, attachmentId); - }); - - // TODO: create marionette view and encapsulate events - $(document).on('click', '#mail-message .attachments-save-to-cloud', function(event) { - event.stopPropagation(); - var messageId = $(this).data('messageId'); - Radio.message.trigger('attachment:save', messageId); - }); - $(document).on('click', '.link-mailto', function(event) { Radio.ui.trigger('composer:show', event); }); diff --git a/js/views/message.js b/js/views/message.js index 5249eaadb..bf14912c8 100644 --- a/js/views/message.js +++ b/js/views/message.js @@ -135,7 +135,8 @@ define(function(require) { var folderId = this.message.get('folderId'); this.attachments.show(new MessageAttachmentsView({ - collection: new Attachments(this.message.get('attachments')) + collection: new Attachments(this.message.get('attachments')), + message: this.model })); // setup reply composer view diff --git a/js/views/messageattachment.js b/js/views/messageattachment.js index 5159ea400..7746b02d0 100644 --- a/js/views/messageattachment.js +++ b/js/views/messageattachment.js @@ -20,15 +20,54 @@ define(function(require) { 'use strict'; + var $ = require('jquery'); var Handlebars = require('handlebars'); var Marionette = require('marionette'); + var MessageController = require('controller/messagecontroller'); var MessageAttachmentTemplate = require('text!templates/message-attachment.html'); /** * @class MessageAttachmentView */ var MessageAttachmentView = Marionette.ItemView.extend({ - template: Handlebars.compile(MessageAttachmentTemplate) + template: Handlebars.compile(MessageAttachmentTemplate), + ui: { + 'downloadButton': '.attachment-download', + 'saveToCloudButton': '.attachment-save-to-cloud' + }, + events: { + 'click': '_onDownload', + 'click @ui.saveToCloudButton': '_onSaveToCloud' + }, + _onDownload: function(e) { + if (!e.isDefaultPrevented()) { + e.preventDefault(); + window.location = this.model.get('downloadUrl'); + } + }, + _onSaveToCloud: function(e) { + e.preventDefault(); + // TODO: 'message' should be a property of this attachment model + // TODO: 'folder' should be a property of the message model and so on + var account = require('state').currentAccount; + var folder = require('state').currentFolder; + var messageId = this.model.get('messageId'); + var attachmentId = this.model.get('id'); + var saving = MessageController.saveAttachmentToFiles(account, folder, messageId, attachmentId); + + // Loading feedback + this.ui.saveToCloudButton.removeClass('icon-folder') + .addClass('icon-loading-small') + .prop('disabled', true); + + var _this = this; + $.when(saving).always(function() { + // Remove loading feedback again + _this.ui.saveToCloudButton.addClass('icon-folder') + .removeClass('icon-loading-small') + .prop('disabled', false); + }); + } }); return MessageAttachmentView; diff --git a/js/views/messageattachments.js b/js/views/messageattachments.js index b24771e9a..00697ba2a 100644 --- a/js/views/messageattachments.js +++ b/js/views/messageattachments.js @@ -20,8 +20,10 @@ define(function(require) { 'use strict'; + var $ = require('jquery'); var Handlebars = require('handlebars'); var Marionette = require('marionette'); + var MessageController = require('controller/messagecontroller'); var AttachmentView = require('views/messageattachment'); var AttachmentsTemplate = require('text!templates/message-attachments.html'); @@ -33,13 +35,45 @@ define(function(require) { * @lends Marionette.CompositeView */ template: Handlebars.compile(AttachmentsTemplate), + ui: { + 'saveAllToCloud': '.attachments-save-to-cloud' + }, + events: { + 'click @ui.saveAllToCloud': '_onSaveAllToCloud' + }, templateHelpers: function() { return { moreThanOne: this.collection.length > 1 }; }, childView: AttachmentView, - childViewContainer: '.attachments' + childViewContainer: '.attachments', + initialize: function(options) { + this.message = options.message; + }, + _onSaveAllToCloud: function(e) { + e.preventDefault(); + + // TODO: 'message' should be a property of this attachment model + // TODO: 'folder' should be a property of the message model and so on + var account = require('state').currentAccount; + var folder = require('state').currentFolder; + var messageId = this.message.get('id'); + var saving = MessageController.saveAttachmentsToFiles(account, folder, messageId); + + // Loading feedback + this.ui.saveAllToCloud.removeClass('icon-folder') + .addClass('icon-loading-small') + .prop('disabled', true); + + var _this = this; + $.when(saving).always(function() { + // Remove loading feedback again + _this.ui.saveAllToCloud.addClass('icon-folder') + .removeClass('icon-loading-small') + .prop('disabled', false); + }); + } }); return MessageAttachmentsView; diff --git a/lib/controller/messagescontroller.php b/lib/controller/messagescontroller.php index 82da0e25d..1b3ddb168 100755 --- a/lib/controller/messagescontroller.php +++ b/lib/controller/messagescontroller.php @@ -272,7 +272,7 @@ class MessagesController extends Controller { * @param int $accountId * @param string $folderId * @param string $messageId - * @param string $attachmentId + * @param int $attachmentId * @param string $targetPath * @return JSONResponse */ |