Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/Controller/LocalAttachmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\Attachment\UploadedFile;
use OCA\Mail\Service\DelegationService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
Expand All @@ -22,18 +23,21 @@
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class LocalAttachmentsController extends Controller {
private IAttachmentService $attachmentService;
private DelegationService $delegationService;
private string $userId;

/**
* @param string $appName
* @param IRequest $request
* @param IAttachmentService $attachmentService
* @param DelegationService $delegationService
* @param string $UserId
*/
public function __construct(string $appName, IRequest $request,
IAttachmentService $attachmentService, $userId) {
IAttachmentService $attachmentService, DelegationService $delegationService, $userId) {
parent::__construct($appName, $request);
$this->attachmentService = $attachmentService;
$this->delegationService = $delegationService;
$this->userId = $userId;
}

Expand All @@ -43,15 +47,21 @@ public function __construct(string $appName, IRequest $request,
* @return JSONResponse
*/
#[TrapError]
public function create(): JSONResponse {
public function create(?int $accountId = null): JSONResponse {
$file = $this->request->getUploadedFile('attachment');

if (is_null($file)) {
throw new ClientException('no file attached');
}

// Store the attachment under the account owner so it can be linked and
// sent when composing on behalf of a delegated account.
$userId = $accountId !== null
? $this->delegationService->resolveAccountUserId($accountId, $this->userId)
: $this->userId;

$uploadedFile = new UploadedFile($file);
$attachment = $this->attachmentService->addFile($this->userId, $uploadedFile);
$attachment = $this->attachmentService->addFile($userId, $uploadedFile);

return new JSONResponse($attachment, Http::STATUS_CREATED);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/Composer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
<ComposerAttachments
v-model="attachments"
:bus="bus"
:account-id="selectedAlias.id"
:upload-size-limit="attachmentSizeLimit"
@upload="$emit('upload-attachment', $event, getMessageData())" />
<div class="composer-actions-right composer-actions">
Expand Down
7 changes: 6 additions & 1 deletion src/components/ComposerAttachments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export default {
type: Number,
default: 0,
},

accountId: {
type: Number,
default: null,
},
},

data() {
Expand Down Expand Up @@ -292,7 +297,7 @@ export default {
uploaded: 0,
})
try {
return uploadLocalAttachment(file, progress(file.name), controller)
return uploadLocalAttachment(file, this.accountId, progress(file.name), controller)
.catch(() => {
this.attachments.some((attachment) => {
if (attachment.displayName === file.name && !attachment.error) {
Expand Down
6 changes: 5 additions & 1 deletion src/service/AttachmentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function downloadAttachment(url) {
return Axios.get(url).then((res) => res.data)
}

export function uploadLocalAttachment(file, progress, controller) {
export function uploadLocalAttachment(file, accountId, progress, controller) {
const url = generateUrl('/apps/mail/api/attachments')
const data = new FormData()
const opts = {
Expand All @@ -40,6 +40,10 @@ export function uploadLocalAttachment(file, progress, controller) {
}
data.append('attachment', file)

if (accountId) {
data.append('accountId', accountId)
}

return Axios.post(url, data, opts)
.then((resp) => resp.data)
.then(({ id }) => {
Expand Down
33 changes: 32 additions & 1 deletion tests/Unit/Controller/LocalAttachmentsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\Mail\Db\LocalAttachment;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Service\Attachment\UploadedFile;
use OCA\Mail\Service\DelegationService;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -24,6 +25,9 @@ class LocalAttachmentsControllerTest extends TestCase {
/** @var IAttachmentService|MockObject */
private $service;

/** @var DelegationService|MockObject */
private $delegationService;

/** @var string */
private $userId;

Expand All @@ -35,9 +39,10 @@ protected function setUp(): void {

$this->request = $this->createMock(IRequest::class);
$this->service = $this->createMock(IAttachmentService::class);
$this->delegationService = $this->createMock(DelegationService::class);
$this->userId = 'jane';

$this->controller = new LocalAttachmentsController('mail', $this->request, $this->service, $this->userId);
$this->controller = new LocalAttachmentsController('mail', $this->request, $this->service, $this->delegationService, $this->userId);
}

public function testCreateWithoutFile() {
Expand All @@ -60,6 +65,8 @@ public function testCreate() {
->willReturn($fileData);
$attachment = new LocalAttachment();
$uploadedFile = new UploadedFile($fileData);
$this->delegationService->expects($this->never())
->method('resolveAccountUserId');
$this->service->expects($this->once())
->method('addFile')
->with($this->equalTo($this->userId), $this->equalTo($uploadedFile))
Expand All @@ -69,4 +76,28 @@ public function testCreate() {

$this->assertEquals(new JSONResponse($attachment, 201), $actual);
}

public function testCreateForDelegatedAccount() {
$fileData = [
'name' => 'cat.jpg',
];
$this->request->expects($this->once())
->method('getUploadedFile')
->with('attachment')
->willReturn($fileData);
$attachment = new LocalAttachment();
$uploadedFile = new UploadedFile($fileData);
$this->delegationService->expects($this->once())
->method('resolveAccountUserId')
->with(42, $this->userId)
->willReturn('owner');
$this->service->expects($this->once())
->method('addFile')
->with($this->equalTo('owner'), $this->equalTo($uploadedFile))
->willReturn($attachment);

$actual = $this->controller->create(42);

$this->assertEquals(new JSONResponse($attachment, 201), $actual);
}
}
Loading