Skip to content

Commit

Permalink
fix: Attachments show up correctly again now (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
schneider-felix authored Dec 4, 2024
1 parent befcf0f commit dec77b6
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 82 deletions.
11 changes: 11 additions & 0 deletions src/Content/MailArchive/MailArchiveException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MailArchiveException extends HttpException
{
public const NOT_FOUND_CODE = 'MAIL_ARCHIVE__NOT_FOUND';
public const MISSING_PARAMETER_CODE = 'MAIL_ARCHIVE__MISSING_PARAMETER';
public const INVALID_UUID_CODE = 'MAIL_ARCHIVE__PARAMETER_INVALID_UUID';
public const UNREADABLE_EML_CODE = 'MAIL_ARCHIVE__UNREADABLE_EML';

public static function notFound(): self
Expand All @@ -32,6 +33,16 @@ public static function parameterMissing(string $parameter): self
);
}

public static function parameterInvalidUuid(string $parameter): self
{
return new self(
Response::HTTP_BAD_REQUEST,
self::INVALID_UUID_CODE,
'Parameter "{{parameter}}" is not a valid UUID',
['parameter' => $parameter],
);
}

public static function unreadableEml(string $path): self
{
return new self(
Expand Down
27 changes: 14 additions & 13 deletions src/Controller/Api/MailArchiveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\PlatformRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
Expand All @@ -36,24 +37,24 @@ class MailArchiveController extends AbstractController
* @param EntityRepository<EntityCollection<MailArchiveAttachmentEntity>> $froshMailArchiveAttachmentRepository
*/
public function __construct(
private readonly EntityRepository $froshMailArchiveRepository,
private readonly EntityRepository $froshMailArchiveAttachmentRepository,
private readonly EntityRepository $froshMailArchiveRepository,
private readonly EntityRepository $froshMailArchiveAttachmentRepository,
#[Autowire(service: MailSender::class)]
private readonly AbstractMailSender $mailSender,
private readonly RequestStack $requestStack,
private readonly EmlFileManager $emlFileManager,
private readonly RequestStack $requestStack,
private readonly EmlFileManager $emlFileManager,
) {}

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

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

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

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

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

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

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

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

$attachment = $this->froshMailArchiveAttachmentRepository->search($criteria, Context::createDefaultContext())->first();
$attachment = $this->froshMailArchiveAttachmentRepository->search($criteria, $context)->first();
if (!$attachment instanceof MailArchiveAttachmentEntity) {
throw MailArchiveException::notFound();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@

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

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

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

<template #content>
<sw-card-view v-if="archive">
<sw-alert
v-if="archive.transportState === 'failed'"
variant="warning"
class="frosh-mail-archive__detail-alert"
v-if="archive.transportState === 'failed'"
variant="warning"
class="frosh-mail-archive__detail-alert"
>
{{ $tc('frosh-mail-archive.detail.alert.transportFailed') }}
{{ $t('frosh-mail-archive.detail.alert.transportFailed') }}
</sw-alert>
<sw-card
:title="$tc('frosh-mail-archive.detail.metadata.title')"
position-identifier="frosh-mail-archive-metadata"
:title="$t('frosh-mail-archive.detail.metadata.title')"
position-identifier="frosh-mail-archive-metadata"
>
<sw-text-field :label="$tc('frosh-mail-archive.detail.metadata.sentDate')" :disabled="true"
v-model="createdAtDate"></sw-text-field>
Expand All @@ -47,40 +47,50 @@
<frosh-mail-resend-history :key="resendKey" :currentMailId="archive.id"
:sourceMailId="archive.sourceMailId ?? archive.id"/>
<sw-card
:title="$tc('frosh-mail-archive.detail.content.title')"
position-identifier="frosh-mail-archive-content"
:title="$t('frosh-mail-archive.detail.content.title')"
position-identifier="frosh-mail-archive-content"
>
<h4>HTML</h4>
<iframe :src="htmlText" sandbox frameborder="0"></iframe>

<h4>Plain</h4>
<iframe :src="plainText" sandbox frameborder="0"></iframe>

<h4>Attachments: {{ archive.attachments.length }}</h4>

<sw-data-grid
v-if="archive.attachments.length > 0"
:showSelection="false"
:dataSource="archive.attachments"
:columns="attachmentsColumns"
>
<template #column-fileSize="{ item }">
<template v-if="item.fileSize < 0">
unknown
</template>
<template v-else>
{{ formatSize(item.fileSize) }}
</sw-card>
<sw-card :title="$t('frosh-mail-archive.detail.attachments.title')"
position-identifier="frosh-mail-archive-attachments"
>
<template #grid>
<sw-card-section v-if="archive.transportState === 'pending' || archive.attachments.length === 0"
secondary divider="bottom">
<sw-alert variant="warning" v-if="archive.transportState === 'pending'">
{{ $t('frosh-mail-archive.detail.attachments.attachments-incomplete-alert') }}
</sw-alert>
<sw-alert v-if="archive.attachments.length === 0">
{{ $t('frosh-mail-archive.detail.attachments.no-attachments-alert') }}
</sw-alert>
</sw-card-section>
<sw-data-grid
:showSelection="false"
:dataSource="archive.attachments"
:columns="attachmentsColumns"
>
<template #column-fileSize="{ item }">
<template v-if="item.fileSize < 0">
{{ $t('frosh-mail-archive.detail.attachments.size-unknown') }}
</template>
<template v-else>
{{ formatSize(item.fileSize) }}
</template>
</template>
</template>

<template #actions="{ item }">
<sw-context-menu-item class="sw-entity-listing__context-menu-show-action"
@click="downloadAttachment(item.id)">
Download
</sw-context-menu-item>
</template>

</sw-data-grid>
<template #actions="{ item }">
<sw-context-menu-item class="sw-entity-listing__context-menu-show-action"
@click="downloadAttachment(item.id)">
{{ $t('frosh-mail-archive.detail.attachments.download') }}
</sw-context-menu-item>
</template>
</sw-data-grid>
</template>
</sw-card>
</sw-card-view>
</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ Component.register('frosh-mail-archive-detail', {
return [
{
property: 'fileName',
label: 'Name',
label: this.$t('frosh-mail-archive.detail.attachments.file-name'),
rawData: true
},
{
property: 'fileSize',
label: 'Size',
label: this.$t('frosh-mail-archive.detail.attachments.size'),
rawData: true
},
{
property: 'contentType',
label: 'ContentType',
label: this.$t('frosh-mail-archive.detail.attachments.type'),
rawData: true
}
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
"content": {
"title": "Inhalt"
},
"attachments": {
"title": "Anhänge",
"size-unknown": "unbekannt",
"download": "Herunterladen",
"size": "Dateigröße",
"file-name": "Name",
"type": "MIME type",
"no-attachments-alert": "Diese E-Mail enthält keine Anhänge.",
"attachments-incomplete-alert": "Möglicherweise werden nicht alle Anhänge angezeigt, während die E-Mail im \"ausstehend\" Status ist."
},
"alert": {
"transportFailed": "E-Mail-Versand fehlgeschlagen. Bitte überprüfe deine Mailer-Einstellungen und versuche es erneut."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
"content": {
"title": "Content"
},
"attachments": {
"title": "Attachments",
"size-unknown": "unknown",
"download": "Download",
"size": "File size",
"file-name": "Name",
"type": "MIME type",
"no-attachments-alert": "This mail does not contain any attachments.",
"attachments-incomplete-alert": "Some attachments might not show up while the mail is in pending state."
},
"alert": {
"transportFailed": "Mail delivery failed. Please check mailer settings and try again."
},
Expand Down
14 changes: 2 additions & 12 deletions src/Services/MailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\PlatformRequest;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mailer\Envelope;
Expand Down Expand Up @@ -62,16 +63,6 @@ private function saveMail(string $id, Email $message): void
{
$emlPath = $this->emlFileManager->writeFile($id, $message->toString());

$attachments = [];

foreach ($message->getAttachments() as $attachment) {
$attachments[] = [
'fileName' => $attachment->getFilename(),
'contentType' => $attachment->getContentType(),
'fileSize' => \strlen($attachment->bodyToString()),
];
}

$context = Context::createDefaultContext();
$this->froshMailArchiveRepository->create([
[
Expand All @@ -84,7 +75,6 @@ private function saveMail(string $id, Email $message): void
'emlPath' => $emlPath,
'salesChannelId' => $this->getCurrentSalesChannelId(),
'customerId' => $this->getCustomerIdByMail($message->getTo()),
'attachments' => $attachments,
'sourceMailId' => $this->getSourceMailId($context),
'transportState' => self::TRANSPORT_STATE_PENDING,
],
Expand All @@ -97,7 +87,7 @@ private function getCurrentSalesChannelId(): ?string
return null;
}

$salesChannelId = $this->requestStack->getMainRequest()->attributes->get('sw-sales-channel-id');
$salesChannelId = $this->requestStack->getMainRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
if (!\is_string($salesChannelId)) {
return null;
}
Expand Down
Loading

0 comments on commit dec77b6

Please sign in to comment.