Added twig extension to format file size, links to download and delete files attached to the book.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user