init
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
class Config
|
||||
{
|
||||
private const PREFIX = 'esun_video/general/';
|
||||
|
||||
public function __construct(private readonly ScopeConfigInterface $scopeConfig)
|
||||
{
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->scopeConfig->isSetFlag(self::PREFIX . 'enabled', ScopeInterface::SCOPE_WEBSITE);
|
||||
}
|
||||
|
||||
public function getMediaPath(): string
|
||||
{
|
||||
$path = trim((string)$this->scopeConfig->getValue(self::PREFIX . 'media_path'), '/ ');
|
||||
return $path !== '' ? $path : 'esun/video';
|
||||
}
|
||||
|
||||
public function getChunkSizeBytes(): int
|
||||
{
|
||||
return max(1, (int)$this->scopeConfig->getValue(self::PREFIX . 'chunk_size')) * 1024 * 1024;
|
||||
}
|
||||
|
||||
public function getFfprobeBinary(): string
|
||||
{
|
||||
return trim((string)$this->scopeConfig->getValue(self::PREFIX . 'ffprobe_binary')) ?: 'ffprobe';
|
||||
}
|
||||
|
||||
public function getFfmpegBinary(): string
|
||||
{
|
||||
return trim((string)$this->scopeConfig->getValue(self::PREFIX . 'ffmpeg_binary')) ?: 'ffmpeg';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model\Config\Backend;
|
||||
|
||||
use Esun\Video\Model\RemoteStorage\ConfigSynchronizer;
|
||||
use Magento\Framework\App\Config\Value;
|
||||
|
||||
class RemoteStorage extends Value
|
||||
{
|
||||
public function __construct(
|
||||
\Magento\Framework\Model\Context $context,
|
||||
\Magento\Framework\Registry $registry,
|
||||
\Magento\Framework\App\Config\ScopeConfigInterface $config,
|
||||
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
|
||||
private readonly ConfigSynchronizer $configSynchronizer,
|
||||
?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
|
||||
?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
|
||||
}
|
||||
|
||||
public function beforeSave()
|
||||
{
|
||||
if ($this->getScope() !== 'default') {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Media storage can only be configured at Default Config scope.')
|
||||
);
|
||||
}
|
||||
return parent::beforeSave();
|
||||
}
|
||||
|
||||
public function afterSave()
|
||||
{
|
||||
$result = parent::afterSave();
|
||||
$this->configSynchronizer->synchronize($this);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model\Config\Backend;
|
||||
|
||||
use Esun\Video\Model\RemoteStorage\ConfigSynchronizer;
|
||||
use Magento\Config\Model\Config\Backend\Encrypted;
|
||||
|
||||
class RemoteStorageEncrypted extends Encrypted
|
||||
{
|
||||
public function __construct(
|
||||
\Magento\Framework\Model\Context $context,
|
||||
\Magento\Framework\Registry $registry,
|
||||
\Magento\Framework\App\Config\ScopeConfigInterface $config,
|
||||
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
|
||||
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
|
||||
private readonly ConfigSynchronizer $configSynchronizer,
|
||||
?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
|
||||
?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct(
|
||||
$context,
|
||||
$registry,
|
||||
$config,
|
||||
$cacheTypeList,
|
||||
$encryptor,
|
||||
$resource,
|
||||
$resourceCollection,
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
public function beforeSave()
|
||||
{
|
||||
if ($this->getScope() !== 'default') {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Media storage can only be configured at Default Config scope.')
|
||||
);
|
||||
}
|
||||
return parent::beforeSave();
|
||||
}
|
||||
|
||||
public function afterSave()
|
||||
{
|
||||
$result = parent::afterSave();
|
||||
$this->configSynchronizer->synchronize($this);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model\Config\Source;
|
||||
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class StorageDriver implements OptionSourceInterface
|
||||
{
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
return [
|
||||
['value' => 'file', 'label' => __('Local (pub/media)')],
|
||||
['value' => 'aws-s3', 'label' => __('AWS S3')],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class Ffmpeg
|
||||
{
|
||||
public function __construct(private readonly Config $config)
|
||||
{
|
||||
}
|
||||
|
||||
public function createCover(string $videoFile, string $coverFile): void
|
||||
{
|
||||
$process = new Process([
|
||||
$this->config->getFfmpegBinary(),
|
||||
'-hide_banner',
|
||||
'-loglevel', 'error',
|
||||
'-ss', '1',
|
||||
'-i', $videoFile,
|
||||
'-frames:v', '1',
|
||||
'-q:v', '2',
|
||||
'-y',
|
||||
$coverFile,
|
||||
]);
|
||||
$process->setTimeout(60);
|
||||
|
||||
try {
|
||||
$process->run();
|
||||
} catch (\Throwable $e) {
|
||||
throw new LocalizedException(__('Unable to run ffmpeg: %1', $e->getMessage()));
|
||||
}
|
||||
|
||||
if (!$process->isSuccessful() || !is_file($coverFile) || filesize($coverFile) === 0) {
|
||||
throw new LocalizedException(
|
||||
__('ffmpeg failed to create the cover image: %1', trim($process->getErrorOutput()) ?: __('unknown error'))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class Ffprobe
|
||||
{
|
||||
public function __construct(private readonly Config $config)
|
||||
{
|
||||
}
|
||||
|
||||
public function inspect(string $absoluteFile): array
|
||||
{
|
||||
$process = new Process([
|
||||
$this->config->getFfprobeBinary(),
|
||||
'-v', 'error',
|
||||
'-select_streams', 'v:0',
|
||||
'-show_entries', 'format=format_name,duration,size:stream=width,height,codec_name',
|
||||
'-of', 'json',
|
||||
$absoluteFile,
|
||||
]);
|
||||
$process->setTimeout(60);
|
||||
try {
|
||||
$process->run();
|
||||
} catch (\Throwable $e) {
|
||||
throw new LocalizedException(__('Unable to run ffprobe: %1', $e->getMessage()));
|
||||
}
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
throw new LocalizedException(
|
||||
__('ffprobe failed: %1', trim($process->getErrorOutput()) ?: __('unknown error'))
|
||||
);
|
||||
}
|
||||
|
||||
$data = json_decode($process->getOutput(), true);
|
||||
$stream = $data['streams'][0] ?? [];
|
||||
$format = $data['format'] ?? [];
|
||||
$formatName = strtolower((string)($format['format_name'] ?? ''));
|
||||
if (!str_contains($formatName, 'mp4') && !str_contains($formatName, 'mov')) {
|
||||
throw new LocalizedException(__('Only MP4 container files are supported.'));
|
||||
}
|
||||
$width = (int)($stream['width'] ?? 0);
|
||||
$height = (int)($stream['height'] ?? 0);
|
||||
if ($width <= 0 || $height <= 0) {
|
||||
throw new LocalizedException(__('The MP4 file does not contain a readable video stream.'));
|
||||
}
|
||||
|
||||
return [
|
||||
'duration' => round((float)($format['duration'] ?? 0), 3),
|
||||
'size' => (int)($format['size'] ?? filesize($absoluteFile)),
|
||||
'width' => $width,
|
||||
'height' => $height,
|
||||
'resolution' => $this->getResolution($width, $height),
|
||||
];
|
||||
}
|
||||
|
||||
private function getResolution(int $width, int $height): string
|
||||
{
|
||||
$longEdge = max($width, $height);
|
||||
$shortEdge = min($width, $height);
|
||||
if ($longEdge >= 3840 || $shortEdge >= 2160) {
|
||||
return '4K';
|
||||
}
|
||||
if ($longEdge >= 1920 || $shortEdge >= 1080) {
|
||||
return '1080p';
|
||||
}
|
||||
return $shortEdge . 'p';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class MediaBinaryStatus
|
||||
{
|
||||
public function __construct(private readonly Config $config)
|
||||
{
|
||||
}
|
||||
|
||||
public function getMissingBinaries(): array
|
||||
{
|
||||
$binaries = [
|
||||
'ffmpeg' => $this->config->getFfmpegBinary(),
|
||||
'ffprobe' => $this->config->getFfprobeBinary(),
|
||||
];
|
||||
$missing = [];
|
||||
|
||||
foreach ($binaries as $name => $binary) {
|
||||
if (!$this->isAvailable($binary)) {
|
||||
$missing[$name] = $binary;
|
||||
}
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
private function isAvailable(string $binary): bool
|
||||
{
|
||||
$process = new Process([$binary, '-version']);
|
||||
$process->setTimeout(5);
|
||||
|
||||
try {
|
||||
$process->run();
|
||||
return $process->isSuccessful();
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model\RemoteStorage;
|
||||
|
||||
use Magento\Framework\App\Cache\Type\Config as ConfigCache;
|
||||
use Magento\Framework\App\Cache\TypeListInterface;
|
||||
use Magento\Framework\App\Config\Value;
|
||||
use Magento\Framework\App\DeploymentConfig;
|
||||
use Magento\Framework\App\DeploymentConfig\Writer;
|
||||
use Magento\Framework\Config\File\ConfigFilePool;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
|
||||
class ConfigSynchronizer
|
||||
{
|
||||
private bool $synchronized = false;
|
||||
|
||||
public function __construct(
|
||||
private readonly DeploymentConfig $deploymentConfig,
|
||||
private readonly Writer $deploymentConfigWriter,
|
||||
private readonly TypeListInterface $cacheTypeList
|
||||
) {
|
||||
}
|
||||
|
||||
public function synchronize(Value $backend): void
|
||||
{
|
||||
if ($this->synchronized) {
|
||||
return;
|
||||
}
|
||||
|
||||
$driver = (string)$backend->getFieldsetDataValue('driver');
|
||||
if (!in_array($driver, ['file', 'aws-s3'], true)) {
|
||||
throw new LocalizedException(__('Unsupported media storage driver.'));
|
||||
}
|
||||
if (!$this->deploymentConfigWriter->checkIfWritable()) {
|
||||
throw new LocalizedException(__('app/etc/env.php is not writable by the PHP process.'));
|
||||
}
|
||||
|
||||
$remoteStorage = ['driver' => $driver];
|
||||
if ($driver === 'aws-s3') {
|
||||
$bucket = trim((string)$backend->getFieldsetDataValue('bucket'));
|
||||
$region = trim((string)$backend->getFieldsetDataValue('region'));
|
||||
if ($bucket === '' || $region === '') {
|
||||
throw new LocalizedException(__('S3 Bucket and AWS Region are required.'));
|
||||
}
|
||||
|
||||
$current = (array)$this->deploymentConfig->get('remote_storage', []);
|
||||
$currentCredentials = (array)($current['config']['credentials'] ?? []);
|
||||
$accessKey = $this->resolveSecret(
|
||||
(string)$backend->getFieldsetDataValue('access_key'),
|
||||
(string)($currentCredentials['key'] ?? '')
|
||||
);
|
||||
$secretKey = $this->resolveSecret(
|
||||
(string)$backend->getFieldsetDataValue('secret_key'),
|
||||
(string)($currentCredentials['secret'] ?? '')
|
||||
);
|
||||
|
||||
$remoteStorage['prefix'] = trim((string)$backend->getFieldsetDataValue('prefix'), '/ ');
|
||||
$remoteStorage['config'] = [
|
||||
'bucket' => $bucket,
|
||||
'region' => $region,
|
||||
'path_style' => (int)$backend->getFieldsetDataValue('path_style'),
|
||||
'credentials' => [
|
||||
'key' => $accessKey,
|
||||
'secret' => $secretKey,
|
||||
],
|
||||
];
|
||||
$endpoint = trim((string)$backend->getFieldsetDataValue('endpoint'));
|
||||
if ($endpoint !== '') {
|
||||
$remoteStorage['config']['endpoint'] = $endpoint;
|
||||
}
|
||||
}
|
||||
|
||||
$this->deploymentConfigWriter->saveConfig(
|
||||
[ConfigFilePool::APP_ENV => ['remote_storage' => $remoteStorage]],
|
||||
true,
|
||||
null,
|
||||
[],
|
||||
true
|
||||
);
|
||||
$this->cacheTypeList->cleanType(ConfigCache::TYPE_IDENTIFIER);
|
||||
$this->synchronized = true;
|
||||
}
|
||||
|
||||
private function resolveSecret(string $postedValue, string $currentValue): string
|
||||
{
|
||||
if (preg_match('/^\*+$/', $postedValue)) {
|
||||
return $currentValue;
|
||||
}
|
||||
|
||||
if ($postedValue === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $postedValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class Video extends AbstractDb
|
||||
{
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init('esun_video', 'video_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model\ResourceModel\Video;
|
||||
|
||||
use Esun\Video\Model\Video as VideoModel;
|
||||
use Esun\Video\Model\ResourceModel\Video as VideoResource;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init(VideoModel::class, VideoResource::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Video\Model;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
|
||||
class Video extends AbstractModel
|
||||
{
|
||||
public const STATUS_DISABLED = 0;
|
||||
public const STATUS_ENABLED = 1;
|
||||
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init(\Esun\Video\Model\ResourceModel\Video::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user