diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php
index 022964f703..f588a6e41d 100755
--- a/lib/Controller/MessagesController.php
+++ b/lib/Controller/MessagesController.php
@@ -841,6 +841,37 @@ public function downloadAttachments(int $id): Response {
return $zip;
}
+ private function moveAttachmentToFiles(Attachment $attachment, string $targetPath, ?string $fileName): void {
+ if ($this->userFolder === null) {
+ return;
+ }
+
+ if ($fileName === null) {
+ $fileName = '';
+ } else {
+ $fileName = trim($fileName);
+ }
+
+ if ($fileName === '') {
+ $fileName = $attachment->getName() ?? $this->l10n->t('Embedded message %s', [
+ $attachment->getId(),
+ ]) . '.eml';
+ }
+
+ $fileParts = pathinfo($fileName);
+ $fileName = $fileParts['filename'];
+ $fileExtension = $fileParts['extension'] ?? '';
+ $fullPath = "$targetPath/$fileName.$fileExtension";
+ $counter = 2;
+ while ($this->userFolder->nodeExists($fullPath)) {
+ $fullPath = "$targetPath/$fileName ($counter).$fileExtension";
+ $counter++;
+ }
+
+ $newFile = $this->userFolder->newFile($fullPath);
+ $newFile->putContent($attachment->getContent());
+ }
+
/**
* @NoAdminRequired
*
@@ -858,7 +889,8 @@ public function downloadAttachments(int $id): Response {
#[TrapError]
public function saveAttachment(int $id,
string $attachmentId,
- string $targetPath) {
+ string $targetPath,
+ ?string $fileName) {
if ($this->userId === null) {
return new JSONResponse([], Http::STATUS_UNAUTHORIZED);
}
@@ -880,40 +912,27 @@ public function saveAttachment(int $id,
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
- /** @var Attachment[] $attachments */
- $attachments = [];
if ($attachmentId === '0') {
$attachments = $this->mailManager->getMailAttachments(
$account,
$mailbox,
$message,
);
+
+ foreach ($attachments as $attachment) {
+ $this->moveAttachmentToFiles($attachment, $targetPath, null);
+ }
} else {
- $attachments[] = $this->mailManager->getMailAttachment(
+ $attachment = $this->mailManager->getMailAttachment(
$account,
$mailbox,
$message,
$attachmentId,
);
- }
- foreach ($attachments as $attachment) {
- $fileName = $attachment->getName() ?? $this->l10n->t('Embedded message %s', [
- $attachment->getId(),
- ]) . '.eml';
- $fileParts = pathinfo($fileName);
- $fileName = $fileParts['filename'];
- $fileExtension = $fileParts['extension'] ?? '';
- $fullPath = "$targetPath/$fileName.$fileExtension";
- $counter = 2;
- while ($this->userFolder->nodeExists($fullPath)) {
- $fullPath = "$targetPath/$fileName ($counter).$fileExtension";
- $counter++;
- }
-
- $newFile = $this->userFolder->newFile($fullPath);
- $newFile->putContent($attachment->getContent());
+ $this->moveAttachmentToFiles($attachment, $targetPath, $fileName);
}
+
return new JSONResponse();
}
diff --git a/src/components/MessageAttachment.vue b/src/components/MessageAttachment.vue
index e0679e0337..ec3fafdd82 100644
--- a/src/components/MessageAttachment.vue
+++ b/src/components/MessageAttachment.vue
@@ -19,6 +19,7 @@
{{ humanReadable(size) }}
+
isFilePickerOpen = false" />
+
+
+
+
+
controller->saveAttachment(
$id,
$attachmentId,
- $targetPath
+ $targetPath,
+ null
+ );
+
+ $this->assertEquals($expected, $response);
+ }
+
+ public function testSaveAndRenameSingleAttachment() {
+ $accountId = 17;
+ $mailboxId = 987;
+ $id = 123;
+ $uid = 321;
+ $attachmentId = '2.2';
+ $targetPath = 'Downloads';
+ $message = new \OCA\Mail\Db\Message();
+ $message->setMailboxId($mailboxId);
+ $message->setUid($uid);
+ $mailbox = new \OCA\Mail\Db\Mailbox();
+ $mailbox->setName('INBOX');
+ $mailbox->setAccountId($accountId);
+ $this->mailManager->expects($this->once())
+ ->method('getMessage')
+ ->with($this->userId, $id)
+ ->willReturn($message);
+ $this->mailManager->expects($this->once())
+ ->method('getMailbox')
+ ->with($this->userId, $mailboxId)
+ ->willReturn($mailbox);
+ $this->accountService->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo($this->userId), $this->equalTo($accountId))
+ ->will($this->returnValue($this->account));
+ $this->mailManager->expects($this->once())
+ ->method('getMailAttachment')
+ ->with($this->account, $mailbox, $message, $attachmentId)
+ ->will($this->returnValue($this->attachment));
+ $folderNode = $this->createStub(Folder::class);
+ $this->userFolder->expects($this->once())
+ ->method('get')
+ ->with('Downloads')
+ ->willReturn($folderNode);
+ $this->userFolder->expects($this->exactly(2))
+ ->method('nodeExists')
+ ->withConsecutive(['Downloads'], ['Downloads/foobar.jpg'])
+ ->willReturnOnConsecutiveCalls(true, false);
+ $file = $this->getMockBuilder('\OCP\Files\File')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->userFolder->expects($this->once())
+ ->method('newFile')
+ ->with('Downloads/foobar.jpg')
+ ->will($this->returnValue($file));
+ $file->expects($this->once())
+ ->method('putContent')
+ ->with('abcdefg');
+ $this->attachment->expects($this->once())
+ ->method('getContent')
+ ->will($this->returnValue('abcdefg'));
+
+ $expected = new JSONResponse();
+ $response = $this->controller->saveAttachment(
+ $id,
+ $attachmentId,
+ $targetPath,
+ 'foobar.jpg'
);
$this->assertEquals($expected, $response);
@@ -476,7 +540,8 @@ public function testSaveAllAttachments() {
$response = $this->controller->saveAttachment(
$id,
$attachmentId,
- $targetPath
+ $targetPath,
+ null
);
$this->assertEquals($expected, $response);
@@ -488,7 +553,7 @@ public function testSaveAttachmentTargetPathNotFound(): void {
->with('NoSuchFolder')
->willReturn(false);
- $response = $this->controller->saveAttachment(123, '1', 'NoSuchFolder');
+ $response = $this->controller->saveAttachment(123, '1', 'NoSuchFolder', null);
$this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
}
@@ -506,7 +571,7 @@ public function testSaveAttachmentTargetPathIsFile(): void {
->with('some/file.txt')
->willReturn($fileNode);
- $response = $this->controller->saveAttachment(123, '1', 'some/file.txt');
+ $response = $this->controller->saveAttachment(123, '1', 'some/file.txt', null);
$this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
}