Skip to content

Commit

Permalink
First draft of a "Download all attachments" feature
Browse files Browse the repository at this point in the history
This solves #552
  • Loading branch information
jakobsack committed Oct 29, 2017
1 parent d17138e commit bc4f0af
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
'url' => '/api/accounts/{accountId}/folders/{folderId}/messages/{messageId}/attachment/{attachmentId}',
'verb' => 'GET'
],
[
'name' => 'messages#downloadAttachments',
'url' => '/api/accounts/{accountId}/folders/{folderId}/messages/{messageId}/attachments',
'verb' => 'GET'
],
[
'name' => 'messages#saveAttachment',
'url' => '/api/accounts/{accountId}/folders/{folderId}/messages/{messageId}/attachment/{attachmentId}',
Expand Down
1 change: 1 addition & 0 deletions css/mail.css
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ input.submit-message,
/* show icon + text for Download all button
as well as when there is only one attachment */
.attachments-save-to-cloud,
.attachments-download-all,
.attachments-download,
.mail-message-attachment-single .attachment-save-to-cloud,
.mail-message-attachment-single .attachment-download {
Expand Down
1 change: 1 addition & 0 deletions js/templates/message-attachments.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
{{#if moreThanOne}}
<p>
<button class="icon-folder attachments-save-to-cloud">{{ t 'Save all to Files' }}</button>
<button class="icon-download attachments-download-all">{{ t 'Download all' }}</button>
</p>
{{/if}}
12 changes: 10 additions & 2 deletions js/views/messageattachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ define(function(require) {
*/
template: AttachmentsTemplate,
ui: {
'saveAllToCloud': '.attachments-save-to-cloud'
'saveAllToCloud': '.attachments-save-to-cloud',
'downloadAll': '.attachments-download-all'
},
events: {
'click @ui.saveAllToCloud': '_onSaveAllToCloud'
'click @ui.saveAllToCloud': '_onSaveAllToCloud',
'click @ui.downloadAll': '_onDownloadAll'
},
templateContext: function() {
return {
Expand Down Expand Up @@ -70,6 +72,12 @@ define(function(require) {
.removeClass('icon-loading-small')
.prop('disabled', false);
});
},
_onDownloadAll: function(e) {
e.preventDefault();

window.open(this.message.get('downloadAllAttachmentsUrl'));
window.focus();
}
});

Expand Down
41 changes: 41 additions & 0 deletions lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Util;
use OC\Streamer;
use OC_UTIL;

class MessagesController extends Controller {

Expand Down Expand Up @@ -293,6 +295,37 @@ public function downloadAttachment($accountId, $folderId, $messageId, $attachmen
$attachment->getType());
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $accountId
* @param string $folderId
* @param string $messageId
*/
public function downloadAttachments($accountId, $folderId, $messageId) {
$mailBox = $this->getFolder($accountId, $folderId);
$message = $mailBox->getMessage($messageId);

$json = $message->getFullMessage($mailBox->getSpecialRole());

$streamer = new Streamer();
OC_Util::obEnd();
$streamer->sendHeaders("attachments");

if (isset($json['attachments'])) {
foreach($json['attachments'] as $attachment){
$attachmentObject = $mailBox->getAttachment($messageId, $attachment['id']);
$fh = fopen("php://temp", 'r+');
fputs($fh, $attachmentObject->getContents());
rewind($fh);
$streamer->addFileFromStream($fh, $attachment['fileName'], $attachment['size'], false);
fclose($fh);
}
}
$streamer->finalize();
}

/**
* @NoAdminRequired
*
Expand Down Expand Up @@ -495,6 +528,14 @@ private function loadMultiple($accountId, $folderId, $ids) {
private function enhanceMessage($accountId, $folderId, $id, IMAPMessage $m, $mailBox) {
$json = $m->getFullMessage($mailBox->getSpecialRole());
$json['senderImage'] = $this->contactsIntegration->getPhoto($m->getFrom()->first()->toHorde()->bare_address);
$downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.downloadAttachments', [
'accountId' => $accountId,
'folderId' => $folderId,
'messageId' => $id,
]);
$downloadUrl = $this->urlGenerator->getAbsoluteURL($downloadUrl);

$json['downloadAllAttachmentsUrl'] = $downloadUrl;
if (isset($json['hasHtmlBody'])) {
$json['htmlBodyUrl'] = $this->buildHtmlBodyUrl($accountId, $folderId, $id);
}
Expand Down

0 comments on commit bc4f0af

Please sign in to comment.