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;
}
}