Skip to content

Commit

Permalink
FilePreview abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jul 25, 2024
1 parent 54e2ed4 commit 3ebd535
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Filesystem/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function isResizable(): bool

/**
* Checks if a preview can be displayed for the file
* in the panel or in the frontend
* in the Panel or in the frontend
*/
public function isViewable(): bool
{
Expand Down
88 changes: 88 additions & 0 deletions src/Panel/Ui/FilePreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Kirby\Panel\Ui;

use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Panel\Ui\FilePreviews\FileDefaultPreview;
use Kirby\Toolkit\I18n;

/**
* Defines a component that implements a file preview
*
* @package Kirby Panel
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
abstract class FilePreview extends Component
{
public string $component = 'k-file-default-preview';

public function __construct(
public File $file
) {
}

/**
* Returns true if this class should
* handle the preview of this file
*/
abstract public static function accepts(File $file): bool;

/**
* Returns detail information about the file
*/
public function details(): array
{
return [
[
'title' => I18n::translate('template'),
'text' => $this->file->template() ?? ''
],
[
'title' => I18n::translate('mime'),
'text' => $this->file->mime()
],
[
'title' => I18n::translate('url'),
'text' => $this->file->id(),
'link' => $this->file->previewUrl()
],
[
'title' => I18n::translate('size'),
'text' => $this->file->niceSize()
],
];
}

/**
* Returns a file preview instance by going through all
* available handler classes and finding the first that
* accepts the file
*/
final public static function factory(File $file): static
{
// get file preview classes providers from plugins
$handlers = App::instance()->extensions('filePreviews');

foreach ($handlers as $handler) {
if ($handler::accepts($file) === true) {
return new $handler($file);
}
}

return new FileDefaultPreview($file);
}

public function props(): array
{
return [
'details' => $this->details(),
'url' => $this->file->previewUrl()
];
}
}
107 changes: 107 additions & 0 deletions tests/Panel/Ui/FilePreviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Kirby\Panel\Ui;

use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Panel\Ui\FilePreviews\FileDefaultPreview;
use Kirby\TestCase;

class FileDummyPreview extends FilePreview
{
public static function accepts(File $file): bool
{
return $file->type() === 'document';
}
}

/**
* @coversDefaultClass \Kirby\Panel\Ui\FilePreview
* @covers ::__construct
*/
class FilePreviewTest extends TestCase
{
/**
* @covers ::factory
*/
public function testFactory()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);

$component = FilePreview::factory($file);
$this->assertInstanceOf(FileDefaultPreview::class, $component);
}

/**
* @covers ::factory
*/
public function testFactoryWithCustomHandler()
{
new App([
'roots' => [
'index' => '/dev/null'
],
'filePreviews' => [
FileDummyPreview::class
]
]);

$page = new Page(['slug' => 'test']);

$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$component = FilePreview::factory($file);
$this->assertInstanceOf(FileDefaultPreview::class, $component);

$file = new File(['filename' => 'test.xls', 'parent' => $page]);
$component = FilePreview::factory($file);
$this->assertInstanceOf(FileDummyPreview::class, $component);
}

/**
* @covers ::details
* @covers ::props
*/
public function testProps()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$component = new FileDummyPreview($file);

$this->assertSame([
'details' => [
[
'title' => 'Template',
'text' => '',
],
[
'title' => 'Media Type',
'text' => 'image/jpeg',
],
[
'title' => 'Url',
'text' => 'test/test.jpg',
'link' => '/test/test.jpg',
],
[
'title' => 'Size',
'text' => '0 KB',
]
],
'url' => '/test/test.jpg'
], $component->props());
}

/**
* @covers ::render
*/
public function testRender()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$component = new FileDummyPreview($file);

$this->assertSame('k-file-default-preview', $component->render()['component']);
}
}

0 comments on commit 3ebd535

Please sign in to comment.