41 lines
1.8 KiB
PHP
41 lines
1.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerMessage\Controller\Adminhtml\Attachment;
|
|
|
|
use Esun\CustomerMessage\Model\AttachmentStorage;
|
|
use Esun\CustomerMessage\Model\ConversationService;
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Backend\App\Action\Context;
|
|
use Magento\Framework\App\Action\HttpGetActionInterface;
|
|
use Magento\Framework\Controller\Result\RawFactory;
|
|
|
|
class View extends Action implements HttpGetActionInterface
|
|
{
|
|
public const ADMIN_RESOURCE = 'Esun_CustomerMessage::messages';
|
|
|
|
public function __construct(Context $context, 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->getAttachmentForAdmin((int)$this->getRequest()->getParam('id'));
|
|
if (!$attachment) {
|
|
throw new \Magento\Framework\Exception\LocalizedException(__('The requested image is not available.'));
|
|
}
|
|
$filename = str_replace(["\r", "\n"], '', (string)$attachment['original_name']);
|
|
return $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) {
|
|
return $result->setHttpResponseCode(404)->setContents('Not found');
|
|
}
|
|
}
|
|
}
|