diff --git a/app/config/container.php b/app/config/container.php index e40f7e6..0bb233a 100644 --- a/app/config/container.php +++ b/app/config/container.php @@ -47,6 +47,7 @@ return [ ViewFunctions\FileUrl::class, ViewFunctions\Icon::class, ViewFunctions\Markdown::class, + ViewFunctions\ModifiedTime::class, ViewFunctions\ParentUrl::class, ViewFunctions\SizeForHumans::class, ViewFunctions\Translate::class, diff --git a/app/src/ViewFunctions/ModifiedTime.php b/app/src/ViewFunctions/ModifiedTime.php new file mode 100644 index 0000000..87d0009 --- /dev/null +++ b/app/src/ViewFunctions/ModifiedTime.php @@ -0,0 +1,32 @@ +config = $config; + } + + /** Get the modified time from a file object. */ + public function __invoke(SplFileInfo $file): string + { + try { + $modifiedTime = $file->getMTime(); + } catch (RuntimeException $exception) { + $modifiedTime = lstat($file->getPathname())['mtime']; + } + + return date($this->config->get('date_format'), $modifiedTime); + } +} diff --git a/app/src/ViewFunctions/SizeForHumans.php b/app/src/ViewFunctions/SizeForHumans.php index 6648458..8b92ad2 100644 --- a/app/src/ViewFunctions/SizeForHumans.php +++ b/app/src/ViewFunctions/SizeForHumans.php @@ -2,6 +2,7 @@ namespace App\ViewFunctions; +use RuntimeException; use Symfony\Component\Finder\SplFileInfo; class SizeForHumans extends ViewFunction @@ -12,9 +13,15 @@ class SizeForHumans extends ViewFunction /** Get the human readable file size from a file object. */ public function __invoke(SplFileInfo $file): string { - $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - $factor = (int) floor((strlen((string) $file->getSize()) - 1) / 3); + try { + $fileSize = $file->getSize(); + } catch (RuntimeException $exception) { + return 0; + } - return sprintf('%.2f', $file->getSize() / pow(1024, $factor)) . $sizes[$factor]; + $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + $factor = (int) floor((strlen((string) $fileSize) - 1) / 3); + + return sprintf('%.2f', $fileSize / pow(1024, $factor)) . $sizes[$factor]; } } diff --git a/app/views/components/file.twig b/app/views/components/file.twig index cec665d..84f6cce 100644 --- a/app/views/components/file.twig +++ b/app/views/components/file.twig @@ -36,7 +36,7 @@