From 62e863dec00eda045c9614cfa1155ca29ed7f3c2 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 3 Jun 2020 18:06:37 +0300 Subject: [PATCH] Improve Media handling in Flex --- system/src/Grav/Common/Filesystem/Folder.php | 2 +- .../Users/Traits/UserObjectLegacyTrait.php | 88 +++++++ .../Common/Flex/Types/Users/UserObject.php | 245 +++++++----------- .../Media/Interfaces/MediaObjectInterface.php | 2 + .../Media/Interfaces/MediaUploadInterface.php | 42 +++ .../Common/Media/Traits/ImageMediaTrait.php | 3 +- .../Common/Media/Traits/MediaUploadTrait.php | 22 +- system/src/Grav/Common/Page/Media.php | 7 +- .../Grav/Common/Page/Medium/AbstractMedia.php | 6 +- .../Grav/Common/Page/Medium/GlobalMedia.php | 9 +- .../Grav/Common/Page/Medium/MediumFactory.php | 8 +- system/src/Grav/Common/User/DataUser/User.php | 2 +- .../Framework/Flex/Traits/FlexMediaTrait.php | 161 +++++++----- 13 files changed, 364 insertions(+), 233 deletions(-) create mode 100644 system/src/Grav/Common/Flex/Types/Users/Traits/UserObjectLegacyTrait.php create mode 100644 system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php diff --git a/system/src/Grav/Common/Filesystem/Folder.php b/system/src/Grav/Common/Filesystem/Folder.php index 3142060a3..5717f71f3 100644 --- a/system/src/Grav/Common/Filesystem/Folder.php +++ b/system/src/Grav/Common/Filesystem/Folder.php @@ -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)) { diff --git a/system/src/Grav/Common/Flex/Types/Users/Traits/UserObjectLegacyTrait.php b/system/src/Grav/Common/Flex/Types/Users/Traits/UserObjectLegacyTrait.php new file mode 100644 index 000000000..25fb43303 --- /dev/null +++ b/system/src/Grav/Common/Flex/Types/Users/Traits/UserObjectLegacyTrait.php @@ -0,0 +1,88 @@ +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()); + } +} diff --git a/system/src/Grav/Common/Flex/Types/Users/UserObject.php b/system/src/Grav/Common/Flex/Types/Users/UserObject.php index 077573c6f..32e321036 100644 --- a/system/src/Grav/Common/Flex/Types/Users/UserObject.php +++ b/system/src/Grav/Common/Flex/Types/Users/UserObject.php @@ -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; + } } diff --git a/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php b/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php index c749cdbf2..bc2db439f 100644 --- a/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php +++ b/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php @@ -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 { diff --git a/system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php b/system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php new file mode 100644 index 000000000..e776e462a --- /dev/null +++ b/system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php @@ -0,0 +1,42 @@ +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); } diff --git a/system/src/Grav/Common/Page/Media.php b/system/src/Grav/Common/Page/Media.php index 2c17d430f..0f4a32a6e 100644 --- a/system/src/Grav/Common/Page/Media.php +++ b/system/src/Grav/Common/Page/Media.php @@ -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']); diff --git a/system/src/Grav/Common/Page/Medium/AbstractMedia.php b/system/src/Grav/Common/Page/Medium/AbstractMedia.php index d7fa8bef1..4abf6cb2b 100644 --- a/system/src/Grav/Common/Page/Medium/AbstractMedia.php +++ b/system/src/Grav/Common/Page/Medium/AbstractMedia.php @@ -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]; } } diff --git a/system/src/Grav/Common/Page/Medium/GlobalMedia.php b/system/src/Grav/Common/Page/Medium/GlobalMedia.php index 37c1841fc..5198eed97 100644 --- a/system/src/Grav/Common/Page/Medium/GlobalMedia.php +++ b/system/src/Grav/Common/Page/Medium/GlobalMedia.php @@ -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; } diff --git a/system/src/Grav/Common/Page/Medium/MediumFactory.php b/system/src/Grav/Common/Page/Medium/MediumFactory.php index 5073a94ae..a9064d784 100644 --- a/system/src/Grav/Common/Page/Medium/MediumFactory.php +++ b/system/src/Grav/Common/Page/Medium/MediumFactory.php @@ -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; } diff --git a/system/src/Grav/Common/User/DataUser/User.php b/system/src/Grav/Common/User/DataUser/User.php index 1c82b442d..f3b4ea149 100644 --- a/system/src/Grav/Common/User/DataUser/User.php +++ b/system/src/Grav/Common/User/DataUser/User.php @@ -177,7 +177,7 @@ class User extends Data implements UserInterface /** * Serialize user. * - * @return array + * @return string[] */ public function __sleep() { diff --git a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php index be1bee165..67dd00a2f 100644 --- a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php +++ b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php @@ -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 + * @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 */ 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); + } + } }