95 lines
3.5 KiB
PHP
95 lines
3.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Video\Controller\Adminhtml\Video;
|
|
|
|
use Esun\Video\Model\Config;
|
|
use Esun\Video\Model\VideoFactory;
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Framework\App\Filesystem\DirectoryList;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\Filesystem;
|
|
use Magento\MediaStorage\Model\File\UploaderFactory;
|
|
|
|
class Save extends Action
|
|
{
|
|
public const ADMIN_RESOURCE = 'Esun_Video::edit';
|
|
|
|
public function __construct(
|
|
Action\Context $context,
|
|
private readonly VideoFactory $videoFactory,
|
|
private readonly UploaderFactory $uploaderFactory,
|
|
private readonly Filesystem $filesystem,
|
|
private readonly Config $config
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$id = (int)$this->getRequest()->getParam('video_id');
|
|
$redirect = $this->resultRedirectFactory->create();
|
|
|
|
try {
|
|
$video = $this->videoFactory->create()->load($id);
|
|
if (!$video->getId()) {
|
|
throw new LocalizedException(__('The video no longer exists.'));
|
|
}
|
|
|
|
$title = trim((string)$this->getRequest()->getParam('title'));
|
|
if ($title === '') {
|
|
throw new LocalizedException(__('Title is required.'));
|
|
}
|
|
|
|
$oldCover = (string)$video->getData('cover_path');
|
|
$newCover = $this->saveCover();
|
|
$video->addData([
|
|
'title' => $title,
|
|
'description' => trim((string)$this->getRequest()->getParam('description')),
|
|
'status' => $this->getRequest()->getParam('status') === '0' ? 0 : 1,
|
|
]);
|
|
if ($newCover !== null) {
|
|
$video->setData('cover_path', $newCover);
|
|
}
|
|
$video->save();
|
|
|
|
if ($newCover !== null && $oldCover !== '' && $oldCover !== $newCover) {
|
|
$media = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
|
if ($media->isExist($oldCover)) {
|
|
$media->delete($oldCover);
|
|
}
|
|
}
|
|
|
|
$this->messageManager->addSuccessMessage(__('The video has been saved.'));
|
|
return $redirect->setPath('esun_video/video/index');
|
|
} catch (\Throwable $e) {
|
|
$this->messageManager->addErrorMessage($e->getMessage());
|
|
return $redirect->setPath('esun_video/video/edit', ['id' => $id]);
|
|
}
|
|
}
|
|
|
|
private function saveCover(): ?string
|
|
{
|
|
if (!isset($_FILES['cover']) || (int)$_FILES['cover']['error'] === UPLOAD_ERR_NO_FILE) {
|
|
return null;
|
|
}
|
|
if ((int)$_FILES['cover']['error'] !== UPLOAD_ERR_OK) {
|
|
throw new LocalizedException(__('The cover image could not be uploaded.'));
|
|
}
|
|
|
|
$relativeDirectory = $this->config->getMediaPath() . '/covers/' . date('Y/m');
|
|
$media = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
|
$media->create($relativeDirectory);
|
|
$uploader = $this->uploaderFactory->create(['fileId' => 'cover']);
|
|
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif', 'webp']);
|
|
$uploader->setAllowRenameFiles(true);
|
|
$uploader->setFilesDispersion(true);
|
|
$saved = $uploader->save($media->getAbsolutePath($relativeDirectory));
|
|
if (!is_array($saved) || empty($saved['file'])) {
|
|
throw new LocalizedException(__('Unable to save the cover image.'));
|
|
}
|
|
|
|
return trim($relativeDirectory . '/' . ltrim((string)$saved['file'], '/'), '/');
|
|
}
|
|
}
|