Skip to content

Commit dec77b6

Browse files
fix: Attachments show up correctly again now (#92)
1 parent befcf0f commit dec77b6

File tree

8 files changed

+135
-82
lines changed

8 files changed

+135
-82
lines changed

src/Content/MailArchive/MailArchiveException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class MailArchiveException extends HttpException
1111
{
1212
public const NOT_FOUND_CODE = 'MAIL_ARCHIVE__NOT_FOUND';
1313
public const MISSING_PARAMETER_CODE = 'MAIL_ARCHIVE__MISSING_PARAMETER';
14+
public const INVALID_UUID_CODE = 'MAIL_ARCHIVE__PARAMETER_INVALID_UUID';
1415
public const UNREADABLE_EML_CODE = 'MAIL_ARCHIVE__UNREADABLE_EML';
1516

1617
public static function notFound(): self
@@ -32,6 +33,16 @@ public static function parameterMissing(string $parameter): self
3233
);
3334
}
3435

36+
public static function parameterInvalidUuid(string $parameter): self
37+
{
38+
return new self(
39+
Response::HTTP_BAD_REQUEST,
40+
self::INVALID_UUID_CODE,
41+
'Parameter "{{parameter}}" is not a valid UUID',
42+
['parameter' => $parameter],
43+
);
44+
}
45+
3546
public static function unreadableEml(string $path): self
3647
{
3748
return new self(

src/Controller/Api/MailArchiveController.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
1515
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
1616
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
17+
use Shopware\Core\Framework\Uuid\Uuid;
1718
use Shopware\Core\PlatformRequest;
1819
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1920
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -36,24 +37,24 @@ class MailArchiveController extends AbstractController
3637
* @param EntityRepository<EntityCollection<MailArchiveAttachmentEntity>> $froshMailArchiveAttachmentRepository
3738
*/
3839
public function __construct(
39-
private readonly EntityRepository $froshMailArchiveRepository,
40-
private readonly EntityRepository $froshMailArchiveAttachmentRepository,
40+
private readonly EntityRepository $froshMailArchiveRepository,
41+
private readonly EntityRepository $froshMailArchiveAttachmentRepository,
4142
#[Autowire(service: MailSender::class)]
4243
private readonly AbstractMailSender $mailSender,
43-
private readonly RequestStack $requestStack,
44-
private readonly EmlFileManager $emlFileManager,
44+
private readonly RequestStack $requestStack,
45+
private readonly EmlFileManager $emlFileManager,
4546
) {}
4647

4748
#[Route(path: '/api/_action/frosh-mail-archive/resend-mail', name: 'api.action.frosh-mail-archive.resend-mail')]
48-
public function resend(Request $request): JsonResponse
49+
public function resend(Request $request, Context $context): JsonResponse
4950
{
5051
$mailId = $request->request->get('mailId');
5152

5253
if (!\is_string($mailId)) {
5354
throw MailArchiveException::parameterMissing('mailId');
5455
}
5556

56-
$mailArchive = $this->froshMailArchiveRepository->search(new Criteria([$mailId]), Context::createDefaultContext())->first();
57+
$mailArchive = $this->froshMailArchiveRepository->search(new Criteria([$mailId]), $context)->first();
5758
if (!$mailArchive instanceof MailArchiveEntity) {
5859
throw MailArchiveException::notFound();
5960
}
@@ -83,15 +84,15 @@ public function resend(Request $request): JsonResponse
8384
}
8485

8586
#[Route(path: '/api/_action/frosh-mail-archive/content')]
86-
public function download(Request $request): JsonResponse
87+
public function download(Request $request, Context $context): JsonResponse
8788
{
8889
$mailId = $request->request->get('mailId');
8990

9091
if (!\is_string($mailId)) {
9192
throw MailArchiveException::parameterMissing('mailId');
9293
}
9394

94-
$mailArchive = $this->froshMailArchiveRepository->search(new Criteria([$mailId]), Context::createDefaultContext())->first();
95+
$mailArchive = $this->froshMailArchiveRepository->search(new Criteria([$mailId]), $context)->first();
9596
if (!$mailArchive instanceof MailArchiveEntity) {
9697
throw MailArchiveException::notFound();
9798
}
@@ -120,17 +121,17 @@ public function download(Request $request): JsonResponse
120121
}
121122

122123
#[Route(path: '/api/_action/frosh-mail-archive/attachment', name: 'api.action.frosh-mail-archive.attachment')]
123-
public function attachment(Request $request): JsonResponse
124+
public function attachment(Request $request, Context $context): JsonResponse
124125
{
125-
$attachmentId = $request->request->get('attachmentId');
126-
if (!\is_string($attachmentId)) {
127-
throw MailArchiveException::parameterMissing('attachmentId');
126+
$attachmentId = $request->request->getString('attachmentId');
127+
if (!Uuid::isValid($attachmentId)) {
128+
throw MailArchiveException::parameterInvalidUuid('attachmentId');
128129
}
129130

130131
$criteria = new Criteria([$attachmentId]);
131132
$criteria->addAssociation('mailArchive');
132133

133-
$attachment = $this->froshMailArchiveAttachmentRepository->search($criteria, Context::createDefaultContext())->first();
134+
$attachment = $this->froshMailArchiveAttachmentRepository->search($criteria, $context)->first();
134135
if (!$attachment instanceof MailArchiveAttachmentEntity) {
135136
throw MailArchiveException::notFound();
136137
}

src/Resources/app/administration/src/module/frosh-mail-archive/page/frosh-mail-archive-detail/frosh-mail-archive-detail.twig

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@
55

66
<template #smart-bar-actions>
77
<sw-button variant="ghost" v-if="archive && archive.customer" @click="openCustomer">
8-
{{ $tc('frosh-mail-archive.detail.toolbar.customer') }}
8+
{{ $t('frosh-mail-archive.detail.toolbar.customer') }}
99
</sw-button>
1010

11-
<sw-button-process :isLoading="downloadIsLoading" :processSuccess="downloadIsSuccessful" @click="downloadMail"
12-
@process-finish="downloadFinish">
13-
{{ $tc('frosh-mail-archive.detail.toolbar.downloadEml') }}
11+
<sw-button-process :isLoading="downloadIsLoading" :process-success="downloadIsSuccessful" @click="downloadMail"
12+
@update:process-success="downloadFinish">
13+
{{ $t('frosh-mail-archive.detail.toolbar.downloadEml') }}
1414
</sw-button-process>
1515

1616
<sw-button-process :isLoading="resendIsLoading" :processSuccess="resendIsSuccessful" @click="resendMail"
17-
@process-finish="resendFinish">
18-
{{ $tc('frosh-mail-archive.detail.toolbar.resend') }}
17+
@update:process-success="resendFinish">
18+
{{ $t('frosh-mail-archive.detail.toolbar.resend') }}
1919
</sw-button-process>
2020
</template>
2121

2222
<template #content>
2323
<sw-card-view v-if="archive">
2424
<sw-alert
25-
v-if="archive.transportState === 'failed'"
26-
variant="warning"
27-
class="frosh-mail-archive__detail-alert"
25+
v-if="archive.transportState === 'failed'"
26+
variant="warning"
27+
class="frosh-mail-archive__detail-alert"
2828
>
29-
{{ $tc('frosh-mail-archive.detail.alert.transportFailed') }}
29+
{{ $t('frosh-mail-archive.detail.alert.transportFailed') }}
3030
</sw-alert>
3131
<sw-card
32-
:title="$tc('frosh-mail-archive.detail.metadata.title')"
33-
position-identifier="frosh-mail-archive-metadata"
32+
:title="$t('frosh-mail-archive.detail.metadata.title')"
33+
position-identifier="frosh-mail-archive-metadata"
3434
>
3535
<sw-text-field :label="$tc('frosh-mail-archive.detail.metadata.sentDate')" :disabled="true"
3636
v-model="createdAtDate"></sw-text-field>
@@ -47,40 +47,50 @@
4747
<frosh-mail-resend-history :key="resendKey" :currentMailId="archive.id"
4848
:sourceMailId="archive.sourceMailId ?? archive.id"/>
4949
<sw-card
50-
:title="$tc('frosh-mail-archive.detail.content.title')"
51-
position-identifier="frosh-mail-archive-content"
50+
:title="$t('frosh-mail-archive.detail.content.title')"
51+
position-identifier="frosh-mail-archive-content"
5252
>
5353
<h4>HTML</h4>
5454
<iframe :src="htmlText" sandbox frameborder="0"></iframe>
5555

5656
<h4>Plain</h4>
5757
<iframe :src="plainText" sandbox frameborder="0"></iframe>
58-
59-
<h4>Attachments: {{ archive.attachments.length }}</h4>
60-
61-
<sw-data-grid
62-
v-if="archive.attachments.length > 0"
63-
:showSelection="false"
64-
:dataSource="archive.attachments"
65-
:columns="attachmentsColumns"
66-
>
67-
<template #column-fileSize="{ item }">
68-
<template v-if="item.fileSize < 0">
69-
unknown
70-
</template>
71-
<template v-else>
72-
{{ formatSize(item.fileSize) }}
58+
</sw-card>
59+
<sw-card :title="$t('frosh-mail-archive.detail.attachments.title')"
60+
position-identifier="frosh-mail-archive-attachments"
61+
>
62+
<template #grid>
63+
<sw-card-section v-if="archive.transportState === 'pending' || archive.attachments.length === 0"
64+
secondary divider="bottom">
65+
<sw-alert variant="warning" v-if="archive.transportState === 'pending'">
66+
{{ $t('frosh-mail-archive.detail.attachments.attachments-incomplete-alert') }}
67+
</sw-alert>
68+
<sw-alert v-if="archive.attachments.length === 0">
69+
{{ $t('frosh-mail-archive.detail.attachments.no-attachments-alert') }}
70+
</sw-alert>
71+
</sw-card-section>
72+
<sw-data-grid
73+
:showSelection="false"
74+
:dataSource="archive.attachments"
75+
:columns="attachmentsColumns"
76+
>
77+
<template #column-fileSize="{ item }">
78+
<template v-if="item.fileSize < 0">
79+
{{ $t('frosh-mail-archive.detail.attachments.size-unknown') }}
80+
</template>
81+
<template v-else>
82+
{{ formatSize(item.fileSize) }}
83+
</template>
7384
</template>
74-
</template>
75-
76-
<template #actions="{ item }">
77-
<sw-context-menu-item class="sw-entity-listing__context-menu-show-action"
78-
@click="downloadAttachment(item.id)">
79-
Download
80-
</sw-context-menu-item>
81-
</template>
8285

83-
</sw-data-grid>
86+
<template #actions="{ item }">
87+
<sw-context-menu-item class="sw-entity-listing__context-menu-show-action"
88+
@click="downloadAttachment(item.id)">
89+
{{ $t('frosh-mail-archive.detail.attachments.download') }}
90+
</sw-context-menu-item>
91+
</template>
92+
</sw-data-grid>
93+
</template>
8494
</sw-card>
8595
</sw-card-view>
8696
</template>

src/Resources/app/administration/src/module/frosh-mail-archive/page/frosh-mail-archive-detail/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ Component.register('frosh-mail-archive-detail', {
8585
return [
8686
{
8787
property: 'fileName',
88-
label: 'Name',
88+
label: this.$t('frosh-mail-archive.detail.attachments.file-name'),
8989
rawData: true
9090
},
9191
{
9292
property: 'fileSize',
93-
label: 'Size',
93+
label: this.$t('frosh-mail-archive.detail.attachments.size'),
9494
rawData: true
9595
},
9696
{
9797
property: 'contentType',
98-
label: 'ContentType',
98+
label: this.$t('frosh-mail-archive.detail.attachments.type'),
9999
rawData: true
100100
}
101101
];

src/Resources/app/administration/src/module/frosh-mail-archive/snippet/de-DE.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
"content": {
5252
"title": "Inhalt"
5353
},
54+
"attachments": {
55+
"title": "Anhänge",
56+
"size-unknown": "unbekannt",
57+
"download": "Herunterladen",
58+
"size": "Dateigröße",
59+
"file-name": "Name",
60+
"type": "MIME type",
61+
"no-attachments-alert": "Diese E-Mail enthält keine Anhänge.",
62+
"attachments-incomplete-alert": "Möglicherweise werden nicht alle Anhänge angezeigt, während die E-Mail im \"ausstehend\" Status ist."
63+
},
5464
"alert": {
5565
"transportFailed": "E-Mail-Versand fehlgeschlagen. Bitte überprüfe deine Mailer-Einstellungen und versuche es erneut."
5666
},

src/Resources/app/administration/src/module/frosh-mail-archive/snippet/en-GB.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
"content": {
5252
"title": "Content"
5353
},
54+
"attachments": {
55+
"title": "Attachments",
56+
"size-unknown": "unknown",
57+
"download": "Download",
58+
"size": "File size",
59+
"file-name": "Name",
60+
"type": "MIME type",
61+
"no-attachments-alert": "This mail does not contain any attachments.",
62+
"attachments-incomplete-alert": "Some attachments might not show up while the mail is in pending state."
63+
},
5464
"alert": {
5565
"transportFailed": "Mail delivery failed. Please check mailer settings and try again."
5666
},

src/Services/MailSender.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
1515
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
1616
use Shopware\Core\Framework\Uuid\Uuid;
17+
use Shopware\Core\PlatformRequest;
1718
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
1819
use Symfony\Component\HttpFoundation\RequestStack;
1920
use Symfony\Component\Mailer\Envelope;
@@ -62,16 +63,6 @@ private function saveMail(string $id, Email $message): void
6263
{
6364
$emlPath = $this->emlFileManager->writeFile($id, $message->toString());
6465

65-
$attachments = [];
66-
67-
foreach ($message->getAttachments() as $attachment) {
68-
$attachments[] = [
69-
'fileName' => $attachment->getFilename(),
70-
'contentType' => $attachment->getContentType(),
71-
'fileSize' => \strlen($attachment->bodyToString()),
72-
];
73-
}
74-
7566
$context = Context::createDefaultContext();
7667
$this->froshMailArchiveRepository->create([
7768
[
@@ -84,7 +75,6 @@ private function saveMail(string $id, Email $message): void
8475
'emlPath' => $emlPath,
8576
'salesChannelId' => $this->getCurrentSalesChannelId(),
8677
'customerId' => $this->getCustomerIdByMail($message->getTo()),
87-
'attachments' => $attachments,
8878
'sourceMailId' => $this->getSourceMailId($context),
8979
'transportState' => self::TRANSPORT_STATE_PENDING,
9080
],
@@ -97,7 +87,7 @@ private function getCurrentSalesChannelId(): ?string
9787
return null;
9888
}
9989

100-
$salesChannelId = $this->requestStack->getMainRequest()->attributes->get('sw-sales-channel-id');
90+
$salesChannelId = $this->requestStack->getMainRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
10191
if (!\is_string($salesChannelId)) {
10292
return null;
10393
}

0 commit comments

Comments
 (0)