mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-27 15:50:40 +02:00
Broken symlinks will no longer cause errors
This commit is contained in:
@@ -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,
|
||||
|
32
app/src/ViewFunctions/ModifiedTime.php
Normal file
32
app/src/ViewFunctions/ModifiedTime.php
Normal 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);
|
||||
}
|
||||
}
|
@@ -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];
|
||||
}
|
||||
}
|
||||
|
@@ -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>
|
||||
|
32
tests/ViewFunctions/ModifiedTimeTest.php
Normal file
32
tests/ViewFunctions/ModifiedTimeTest.php
Normal 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));
|
||||
}
|
||||
}
|
1
tests/_files/somedir/broken.symlink
Symbolic link
1
tests/_files/somedir/broken.symlink
Symbolic link
@@ -0,0 +1 @@
|
||||
../broken.symlink
|
Reference in New Issue
Block a user