Added twig extension to format file size, links to download and delete files attached to the book.

This commit is contained in:
krzysiej
2022-05-24 11:22:35 +02:00
parent 319f6311fc
commit 1f852ac031
5 changed files with 94 additions and 18 deletions

View File

@@ -4,6 +4,7 @@
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
@@ -15,8 +16,22 @@ class AppExtension extends AbstractExtension
];
}
public function getFilters()
{
return [
new TwigFilter('bytes_format', [$this, 'bytes_format']),
];
}
public function file_exists(string $file): bool
{
return file_exists(ltrim($file, '/'));
}
public function bytes_format(int $bytes): string
{
$base = log($bytes) / log(1024);
$suffix = array("B", "KB", "MB", "GB", "TB")[floor($base)];
return round(pow(1024, $base - floor($base)), 2) . $suffix;
}
}