-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
412 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% if format is same as 'html' %} | ||
<ul> | ||
{% for voucher, file in files %} | ||
<li><a href="{{ notification_center_file_url(voucher) }}">{{ file.name }} ({{ file.size|format_bytes }})</a></li> | ||
{% endfor %} | ||
</ul> | ||
{% endif %} | ||
|
||
{% if format is same as 'text' %} | ||
{% for voucher, file in files %} | ||
- [{{ file.name }} ({{ file.size|format_bytes }})]({{ notification_center_file_url(voucher) }}) | ||
{% endfor %} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
use Symfony\Component\HttpFoundation\UriSigner; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage; | ||
|
||
#[Route('/notifications/download/{voucher}', 'nc_bulky_item_download', requirements: ['voucher' => BulkyItemStorage::VOUCHER_REGEX])] | ||
class DownloadBulkyItemController | ||
{ | ||
public function __construct( | ||
private readonly UriSigner $uriSigner, | ||
private readonly BulkyItemStorage $bulkyItemStorage, | ||
) { | ||
} | ||
|
||
public function __invoke(Request $request, string $voucher): Response | ||
{ | ||
if (!$this->uriSigner->checkRequest($request)) { | ||
throw new NotFoundHttpException(); | ||
} | ||
|
||
if (!$bulkyItem = $this->bulkyItemStorage->retrieve($voucher)) { | ||
throw new NotFoundHttpException(); | ||
} | ||
|
||
$stream = $bulkyItem->getContents(); | ||
|
||
$response = new StreamedResponse( | ||
static function () use ($stream): void { | ||
while (!feof($stream)) { | ||
echo fread($stream, 8192); // Read in chunks of 8 KB | ||
flush(); | ||
} | ||
fclose($stream); | ||
}, | ||
); | ||
|
||
$response->headers->set('Content-Type', 'application/octet-stream'); | ||
$response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate'); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\EventListener; | ||
|
||
use Contao\StringUtil; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemInterface; | ||
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage; | ||
use Terminal42\NotificationCenterBundle\Event\CreateParcelEvent; | ||
use Terminal42\NotificationCenterBundle\Event\GetTokenDefinitionsForNotificationTypeEvent; | ||
use Terminal42\NotificationCenterBundle\Parcel\Parcel; | ||
use Terminal42\NotificationCenterBundle\Parcel\Stamp\BulkyItemsStamp; | ||
use Terminal42\NotificationCenterBundle\Parcel\Stamp\TokenCollectionStamp; | ||
use Terminal42\NotificationCenterBundle\Token\Definition\AnythingTokenDefinition; | ||
use Terminal42\NotificationCenterBundle\Token\Definition\Factory\TokenDefinitionFactoryInterface; | ||
use Terminal42\NotificationCenterBundle\Token\Definition\HtmlTokenDefinition; | ||
use Terminal42\NotificationCenterBundle\Token\Definition\TextTokenDefinition; | ||
use Terminal42\NotificationCenterBundle\Token\Token; | ||
use Twig\Environment; | ||
|
||
class BulkyItemsTokenListener | ||
{ | ||
public function __construct( | ||
private readonly BulkyItemStorage $bulkyItemStorage, | ||
private readonly TokenDefinitionFactoryInterface $tokenDefinitionFactory, | ||
private readonly Environment $twig, | ||
) { | ||
} | ||
|
||
#[AsEventListener] | ||
public function onGetTokenDefinitions(GetTokenDefinitionsForNotificationTypeEvent $event): void | ||
{ | ||
$event | ||
->addTokenDefinition($this->tokenDefinitionFactory->create(AnythingTokenDefinition::class, 'file_item_html_*', 'file_item_html_*')) | ||
->addTokenDefinition($this->tokenDefinitionFactory->create(AnythingTokenDefinition::class, 'file_item_text_*', 'file_item_text_*')) | ||
; | ||
} | ||
|
||
#[AsEventListener] | ||
public function onCreateParcel(CreateParcelEvent $event): void | ||
{ | ||
if (!$event->getParcel()->hasStamp(TokenCollectionStamp::class) || !$event->getParcel()->getStamp(BulkyItemsStamp::class)) { | ||
return; | ||
} | ||
|
||
$tokenCollection = $event->getParcel()->getStamp(TokenCollectionStamp::class)->tokenCollection; | ||
|
||
foreach ($tokenCollection as $token) { | ||
$items = $this->extractFileItems($token, $event->getParcel()->getStamp(BulkyItemsStamp::class)); | ||
|
||
if ([] === $items) { | ||
continue; | ||
} | ||
|
||
$tokenCollection->addToken($this->createFileToken($event->getParcel(), $token, $items, 'html', HtmlTokenDefinition::class)); | ||
$tokenCollection->addToken($this->createFileToken($event->getParcel(), $token, $items, 'text', TextTokenDefinition::class)); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, BulkyItemInterface> $items | ||
*/ | ||
private function createFileToken(Parcel $parcel, Token $token, array $items, string $format, string $tokenDefinitionClass): Token | ||
{ | ||
$content = $this->twig->render('@Contao/notification_center/file_token.html.twig', [ | ||
'files' => $items, | ||
'parcel' => $parcel, | ||
'format' => $format, | ||
]); | ||
|
||
$tokenName = 'file_item_'.$format.'_'.$token->getName(); | ||
|
||
return $this->tokenDefinitionFactory->create($tokenDefinitionClass, $tokenName, $tokenName) | ||
->createToken($tokenName, $content) | ||
; | ||
} | ||
|
||
/** | ||
* @return array<string, BulkyItemInterface> | ||
*/ | ||
private function extractFileItems(Token $token, BulkyItemsStamp $bulkyItemsStamp): array | ||
{ | ||
$possibleVouchers = StringUtil::trimsplit(',', $token->getParserValue()); | ||
$items = []; | ||
|
||
foreach ($possibleVouchers as $possibleVoucher) { | ||
// Shortcut: Not a possibly bulky item voucher anyway - continue | ||
if (!BulkyItemStorage::validateVoucherFormat($possibleVoucher)) { | ||
continue; | ||
} | ||
|
||
if (!$bulkyItemsStamp->has($possibleVoucher)) { | ||
continue; | ||
} | ||
|
||
if ($item = $this->bulkyItemStorage->retrieve($possibleVoucher)) { | ||
$items[$possibleVoucher] = $item; | ||
} | ||
} | ||
|
||
return $items; | ||
} | ||
} |
Oops, something went wrong.