Interface updates

This commit is contained in:
Matias Griese
2022-04-19 10:51:39 +03:00
parent 44eed37bc3
commit bbf2f34ea1
5 changed files with 68 additions and 53 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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.
*

View File

@@ -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;
}
}