Skip to content

Commit

Permalink
Update Document icon provider:
Browse files Browse the repository at this point in the history
- use Mime type image provider
  • Loading branch information
webeweb committed May 31, 2024
1 parent b2b4abb commit 5810e8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 792 deletions.
43 changes: 20 additions & 23 deletions src/Provider/DocumentIconProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

namespace WBW\Bundle\EDMBundle\Provider;

use WBW\Bundle\CommonBundle\Provider\Image\MimeTypeImageProvider;
use WBW\Bundle\CommonBundle\Provider\Image\MimeTypeImageProviderTrait;
use WBW\Bundle\EDMBundle\Model\DocumentInterface;
use WBW\Library\Common\Traits\Strings\StringDirectoryTrait;

/**
* Document icon provider.
Expand All @@ -24,14 +25,7 @@
*/
class DocumentIconProvider {

use StringDirectoryTrait;

/**
* Default icon.
*
* @var string
*/
public const DEFAULT_ICON = "unknown.svg";
use MimeTypeImageProviderTrait;

/**
* Service name.
Expand All @@ -44,7 +38,7 @@ class DocumentIconProvider {
* Constructor.
*/
public function __construct() {
$this->setDirectory(realpath(__DIR__ . "/../Resources/public/img"));
$this->setMimeTypeImageProvider(new MimeTypeImageProvider());
}

/**
Expand All @@ -55,19 +49,9 @@ public function __construct() {
*/
public function getIcon(DocumentInterface $document): string {

if ($document->isDirectory()) {
return "folder.svg";
}

$mimeType = str_replace("/", "-", $document->getMimeType());
$filename = "$mimeType.svg";
$name = $this->getMimeType($document);

$pathname = implode(DIRECTORY_SEPARATOR, [$this->getDirectory(), $filename]);
if (false === file_exists($pathname)) {
$filename = self::DEFAULT_ICON;
}

return $filename;
return $this->getMimeTypeImageProvider()->getImage($name);
}

/**
Expand All @@ -77,6 +61,19 @@ public function getIcon(DocumentInterface $document): string {
* @return string Returns the icon asset.
*/
public function getIconAsset(DocumentInterface $document): string {
return "/bundles/wbwedm/img/{$this->getIcon($document)}";

$name = $this->getMimeType($document);

return $this->getMimeTypeImageProvider()->getImageUrl($name);
}

/**
* Get the mime type.
*
* @param DocumentInterface $document The document.
* @return string Returns the mime type.
*/
protected function getMimeType(DocumentInterface $document): string {
return true === $document->isDirectory() ? "folder" : $document->getMimeType();
}
}
4 changes: 2 additions & 2 deletions tests/DataTables/Provider/DocumentDataTablesProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function testRenderColumn(): void {

$col = $obj->getColumns();

$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwedm/img/application-octet-stream.svg" height="32px"/></span>document.php', $obj->renderColumn($col[0], $document));
$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwcommon/img/mimetype/default/application-octet-stream.svg" height="32px"/></span>document.php', $obj->renderColumn($col[0], $document));
$this->assertRegExp('/^<span class="pull-right">1[\.,]00 Kio<\/span>$/', $obj->renderColumn($col[1], $document));
$this->assertRegExp("/^[0-9\- :]{16}$/", $obj->renderColumn($col[2], $document));
$this->assertEquals("application/octet-stream", $obj->renderColumn($col[3], $document));
Expand Down Expand Up @@ -281,7 +281,7 @@ public function testRenderColumnWithDirectory(): void {

$col = $obj->getColumns();

$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwedm/img/folder.svg" height="32px"/></span>document<br/><span class="font-italic">label.items_count</span>', $obj->renderColumn($col[0], $document));
$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwcommon/img/mimetype/default/folder.svg" height="32px"/></span>document<br/><span class="font-italic">label.items_count</span>', $obj->renderColumn($col[0], $document));
$this->assertRegExp('/^<span class="pull-right">1[\.,]00 Kio<\/span>$/', $obj->renderColumn($col[1], $document));
$this->assertRegExp("/^[0-9\- :]{16}$/", $obj->renderColumn($col[2], $document));
$this->assertEquals("label.directory", $obj->renderColumn($col[3], $document));
Expand Down
Loading

0 comments on commit 5810e8e

Please sign in to comment.