This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Esun\Video\Block\Adminhtml\Product;
use Esun\Video\Model\Config;
use Esun\Video\Model\ResourceModel\Video\CollectionFactory;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Catalog\Model\Product\Media\Config as ProductMediaConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
class Upload extends Template
{
private ?array $libraryItems = null;
public function __construct(
Context $context,
private readonly Config $config,
private readonly Json $json,
private readonly CollectionFactory $collectionFactory,
private readonly StoreManagerInterface $storeManager,
private readonly Filesystem $filesystem,
private readonly ProductMediaConfig $productMediaConfig,
array $data = []
) {
parent::__construct($context, $data);
}
public function getOptionsJson(): string
{
return $this->json->serialize([
'enabled' => $this->config->isEnabled(),
'uploadUrl' => $this->getUrl('esun_video/upload/chunk'),
'libraryUrl' => $this->getUrl('esun_video/video/library'),
'chunkSize' => $this->config->getChunkSizeBytes(),
'libraryItems' => $this->getLibraryItems(),
]);
}
public function getLibraryItems(): array
{
if ($this->libraryItems !== null) {
return $this->libraryItems;
}
$baseUrl = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
$media = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$items = [];
foreach ($this->collectionFactory->create()
->addFieldToFilter('status', 1)
->setOrder('video_id', 'DESC') as $video) {
$coverPath = (string)$video->getData('cover_path');
$extension = strtolower(pathinfo($coverPath, PATHINFO_EXTENSION)) ?: 'jpg';
$galleryFile = '/e/s/esun-library-' . (int)$video->getId() . '.' . $extension;
$tmpPath = $this->productMediaConfig->getTmpMediaPath($galleryFile);
if ($coverPath !== '' && $media->isExist($coverPath) && !$media->isExist($tmpPath)) {
$media->create(dirname($tmpPath));
$media->writeFile($tmpPath, $media->readFile($coverPath));
}
$items[] = [
'id' => (int)$video->getId(),
'title' => (string)$video->getData('title'),
'description' => (string)$video->getData('description'),
'video_url' => $baseUrl . ltrim((string)$video->getData('video_path'), '/'),
'cover_url' => $this->productMediaConfig->getTmpMediaUrl($galleryFile),
'gallery_file' => $galleryFile . '.tmp',
'duration' => (float)$video->getData('duration'),
'size' => (int)$video->getData('size'),
'width' => (int)$video->getData('width'),
'height' => (int)$video->getData('height'),
'resolution' => (string)$video->getData('resolution'),
];
}
return $this->libraryItems = $items;
}
}
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Esun\Video\Block\Adminhtml\Video;
use Esun\Video\Model\Video;
use Esun\Video\Model\VideoFactory;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
class EditBlock extends Template
{
private ?Video $video = null;
public function __construct(
Context $context,
private readonly VideoFactory $videoFactory,
private readonly StoreManagerInterface $storeManager,
array $data = []
) {
parent::__construct($context, $data);
}
public function getVideo(): Video
{
if ($this->video === null) {
$this->video = $this->videoFactory->create()
->load((int)$this->getRequest()->getParam('id'));
if (!$this->video->getId()) {
throw new NoSuchEntityException(__('The video no longer exists.'));
}
}
return $this->video;
}
public function getSaveUrl(): string
{
return $this->getUrl('esun_video/video/save');
}
public function getBackUrl(): string
{
return $this->getUrl('esun_video/video/index');
}
public function getMediaUrl(string $path): string
{
return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . ltrim($path, '/');
}
}
@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace Esun\Video\Block\Adminhtml\Video;
use Esun\Video\Model\Config;
use Esun\Video\Model\MediaBinaryStatus;
use Esun\Video\Model\ResourceModel\Video\CollectionFactory;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
class ListBlock extends Template
{
private ?\Esun\Video\Model\ResourceModel\Video\Collection $videos = null;
public function __construct(
Context $context,
private readonly CollectionFactory $collectionFactory,
private readonly StoreManagerInterface $storeManager,
private readonly Config $config,
private readonly MediaBinaryStatus $mediaBinaryStatus,
private readonly Json $json,
array $data = []
) {
parent::__construct($context, $data);
}
public function getVideos()
{
if ($this->videos === null) {
$this->videos = $this->collectionFactory->create()->setOrder('video_id', 'DESC');
$title = $this->getTitleFilter();
$status = $this->getStatusFilter();
if ($title !== '') {
$this->videos->addFieldToFilter('title', ['like' => '%' . $title . '%']);
}
if ($status !== '') {
$this->videos->addFieldToFilter('status', (int)$status);
}
}
return $this->videos;
}
public function getTitleFilter(): string
{
return trim((string)$this->getRequest()->getParam('title'));
}
public function getStatusFilter(): string
{
$status = (string)$this->getRequest()->getParam('status');
return in_array($status, ['0', '1'], true) ? $status : '';
}
public function getFilterUrl(): string
{
return $this->getUrl('esun_video/video/index');
}
public function getMissingBinaries(): array
{
return $this->mediaBinaryStatus->getMissingBinaries();
}
public function getMediaUrl(string $path): string
{
return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . ltrim($path, '/');
}
public function getDeleteUrl(int $id): string
{
return $this->getUrl('esun_video/video/delete', ['id' => $id]);
}
public function getEditUrl(int $id): string
{
return $this->getUrl('esun_video/video/edit', ['id' => $id]);
}
public function getUploadOptionsJson(): string
{
return $this->json->serialize([
'enabled' => $this->config->isEnabled(),
'uploadUrl' => $this->getUrl('esun_video/upload/chunk'),
'indexUrl' => $this->getUrl('esun_video/video/index'),
'chunkSize' => $this->config->getChunkSizeBytes(),
]);
}
public function isUploadEnabled(): bool
{
return $this->config->isEnabled();
}
}