IMPR: Add video files

This commit is contained in:
Tony Air 2022-07-08 19:41:13 +02:00
parent c3c268488e
commit e2c9f0a376

View File

@ -9,6 +9,7 @@
namespace A2nt\ElementalBasics\Extensions;
use A2nt\ElementalBasics\Elements\SliderElement;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\File;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField;
@ -37,6 +38,10 @@ class SlideImageEx extends DataExtension
'VideoFile' => File::class,
];
private static $owns = [
'VideoFile',
];
private $_cache = [
'element' => [],
];
@ -87,6 +92,14 @@ class SlideImageEx extends DataExtension
return $element->getSlideHeight();
}
public static function formatBytes($size, $precision = 2)
{
$base = log($size, 1024);
$suffixes = array('', 'K', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
}
public function updateCMSFields(FieldList $fields)
{
parent::updateCMSFields($fields);
@ -98,6 +111,17 @@ class SlideImageEx extends DataExtension
'DateOff',
]);
$videoUpload = UploadField::create('VideoFile')
->setAllowedExtensions(['mp4'])
->setFolderName('Uploads/SlideVideos');
$videoUpload->setTitle(
'Video File (max size: '
.self::formatBytes($videoUpload->getValidator()->getAllowedMaxFileSize('mp4'))
.')'
);
$fields->insertAfter('Headline', $videoUpload);
$fields->dataFieldByName('Image')
->setTitle('Image ('.$this->getSlideWidth().' x '.$this->getSlideHeight().' px)');