From bbf2f34ea19eee01fa556cc946df30abcde7f343 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 19 Apr 2022 10:51:39 +0300 Subject: [PATCH] Interface updates --- .../Interfaces/MediaCollectionInterface.php | 31 ++++++++++++++--- .../Media/Interfaces/MediaUploadInterface.php | 12 +++---- .../Common/Media/Traits/MediaUploadTrait.php | 27 +++++---------- .../Grav/Common/Page/Medium/AbstractMedia.php | 17 ++++++---- .../Grav/Common/Page/Medium/LocalMedia.php | 34 +++++++++---------- 5 files changed, 68 insertions(+), 53 deletions(-) diff --git a/system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php b/system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php index f9ab6d286..e32e1bd46 100644 --- a/system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php +++ b/system/src/Grav/Common/Media/Interfaces/MediaCollectionInterface.php @@ -18,6 +18,27 @@ use RuntimeException; */ interface MediaCollectionInterface extends \Grav\Framework\Media\Interfaces\MediaCollectionInterface { + /** + * Get media id. + * + * @return string + */ + public function getId(): string; + + /** + * Get media type used in MediaFactory. + * + * @return string + */ + public function getType(): string; + + /** + * Get media name used in MediaFactory. + * + * @return string + */ + public function getName(): string; + /** * Return media path. * @@ -27,16 +48,18 @@ interface MediaCollectionInterface extends \Grav\Framework\Media\Interfaces\Medi public function getPath(string $filename = null): ?string; /** - * @param string|null $path - * @return void + * Return media file url. + * + * @param string $filename + * @return string */ - public function setPath(?string $path): void; + public function getUrl(string $filename): string; /** * Get medium by filename. * * @param string $filename - * @return Medium|null + * @return MediaObjectInterface|null */ public function get(string $filename): ?MediaObjectInterface; diff --git a/system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php b/system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php index 73625999a..3afe4628e 100644 --- a/system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php +++ b/system/src/Grav/Common/Media/Interfaces/MediaUploadInterface.php @@ -41,33 +41,29 @@ interface MediaUploadInterface * * @example * $filename = null; // Override filename if needed (ignored if randomizing filenames). - * $settings = ['destination' => 'user://pages/media']; // Settings from the form field. - * $filename = $media->checkUploadedFile($uploadedFile, $filename, $settings); + * $filename = $media->checkUploadedFile($uploadedFile, $filename); * $media->copyUploadedFile($uploadedFile, $filename); * * @param UploadedFileInterface $uploadedFile * @param string $filename - * @param array|null $settings * @return void * @throws RuntimeException */ - public function copyUploadedFile(UploadedFileInterface $uploadedFile, string $filename, array $settings = null): void; + public function copyUploadedFile(UploadedFileInterface $uploadedFile, string $filename): void; /** * Delete real file from the media collection. * * @param string $filename - * @param array|null $settings * @return void */ - public function deleteFile(string $filename, array $settings = null): void; + public function deleteFile(string $filename): void; /** * Rename file inside the media collection. * * @param string $from * @param string $to - * @param array|null $settings */ - public function renameFile(string $from, string $to, array $settings = null): void; + public function renameFile(string $from, string $to): void; } diff --git a/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php b/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php index e65ef1b47..7b3d8680a 100644 --- a/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php +++ b/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php @@ -221,25 +221,22 @@ trait MediaUploadTrait * WARNING: Always check uploaded file before copying it! * * @example - * $settings = ['destination' => 'user://pages/media']; // Settings from the form field. - * $filename = $media->checkUploadedFile($uploadedFile, $filename, $settings); - * $media->copyUploadedFile($uploadedFile, $filename, $settings); + * $filename = $media->checkUploadedFile($uploadedFile, $filename); + * $media->copyUploadedFile($uploadedFile, $filename); * * @param UploadedFileInterface $uploadedFile * @param string $filename - * @param array|null $settings * @return void * @throws RuntimeException */ - public function copyUploadedFile(UploadedFileInterface $uploadedFile, string $filename, array $settings = null): void + public function copyUploadedFile(UploadedFileInterface $uploadedFile, string $filename): void { try { // Check if the filename is allowed. $this->checkFilename($filename); // Calculate path without the retina scaling factor. - [$base, $ext,,] = $this->getFileParts($filename); - $name = "{$base}.{$ext}"; + $name = $this->getBasename($filename); $this->clearCache(); @@ -295,19 +292,17 @@ trait MediaUploadTrait * Delete real file from the media collection. * * @param string $filename - * @param array|null $settings * @return void * @throws RuntimeException */ - public function deleteFile(string $filename, array $settings = null): void + public function deleteFile(string $filename): void { try { // Check if the filename is allowed. $this->checkFilename($filename); // Get base name of the file. - [$base, $ext,,] = $this->getFileParts($filename); - $name = "{$base}.{$ext}"; + $name = $this->getBasename($filename); // Remove file and all the associated metadata. $this->clearCache(); @@ -330,9 +325,8 @@ trait MediaUploadTrait * * @param string $from * @param string $to - * @param array|null $settings */ - public function renameFile(string $from, string $to, array $settings = null): void + public function renameFile(string $from, string $to): void { try { // Check if the filename is allowed. @@ -342,11 +336,8 @@ trait MediaUploadTrait $this->clearCache(); // Remove @2x, @3x and .meta.yaml - [$base, $ext,,] = $this->getFileParts($from); - $from = "{$base}.{$ext}"; - - [$base, $ext,,] = $this->getFileParts($to); - $to = "{$base}.{$ext}"; + $from = $this->getBasename($from); + $to = $this->getBasename($to); $this->doRename($from, $to); diff --git a/system/src/Grav/Common/Page/Medium/AbstractMedia.php b/system/src/Grav/Common/Page/Medium/AbstractMedia.php index d4df80ef7..61bf6ca8c 100644 --- a/system/src/Grav/Common/Page/Medium/AbstractMedia.php +++ b/system/src/Grav/Common/Page/Medium/AbstractMedia.php @@ -110,12 +110,6 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac */ abstract public function getUrl(string $filename): string; - /** - * @param string|null $path - * @return void - */ - abstract public function setPath(?string $path): void; - /** * @return bool */ @@ -968,6 +962,17 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac $mediaIndex->save($id, $index); } + /** + * @param string $filename + * @return string + */ + protected function getBasename(string $filename): string + { + [$base, $ext,,] = $this->getFileParts($filename); + + return "{$base}.{$ext}"; + } + /** * Get filename, extension and meta part. * diff --git a/system/src/Grav/Common/Page/Medium/LocalMedia.php b/system/src/Grav/Common/Page/Medium/LocalMedia.php index d453288f8..17c0f57c3 100644 --- a/system/src/Grav/Common/Page/Medium/LocalMedia.php +++ b/system/src/Grav/Common/Page/Medium/LocalMedia.php @@ -66,23 +66,6 @@ abstract class LocalMedia extends AbstractMedia return $this->getPath($filename); } - /** - * @param string|null $path - * @return void - */ - public function setPath(?string $path): void - { - // Make path relative from GRAV_WEBROOT. - $locator = $this->getLocator(); - if ($locator->isStream($path)) { - $path = $locator->findResource($path, false) ?: null; - } else { - $path = Folder::getRelativePath($path, GRAV_WEBROOT) ?: null; - } - - $this->path = $path; - } - /** * Create Medium from a file. * @@ -395,4 +378,21 @@ abstract class LocalMedia extends AbstractMedia Security::sanitizeSVG($filepath); } + + /** + * @param string|null $path + * @return void + */ + protected function setPath(?string $path): void + { + // Make path relative from GRAV_WEBROOT. + $locator = $this->getLocator(); + if ($locator->isStream($path)) { + $path = $locator->findResource($path, false) ?: null; + } else { + $path = Folder::getRelativePath($path, GRAV_WEBROOT) ?: null; + } + + $this->path = $path; + } }