mirror of
https://github.com/getgrav/grav.git
synced 2026-02-23 15:11:27 +01:00
Improve Media handling in Flex
This commit is contained in:
@@ -488,7 +488,7 @@ abstract class Folder
|
||||
* Does a directory contain children
|
||||
*
|
||||
* @param string $directory
|
||||
* @return int
|
||||
* @return int|false
|
||||
*/
|
||||
public static function countChildren($directory) {
|
||||
if (!is_dir($directory)) {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @package Grav\Common\Flex
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Common\Flex\Types\Users\Traits;
|
||||
|
||||
use Grav\Common\Page\Medium\ImageMedium;
|
||||
use Grav\Common\Page\Medium\StaticImageMedium;
|
||||
|
||||
trait UserObjectLegacyTrait
|
||||
{
|
||||
/**
|
||||
* Merge two configurations together.
|
||||
*
|
||||
* @param array $data
|
||||
* @return $this
|
||||
* @deprecated 1.6 Use `->update($data)` instead (same but with data validation & filtering, file upload support).
|
||||
*/
|
||||
public function merge(array $data)
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use ->update($data) method instead', E_USER_DEPRECATED);
|
||||
|
||||
$this->setElements($this->getBlueprint()->mergeData($this->toArray(), $data));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return media object for the User's avatar.
|
||||
*
|
||||
* @return ImageMedium|StaticImageMedium|null
|
||||
* @deprecated 1.6 Use ->getAvatarImage() method instead.
|
||||
*/
|
||||
public function getAvatarMedia()
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use ->getAvatarImage() method instead', E_USER_DEPRECATED);
|
||||
|
||||
return $this->getAvatarImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the User's avatar URL
|
||||
*
|
||||
* @return string
|
||||
* @deprecated 1.6 Use ->getAvatarUrl() method instead.
|
||||
*/
|
||||
public function avatarUrl()
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use ->getAvatarUrl() method instead', E_USER_DEPRECATED);
|
||||
|
||||
return $this->getAvatarUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks user authorization to the action.
|
||||
* Ensures backwards compatibility
|
||||
*
|
||||
* @param string $action
|
||||
* @return bool
|
||||
* @deprecated 1.5 Use ->authorize() method instead.
|
||||
*/
|
||||
public function authorise($action)
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use ->authorize() method instead', E_USER_DEPRECATED);
|
||||
|
||||
return $this->authorize($action) ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Countable interface.
|
||||
*
|
||||
* @return int
|
||||
* @deprecated 1.6 Method makes no sense for user account.
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6', E_USER_DEPRECATED);
|
||||
|
||||
return \count($this->jsonSerialize());
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,10 @@ namespace Grav\Common\Flex\Types\Users;
|
||||
use Grav\Common\Config\Config;
|
||||
use Grav\Common\Flex\Traits\FlexGravTrait;
|
||||
use Grav\Common\Flex\Traits\FlexObjectTrait;
|
||||
use Grav\Common\Flex\Types\Users\Traits\UserObjectLegacyTrait;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaUploadInterface;
|
||||
use Grav\Common\Page\Media;
|
||||
use Grav\Common\Page\Medium\ImageMedium;
|
||||
use Grav\Common\Page\Medium\Medium;
|
||||
@@ -69,6 +71,7 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
getMediaFolder as private getFlexMediaFolder;
|
||||
}
|
||||
use UserTrait;
|
||||
use UserObjectLegacyTrait;
|
||||
|
||||
/** @var array|null */
|
||||
protected $_uploads_original;
|
||||
@@ -121,7 +124,10 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
$this->defProperty('state', 'enabled');
|
||||
}
|
||||
|
||||
public function onPrepareRegistration()
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function onPrepareRegistration(): void
|
||||
{
|
||||
if (!$this->getProperty('access')) {
|
||||
/** @var Config $config */
|
||||
@@ -138,7 +144,7 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
/**
|
||||
* Helper to get content editor will fall back if not set
|
||||
*
|
||||
* @return String
|
||||
* @return string
|
||||
*/
|
||||
public function getContentEditor(): string
|
||||
{
|
||||
@@ -412,7 +418,7 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
* Get value from the configuration and join it with given data.
|
||||
*
|
||||
* @param string $name Dot separated path to the requested value.
|
||||
* @param array|object $value Value to be joined.
|
||||
* @param array|object $value Value to be joined.
|
||||
* @param string $separator Separator, defaults to '.'
|
||||
* @return array
|
||||
* @throws \RuntimeException
|
||||
@@ -526,6 +532,8 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
|
||||
/**
|
||||
* Save user without the username
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
@@ -549,23 +557,6 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
return parent::save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserInterface $user
|
||||
* @param string $action
|
||||
* @param string $scope
|
||||
* @param bool $isMe
|
||||
* @return bool|null
|
||||
*/
|
||||
protected function isAuthorizedOverride(UserInterface $user, string $action, string $scope, bool $isMe = false): ?bool
|
||||
{
|
||||
if ($user instanceof self && $user->getStorageKey() === $this->getStorageKey()) {
|
||||
// User cannot delete his own account, otherwise he has full access.
|
||||
return $action !== 'delete';
|
||||
}
|
||||
|
||||
return parent::isAuthorizedOverride($user, $action, $scope, $isMe);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -579,133 +570,6 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge two configurations together.
|
||||
*
|
||||
* @param array $data
|
||||
* @return $this
|
||||
* @deprecated 1.6 Use `->update($data)` instead (same but with data validation & filtering, file upload support).
|
||||
*/
|
||||
public function merge(array $data)
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use ->update($data) method instead', E_USER_DEPRECATED);
|
||||
|
||||
$this->setElements($this->getBlueprint()->mergeData($this->toArray(), $data));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return media object for the User's avatar.
|
||||
*
|
||||
* @return ImageMedium|StaticImageMedium|null
|
||||
* @deprecated 1.6 Use ->getAvatarImage() method instead.
|
||||
*/
|
||||
public function getAvatarMedia()
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use ->getAvatarImage() method instead', E_USER_DEPRECATED);
|
||||
|
||||
return $this->getAvatarImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the User's avatar URL
|
||||
*
|
||||
* @return string
|
||||
* @deprecated 1.6 Use ->getAvatarUrl() method instead.
|
||||
*/
|
||||
public function avatarUrl()
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use ->getAvatarUrl() method instead', E_USER_DEPRECATED);
|
||||
|
||||
return $this->getAvatarUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks user authorization to the action.
|
||||
* Ensures backwards compatibility
|
||||
*
|
||||
* @param string $action
|
||||
* @return bool
|
||||
* @deprecated 1.5 Use ->authorize() method instead.
|
||||
*/
|
||||
public function authorise($action)
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use ->authorize() method instead', E_USER_DEPRECATED);
|
||||
|
||||
return $this->authorize($action) ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Countable interface.
|
||||
*
|
||||
* @return int
|
||||
* @deprecated 1.6 Method makes no sense for user account.
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6', E_USER_DEPRECATED);
|
||||
|
||||
return \count($this->jsonSerialize());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserGroupIndex|UserGroupCollection
|
||||
*/
|
||||
protected function getGroups()
|
||||
{
|
||||
if (null === $this->_groups) {
|
||||
$this->_groups = $this->getUserGroups()->select((array)$this->getProperty('groups'));
|
||||
}
|
||||
|
||||
return $this->_groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Access
|
||||
*/
|
||||
protected function getAccess(): Access
|
||||
{
|
||||
if (null === $this->_access) {
|
||||
$this->getProperty('access');
|
||||
}
|
||||
|
||||
return $this->_access;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return array
|
||||
*/
|
||||
protected function offsetLoad_access($value): array
|
||||
{
|
||||
if (!$value instanceof Access) {
|
||||
$value = new Access($value);
|
||||
}
|
||||
|
||||
$this->_access = $value;
|
||||
|
||||
return $value->jsonSerialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return array
|
||||
*/
|
||||
protected function offsetPrepare_access($value): array
|
||||
{
|
||||
return $this->offsetLoad_access($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $value
|
||||
* @return array|null
|
||||
*/
|
||||
protected function offsetSerialize_access(?array $value): ?array
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MediaCollectionInterface
|
||||
*/
|
||||
@@ -746,6 +610,23 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
return $folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserInterface $user
|
||||
* @param string $action
|
||||
* @param string $scope
|
||||
* @param bool $isMe
|
||||
* @return bool|null
|
||||
*/
|
||||
protected function isAuthorizedOverride(UserInterface $user, string $action, string $scope, bool $isMe = false): ?bool
|
||||
{
|
||||
if ($user instanceof self && $user->getStorageKey() === $this->getStorageKey()) {
|
||||
// User cannot delete his own account, otherwise he has full access.
|
||||
return $action !== 'delete';
|
||||
}
|
||||
|
||||
return parent::isAuthorizedOverride($user, $action, $scope, $isMe);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
@@ -845,14 +726,19 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
|
||||
protected function saveUpdatedMedia(): void
|
||||
{
|
||||
$media = $this->getFlexMedia();
|
||||
if (!$media instanceof MediaUploadInterface) {
|
||||
throw new \RuntimeException('Internal error M101');
|
||||
}
|
||||
|
||||
// Upload/delete original sized images.
|
||||
/** @var FormFlashFile|null $file */
|
||||
foreach ($this->_uploads_original ?? [] as $name => $file) {
|
||||
$name = 'original/' . $name;
|
||||
if ($file) {
|
||||
$this->uploadMediaFile($file, $name);
|
||||
$media->uploadFile($file, $name);
|
||||
} else {
|
||||
$this->deleteMediaFile($name);
|
||||
$media->deleteFile($name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -862,9 +748,9 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
*/
|
||||
foreach ($this->getUpdatedMedia() as $filename => $file) {
|
||||
if ($file) {
|
||||
$this->uploadMediaFile($file, $filename);
|
||||
$media->uploadFile($file, $filename);
|
||||
} else {
|
||||
$this->deleteMediaFile($filename);
|
||||
$media->deleteFile($filename);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -943,4 +829,61 @@ class UserObject extends FlexObject implements UserInterface, MediaManipulationI
|
||||
|
||||
return $grav['user_groups'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserGroupIndex|UserGroupCollection
|
||||
*/
|
||||
protected function getGroups()
|
||||
{
|
||||
if (null === $this->_groups) {
|
||||
$this->_groups = $this->getUserGroups()->select((array)$this->getProperty('groups'));
|
||||
}
|
||||
|
||||
return $this->_groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Access
|
||||
*/
|
||||
protected function getAccess(): Access
|
||||
{
|
||||
if (null === $this->_access) {
|
||||
$this->getProperty('access');
|
||||
}
|
||||
|
||||
return $this->_access;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return array
|
||||
*/
|
||||
protected function offsetLoad_access($value): array
|
||||
{
|
||||
if (!$value instanceof Access) {
|
||||
$value = new Access($value);
|
||||
}
|
||||
|
||||
$this->_access = $value;
|
||||
|
||||
return $value->jsonSerialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return array
|
||||
*/
|
||||
protected function offsetPrepare_access($value): array
|
||||
{
|
||||
return $this->offsetLoad_access($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $value
|
||||
* @return array|null
|
||||
*/
|
||||
protected function offsetSerialize_access(?array $value): ?array
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ use Grav\Common\Data\Data;
|
||||
|
||||
/**
|
||||
* Class implements media object interface.
|
||||
*
|
||||
* @property string $type
|
||||
*/
|
||||
interface MediaObjectInterface extends \Grav\Framework\Media\Interfaces\MediaObjectInterface
|
||||
{
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Common\Media
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Common\Media\Interfaces;
|
||||
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Implements media upload and delete functionality.
|
||||
*/
|
||||
interface MediaUploadInterface
|
||||
{
|
||||
/**
|
||||
* @param UploadedFileInterface $uploadedFile
|
||||
* @param string|null $filename
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function checkUploadedFile(UploadedFileInterface $uploadedFile, string $filename = null): string;
|
||||
|
||||
/**
|
||||
* Upload file to the media collection.
|
||||
*
|
||||
* @param UploadedFileInterface $uploadedFile
|
||||
* @param string|null $filename
|
||||
* @return void
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function uploadFile(UploadedFileInterface $uploadedFile, string $filename = null): void;
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return void
|
||||
*/
|
||||
public function deleteFile(string $filename): void;
|
||||
}
|
||||
@@ -11,7 +11,6 @@ namespace Grav\Common\Media\Traits;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\ImageMediaInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaObjectInterface;
|
||||
use Grav\Common\Page\Medium\ImageFile;
|
||||
use Grav\Common\Page\Medium\ImageMedium;
|
||||
use Grav\Common\Page\Medium\MediumFactory;
|
||||
@@ -319,7 +318,7 @@ trait ImageMediaTrait
|
||||
/**
|
||||
* Return the image higher quality version
|
||||
*
|
||||
* @return ImageMediaInterface the alternative version with higher quality
|
||||
* @return ImageMediaInterface|$this the alternative version with higher quality
|
||||
*/
|
||||
public function higherQualityAlternative()
|
||||
{
|
||||
|
||||
@@ -29,7 +29,6 @@ trait MediaUploadTrait
|
||||
/**
|
||||
* @param UploadedFileInterface $uploadedFile
|
||||
* @param string|null $filename
|
||||
* @return string $filename
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function checkUploadedFile(UploadedFileInterface $uploadedFile, string $filename = null): string
|
||||
@@ -83,7 +82,7 @@ trait MediaUploadTrait
|
||||
* @return void
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function uploadMediaFile(UploadedFileInterface $uploadedFile, string $filename = null): void
|
||||
public function uploadFile(UploadedFileInterface $uploadedFile, string $filename = null): void
|
||||
{
|
||||
// First check if the file is a valid upload (throws error if not).
|
||||
$filename = $this->checkUploadedFile($uploadedFile, $filename);
|
||||
@@ -94,7 +93,12 @@ trait MediaUploadTrait
|
||||
}
|
||||
|
||||
try {
|
||||
$this->clearMediaCache();
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $this->getGrav()['locator'];
|
||||
if ($locator->isStream($path)) {
|
||||
$path = (string)$locator->findResource($path, true, true);
|
||||
$locator->clearCache($path);
|
||||
}
|
||||
|
||||
$filepath = sprintf('%s/%s', $path, $filename);
|
||||
|
||||
@@ -134,8 +138,9 @@ trait MediaUploadTrait
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return void
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function deleteMediaFile(string $filename): void
|
||||
public function deleteFile(string $filename): void
|
||||
{
|
||||
// First check for allowed filename.
|
||||
$basename = basename($filename);
|
||||
@@ -214,6 +219,7 @@ trait MediaUploadTrait
|
||||
*
|
||||
* @param string $filename
|
||||
* @return void
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function checkFileExtension(string $filename)
|
||||
{
|
||||
@@ -231,13 +237,17 @@ trait MediaUploadTrait
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $grav['locator'];
|
||||
if ($locator->isStream($path)) {
|
||||
if ($path && $locator->isStream($path)) {
|
||||
$path = (string)$locator->findResource($path, true, true);
|
||||
$locator->clearCache($path);
|
||||
}
|
||||
}
|
||||
|
||||
protected function translate($string): string
|
||||
/**
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
protected function translate(string $string): string
|
||||
{
|
||||
return $this->getLanguage()->translate($string);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Grav\Common\Page;
|
||||
use Grav\Common\Config\Config;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Language\Language;
|
||||
use Grav\Common\Media\Interfaces\MediaUploadInterface;
|
||||
use Grav\Common\Media\Traits\MediaUploadTrait;
|
||||
use Grav\Common\Yaml;
|
||||
use Grav\Common\Page\Medium\AbstractMedia;
|
||||
@@ -20,7 +21,7 @@ use Grav\Common\Page\Medium\MediumFactory;
|
||||
use RocketTheme\Toolbox\File\File;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
|
||||
class Media extends AbstractMedia
|
||||
class Media extends AbstractMedia implements MediaUploadInterface
|
||||
{
|
||||
use MediaUploadTrait;
|
||||
|
||||
@@ -122,6 +123,10 @@ class Media extends AbstractMedia
|
||||
foreach ($media as $name => $types) {
|
||||
// First prepare the alternatives in case there is no base medium
|
||||
if (!empty($types['alternative'])) {
|
||||
/**
|
||||
* @var string|int $ratio
|
||||
* @var array $alt
|
||||
*/
|
||||
foreach ($types['alternative'] as $ratio => &$alt) {
|
||||
$alt['file'] = MediumFactory::fromFile($alt['file']);
|
||||
|
||||
|
||||
@@ -164,10 +164,12 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
|
||||
*/
|
||||
public function add($name, $file)
|
||||
{
|
||||
if (!$file) {
|
||||
if (null === $file) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->offsetSet($name, $file);
|
||||
|
||||
switch ($file->type) {
|
||||
case 'image':
|
||||
$this->images[$name] = $file;
|
||||
@@ -252,6 +254,6 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
|
||||
}
|
||||
}
|
||||
|
||||
return array($name, $extension, $type, $extra);
|
||||
return [$name, $extension, $type, $extra];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,8 +50,11 @@ class GlobalMedia extends AbstractMedia
|
||||
{
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = Grav::instance()['locator'];
|
||||
if (!$locator->isStream($filename)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $locator->isStream($filename) ? ($locator->findResource($filename) ?: null) : null;
|
||||
return $locator->findResource($filename) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,10 +69,10 @@ class GlobalMedia extends AbstractMedia
|
||||
}
|
||||
|
||||
$path = dirname($filename);
|
||||
list($basename, $ext,, $extra) = $this->getFileParts(basename($filename));
|
||||
[$basename, $ext,, $extra] = $this->getFileParts(basename($filename));
|
||||
$medium = MediumFactory::fromFile($filename);
|
||||
|
||||
if (empty($medium)) {
|
||||
if (null === $medium) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ namespace Grav\Common\Page\Medium;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Data\Blueprint;
|
||||
use Grav\Common\Media\Interfaces\ImageMediaInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaObjectInterface;
|
||||
use Grav\Framework\Form\FormFlashFile;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
@@ -160,14 +162,14 @@ class MediumFactory
|
||||
/**
|
||||
* Create a new ImageMedium by scaling another ImageMedium object.
|
||||
*
|
||||
* @param ImageMedium|Medium $medium
|
||||
* @param ImageMediaInterface|MediaObjectInterface $medium
|
||||
* @param int $from
|
||||
* @param int $to
|
||||
* @return Medium|array
|
||||
* @return ImageMediaInterface|MediaObjectInterface|array
|
||||
*/
|
||||
public static function scaledFromMedium($medium, $from, $to)
|
||||
{
|
||||
if (! $medium instanceof ImageMedium) {
|
||||
if (!$medium instanceof ImageMedium) {
|
||||
return $medium;
|
||||
}
|
||||
|
||||
|
||||
@@ -177,7 +177,7 @@ class User extends Data implements UserInterface
|
||||
/**
|
||||
* Serialize user.
|
||||
*
|
||||
* @return array
|
||||
* @return string[]
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
|
||||
@@ -13,6 +13,7 @@ use Grav\Common\Config\Config;
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaUploadInterface;
|
||||
use Grav\Common\Media\Traits\MediaTrait;
|
||||
use Grav\Common\Page\Medium\Medium;
|
||||
use Grav\Common\Page\Medium\MediumFactory;
|
||||
@@ -38,13 +39,6 @@ trait FlexMediaTrait
|
||||
/** @var array */
|
||||
protected $_uploads;
|
||||
|
||||
public function __debugInfo()
|
||||
{
|
||||
return parent::__debugInfo() + [
|
||||
'uploads:private' => $this->getUpdatedMedia()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
@@ -68,27 +62,10 @@ trait FlexMediaTrait
|
||||
{
|
||||
$media = $this->media;
|
||||
if (null === $media) {
|
||||
$updated = false;
|
||||
$media = $this->getExistingMedia();
|
||||
|
||||
// Include uploaded media to the object media.
|
||||
/**
|
||||
* @var string $filename
|
||||
* @var UploadedFileInterface|null $upload
|
||||
*/
|
||||
foreach ($this->getUpdatedMedia() as $filename => $upload) {
|
||||
if ($upload) {
|
||||
$medium = MediumFactory::fromUploadedFile($upload);
|
||||
if ($medium) {
|
||||
$updated = true;
|
||||
$media->add($filename, $medium);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($updated) {
|
||||
$media->setTimestamps();
|
||||
}
|
||||
$this->addUpdatedMedia($media);
|
||||
}
|
||||
|
||||
return $media;
|
||||
@@ -100,6 +77,15 @@ trait FlexMediaTrait
|
||||
*/
|
||||
public function checkUploadedMediaFile(UploadedFileInterface $uploadedFile)
|
||||
{
|
||||
$media = $this->getMedia();
|
||||
if ($media instanceof MediaUploadInterface) {
|
||||
$media->checkUploadedFile($uploadedFile);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
user_error(__METHOD__ . '() with the old Media classes is deprecated since Grav 1.7, Use Media class that implements MediaUploadInterface instead', E_USER_DEPRECATED);
|
||||
|
||||
$grav = Grav::instance();
|
||||
$language = $grav['language'];
|
||||
|
||||
@@ -134,27 +120,6 @@ trait FlexMediaTrait
|
||||
$this->checkMediaFilename($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return void
|
||||
*/
|
||||
public function checkMediaFilename(string $filename)
|
||||
{
|
||||
// Check the file extension.
|
||||
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
||||
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var Config $config */
|
||||
$config = $grav['config'];
|
||||
|
||||
// If not a supported type, return
|
||||
if (!$extension || !$config->get("media.types.{$extension}")) {
|
||||
$language = $grav['language'];
|
||||
throw new RuntimeException($language->translate('PLUGIN_ADMIN.UNSUPPORTED_FILE_TYPE') . ': ' . $extension, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UploadedFileInterface $uploadedFile
|
||||
* @param string|null $filename
|
||||
@@ -162,6 +127,25 @@ trait FlexMediaTrait
|
||||
*/
|
||||
public function uploadMediaFile(UploadedFileInterface $uploadedFile, string $filename = null): void
|
||||
{
|
||||
$media = $this->getMedia();
|
||||
if ($media instanceof MediaUploadInterface) {
|
||||
$media->uploadFile($uploadedFile, $filename);
|
||||
$this->clearMediaCache();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
user_error(__METHOD__ . '() with the old Media classes is deprecated since Grav 1.7, Use Media class that implements MediaUploadInterface instead', E_USER_DEPRECATED);
|
||||
|
||||
$grav = Grav::instance();
|
||||
|
||||
$path = $media->getPath();
|
||||
if (!$path) {
|
||||
$language = $grav['language'];
|
||||
|
||||
throw new RuntimeException($language->translate('PLUGIN_ADMIN.FAILED_TO_MOVE_UPLOADED_FILE'), 400);
|
||||
}
|
||||
|
||||
$this->checkUploadedMediaFile($uploadedFile);
|
||||
|
||||
if ($filename) {
|
||||
@@ -170,18 +154,8 @@ trait FlexMediaTrait
|
||||
$filename = $uploadedFile->getClientFilename();
|
||||
}
|
||||
|
||||
$media = $this->getMedia();
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $grav['locator'];
|
||||
$path = $media->getPath();
|
||||
if (!$path) {
|
||||
$language = $grav['language'];
|
||||
|
||||
throw new RuntimeException($language->translate('PLUGIN_ADMIN.FAILED_TO_MOVE_UPLOADED_FILE'), 400);
|
||||
}
|
||||
|
||||
if ($locator->isStream($path)) {
|
||||
$path = (string)$locator->findResource($path, true, true);
|
||||
$locator->clearCache($path);
|
||||
@@ -225,6 +199,21 @@ trait FlexMediaTrait
|
||||
*/
|
||||
public function deleteMediaFile(string $filename): void
|
||||
{
|
||||
$media = $this->getMedia();
|
||||
if ($media instanceof MediaUploadInterface) {
|
||||
$media->deleteFile($filename);
|
||||
$this->clearMediaCache();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
user_error(__METHOD__ . '() with the old Media classes is deprecated since Grav 1.7, Use Media class that implements MediaUploadInterface instead', E_USER_DEPRECATED);
|
||||
|
||||
$path = $media->getPath();
|
||||
if (!$path) {
|
||||
return;
|
||||
}
|
||||
|
||||
$grav = Grav::instance();
|
||||
$language = $grav['language'];
|
||||
|
||||
@@ -238,12 +227,6 @@ trait FlexMediaTrait
|
||||
throw new RuntimeException($language->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': Bad filename: ' . $filename, 400);
|
||||
}
|
||||
|
||||
$media = $this->getMedia();
|
||||
$path = $media->getPath();
|
||||
if (!$path) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $grav['locator'];
|
||||
|
||||
@@ -299,6 +282,13 @@ trait FlexMediaTrait
|
||||
$this->clearMediaCache();
|
||||
}
|
||||
|
||||
public function __debugInfo()
|
||||
{
|
||||
return parent::__debugInfo() + [
|
||||
'uploads:private' => $this->getUpdatedMedia()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $files
|
||||
* @return void
|
||||
@@ -319,7 +309,28 @@ trait FlexMediaTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<UploadedFileInterface|null>
|
||||
* @param MediaCollectionInterface $media
|
||||
*/
|
||||
protected function addUpdatedMedia(MediaCollectionInterface $media): void
|
||||
{
|
||||
$updated = false;
|
||||
foreach ($this->getUpdatedMedia() as $filename => $upload) {
|
||||
if ($upload) {
|
||||
$medium = MediumFactory::fromUploadedFile($upload);
|
||||
if ($medium) {
|
||||
$updated = true;
|
||||
$media->add($filename, $medium);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($updated) {
|
||||
$media->setTimestamps();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, UploadedFileInterface|null>
|
||||
*/
|
||||
protected function getUpdatedMedia(): array
|
||||
{
|
||||
@@ -395,4 +406,28 @@ trait FlexMediaTrait
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getStorageKey(): string;
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return void
|
||||
* @deprecated 1.7 Use Media class that implements MediaUploadInterface instead.
|
||||
*/
|
||||
public function checkMediaFilename(string $filename)
|
||||
{
|
||||
user_error(__METHOD__ . '() is deprecated since Grav 1.7, Use Media class that implements MediaUploadInterface instead', E_USER_DEPRECATED);
|
||||
|
||||
// Check the file extension.
|
||||
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
||||
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var Config $config */
|
||||
$config = $grav['config'];
|
||||
|
||||
// If not a supported type, return
|
||||
if (!$extension || !$config->get("media.types.{$extension}")) {
|
||||
$language = $grav['language'];
|
||||
throw new RuntimeException($language->translate('PLUGIN_ADMIN.UNSUPPORTED_FILE_TYPE') . ': ' . $extension, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user