Files
esun3d/app/code/Esun/CustomerMessage/Controller/Attachment/View.php
T
2026-09-15 10:35:56 +08:00

41 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\CustomerMessage\Controller\Attachment;
use Esun\CustomerMessage\Model\AttachmentStorage;
use Esun\CustomerMessage\Model\ConversationService;
use Magento\Customer\Controller\AbstractAccount;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\RawFactory;
class View extends AbstractAccount implements HttpGetActionInterface
{
public function __construct(Context $context, private readonly Session $session, private readonly ConversationService $conversations, private readonly AttachmentStorage $storage, private readonly RawFactory $rawFactory)
{
parent::__construct($context);
}
public function execute()
{
$result = $this->rawFactory->create();
try {
$attachment = $this->conversations->getAttachmentForCustomer((int)$this->getRequest()->getParam('id'), (int)$this->session->getCustomerId());
if (!$attachment) {
throw new \Magento\Framework\Exception\LocalizedException(__('The requested image is not available.'));
}
$filename = str_replace(["\r", "\n"], '', (string)$attachment['original_name']);
$result->setHeader('Content-Type', (string)$attachment['mime_type'], true)
->setHeader('Content-Length', (string)$attachment['file_size'], true)
->setHeader('Content-Disposition', 'inline; filename="' . addcslashes($filename, '"\\') . '"', true)
->setHeader('X-Content-Type-Options', 'nosniff', true)
->setContents((string)file_get_contents($this->storage->getAbsolutePath((string)$attachment['storage_path'])));
} catch (\Throwable) {
$result->setHttpResponseCode(404)->setContents('Not found');
}
return $result;
}
}