Broken symlinks will no longer cause errors

This commit is contained in:
Chris Kankiewicz
2021-02-23 21:36:28 -07:00
parent 7b166af2fc
commit 8076862029
6 changed files with 77 additions and 4 deletions

View File

@@ -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,

View File

@@ -0,0 +1,32 @@
<?php
namespace App\ViewFunctions;
use App\Config;
use RuntimeException;
use Symfony\Component\Finder\SplFileInfo;
class ModifiedTime extends ViewFunction
{
/** @var string The function name */
protected $name = 'modified_time';
private Config $config;
public function __construct(Config $config)
{
$this->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);
}
}

View File

@@ -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];
}
}

View File

@@ -36,7 +36,7 @@
</div>
<div class="hidden whitespace-nowrap text-right truncate ml-2 w-1/4 sm:block">
{{ parentDir ? '—' : file.getMTime | date }}
{{ parentDir ? '—' : modified_time(file) }}
</div>
</div>
</a>

View File

@@ -0,0 +1,32 @@
<?php
namespace Tests\ViewFunctions;
use App\ViewFunctions\ModifiedTime;
use RuntimeException;
use Symfony\Component\Finder\SplFileInfo;
use Tests\TestCase;
class ModifiedTimeTest extends TestCase
{
public function test_it_can_return_the_modified_time_for_a_file(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getMTime')->willReturn(516976496);
$modifiedTime = new ModifiedTime($this->config);
$this->assertEquals('1986-05-20 12:34:56', $modifiedTime($file));
}
public function test_it_can_return_the_modified_time_for_a_symlink(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getMTime')->willThrowException(new RuntimeException);
$file->method('getPathname')->willReturn(dirname(__DIR__) . '/_files/somedir/broken.symlink');
$modifiedTime = new ModifiedTime($this->config);
$this->assertEquals('1986-05-20 12:34:56', $modifiedTime($file));
}
}

View File

@@ -0,0 +1 @@
../broken.symlink