Skip to content

Commit fdf7c8d

Browse files
authored
Merge pull request #2 from Florisbosch/fix-getmtime
Check for existing files before retrieving modified time
2 parents ebbfd0e + a5fd589 commit fdf7c8d

File tree

14 files changed

+72
-35
lines changed

14 files changed

+72
-35
lines changed

config/log-viewer.php

100644100755
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@
6666
|--------------------------------------------------------------------------
6767
|
6868
*/
69+
'disable_absolute_filepaths' => env('LOG_VIEWER_DISABLE_ABSOLUTE_FILEPATHS', false),
70+
6971
'filesystem' => [
70-
'root' => env('LOG_VIEWER_FILESYSTEM_ROOT', ''),
71-
'disk' => env('LOG_VIEWER_FILESYSTEM_DISK', 'log-viewer-local'),
72+
'root' => env('LOG_VIEWER_FILESYSTEM_ROOT', ''),
73+
'disk' => env('LOG_VIEWER_FILESYSTEM_DISK', 'log-viewer-local'),
7274
],
7375

7476
/*

src/Concerns/LogFile/CanCacheData.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
namespace Opcodes\LogViewer\Concerns\LogFile;
44

55
use Carbon\CarbonInterface;
6-
use Illuminate\Contracts\Cache\Repository;
76
use Opcodes\LogViewer\Facades\Cache;
87
use Opcodes\LogViewer\Utils\GenerateCacheKey;
98

109
trait CanCacheData
1110
{
1211
protected function indexCacheKeyForQuery(string $query = ''): string
1312
{
14-
return GenerateCacheKey::for($this, md5($query) . ':index');
13+
return GenerateCacheKey::for($this, md5($query).':index');
1514
}
1615

1716
public function clearCache(): void

src/Facades/LogViewer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @method static void clearFileCache()
2121
* @method static string|null getRouteDomain()
2222
* @method static array getRouteMiddleware()
23-
* @method static Filesystem getFilesystem()
23+
* @method static Filesystem getFilesystem($absolutePath = '')
2424
* @method static string getRoutePrefix()
2525
* @method static void auth($callback = null)
2626
* @method static void setMaxLogSize(int $bytes)

src/Http/Livewire/FileList.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public function deleteFolder(string $folderIdentifier)
9090
{
9191
$folder = LogViewer::getFolder($folderIdentifier);
9292

93-
9493
if ($folder) {
9594
Gate::authorize('deleteLogFolder', $folder);
9695

src/LogFile.php

100644100755
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Opcodes\LogViewer\Exceptions\InvalidRegularExpression;
99
use Opcodes\LogViewer\Facades\LogViewer;
1010
use Opcodes\LogViewer\Utils\Utils;
11-
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1211
use Symfony\Component\HttpFoundation\StreamedResponse;
1312

1413
class LogFile
@@ -17,21 +16,33 @@ class LogFile
1716
use Concerns\LogFile\CanCacheData;
1817

1918
public string $path;
19+
2020
public string $name;
21+
2122
public string $identifier;
23+
24+
public string $absolutePath = '';
25+
2226
public string $subFolder = '';
27+
2328
private array $_logIndexCache;
2429

2530
public function __construct(string $path)
2631
{
32+
$pathInfo = pathinfo($path);
2733
$this->path = $path;
28-
$this->name = basename($path);
34+
$this->name = $pathInfo['basename'];
2935
$this->identifier = Str::substr(md5($path), -8, 8).'-'.$this->name;
3036

3137
// Let's remove the file name because we already know it.
3238
$this->subFolder = str_replace($this->name, '', $path);
3339
$this->subFolder = rtrim($this->subFolder, DIRECTORY_SEPARATOR);
3440

41+
if (str_starts_with($path, DIRECTORY_SEPARATOR)) {
42+
$this->absolutePath = pathinfo($path)['dirname'];
43+
$this->path = pathinfo($path)['basename'];
44+
}
45+
3546
$this->loadMetadata();
3647
}
3748

@@ -51,7 +62,9 @@ public function logs(): LogReader
5162

5263
public function size(): int
5364
{
54-
return LogViewer::getFilesystem()->size($this->path);
65+
return LogViewer::getFilesystem($this->absolutePath)->exists($this->path)
66+
? LogViewer::getFilesystem($this->absolutePath)->size($this->path)
67+
: 0;
5568
}
5669

5770
public function sizeInMB(): float
@@ -76,7 +89,7 @@ public function downloadUrl(): string
7689

7790
public function download(): StreamedResponse
7891
{
79-
return LogViewer::getFilesystem()->download($this->path);
92+
return LogViewer::getFilesystem($this->absolutePath)->download($this->path);
8093
}
8194

8295
public function addRelatedIndex(LogIndex $logIndex): void
@@ -104,13 +117,13 @@ public function getLastScannedFilePositionForQuery(?string $query = ''): ?int
104117
public function earliestTimestamp(): int
105118
{
106119
return $this->getMetadata('earliest_timestamp')
107-
?? LogViewer::getFilesystem()->lastModified($this->path);
120+
?? LogViewer::getFilesystem($this->absolutePath)->exists($this->path) ? LogViewer::getFilesystem($this->absolutePath)->lastModified($this->path) : 0;
108121
}
109122

110123
public function latestTimestamp(): int
111124
{
112125
return $this->getMetadata('latest_timestamp')
113-
?? LogViewer::getFilesystem()->lastModified($this->path);
126+
?? LogViewer::getFilesystem($this->absolutePath)->exists($this->path) ? LogViewer::getFilesystem($this->absolutePath)->lastModified($this->path) : 0;
114127
}
115128

116129
public function scan(int $maxBytesToScan = null, bool $force = false): void
@@ -134,7 +147,7 @@ public function search(string $query = null): LogReader
134147
public function delete(): void
135148
{
136149
$this->clearCache();
137-
LogViewer::getFilesystem()->delete($this->path);
150+
LogViewer::getFilesystem($this->absolutePath)->delete($this->path);
138151
LogFileDeleted::dispatch($this);
139152
}
140153
}

src/LogIndex.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ class LogIndex
1717
const DEFAULT_CHUNK_SIZE = 20_000;
1818

1919
public string $identifier;
20+
2021
protected int $nextLogIndexToCreate;
22+
2123
protected int $lastScannedFilePosition;
24+
2225
protected int $lastScannedIndex;
2326

2427
public function __construct(

src/LogReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function open(): self
163163
return $this;
164164
}
165165

166-
$this->fileHandle = LogViewer::getFilesystem()->readStream($this->file->path);
166+
$this->fileHandle = LogViewer::getFilesystem($this->file->absolutePath)->readStream($this->file->path);
167167

168168
if ($this->fileHandle === false) {
169169
throw new \Exception('Could not open "'.$this->file->path.'" for reading.');

src/LogViewerService.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,47 @@ protected function getFilePaths(): array
3838
);
3939

4040
foreach (config('log-viewer.include_files', []) as $pattern) {
41+
$absolute = true;
4142
if (! str_starts_with($pattern, DIRECTORY_SEPARATOR)) {
4243
$pattern = $baseDir.$pattern;
44+
$absolute = false;
4345
}
4446

45-
$files = array_merge($files, $this->getFilePathsMatchingPattern($pattern));
47+
$files = array_merge($files, $this->getFilePathsMatchingPattern($pattern, $absolute));
4648
}
4749

4850
foreach (config('log-viewer.exclude_files', []) as $pattern) {
51+
$absolute = true;
4952
if (! str_starts_with($pattern, DIRECTORY_SEPARATOR)) {
5053
$pattern = $baseDir.$pattern;
54+
$absolute = false;
5155
}
5256

53-
$files = array_diff($files, $this->getFilePathsMatchingPattern($pattern));
57+
$files = array_diff($files, $this->getFilePathsMatchingPattern($pattern, $absolute));
5458
}
5559

5660
return array_values(array_reverse($files));
5761
}
5862

59-
protected function getFilePathsMatchingPattern($pattern)
63+
protected function getFilePathsMatchingPattern($pattern, $absolute = false): array
6064
{
6165
$files = [];
6266

63-
foreach($this->getFilesystem()->allFiles($this->basePathForLogs()) as $file)
64-
{
67+
if (! $absolute) {
68+
$scannedFiles = $this->getFilesystem()->allFiles($this->basePathForLogs());
69+
} else {
70+
$pathInfo = pathinfo($pattern);
71+
$dirname = $pathInfo['dirname'];
72+
$pattern = $pathInfo['basename'];
73+
74+
$scannedFiles = $this->getFilesystem($dirname)->allFiles();
75+
}
76+
77+
foreach ($scannedFiles as $file) {
6578
if (preg_match(pattern: Glob::toRegex(glob: $pattern), subject: $file)) {
66-
$files[] = $file;
79+
$files[] = isset($dirname)
80+
? $dirname.DIRECTORY_SEPARATOR.$file
81+
: $file;
6782
}
6883
}
6984

@@ -73,6 +88,7 @@ protected function getFilePathsMatchingPattern($pattern)
7388
public function basePathForLogs(): string
7489
{
7590
$rootFolder = Str::of(config('log-viewer.filesystem.root'));
91+
7692
return empty($rootFolder)
7793
? $rootFolder->finish('/')
7894
: $rootFolder;
@@ -153,8 +169,17 @@ public function getRouteMiddleware(): array
153169
return config('log-viewer.middleware', []) ?: ['web'];
154170
}
155171

156-
public function getFilesystem(): Filesystem
172+
public function getFilesystem($absolutePath = ''): Filesystem
157173
{
174+
if (! config('disable_absolute_filepaths') && ($absolutePath !== '') && is_dir($absolutePath)) {
175+
config()->set('filesystems.disks.log-viewer-absolute', [
176+
'driver' => 'local',
177+
'root' => $absolutePath,
178+
]);
179+
180+
return Storage::disk('log-viewer-absolute');
181+
}
182+
158183
return Storage::disk(config('log-viewer.filesystem.disk'));
159184
}
160185

src/Utils/GenerateCacheKey.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ public static function for(mixed $object, ?string $namespace = null): string
1313
$key = '';
1414

1515
if ($object instanceof LogFile) {
16-
$key = self::baseKey() . ':file:' . md5($object->path);
16+
$key = self::baseKey().':file:'.md5($object->path);
1717
}
1818

1919
if ($object instanceof LogIndex) {
20-
$key = self::for($object->file) . ':' . $object->identifier;
20+
$key = self::for($object->file).':'.$object->identifier;
2121
}
2222

2323
if (is_string($object)) {
24-
$key = self::baseKey() . ':' . $object;
24+
$key = self::baseKey().':'.$object;
2525
}
2626

27-
if (!empty($namespace)) {
28-
$key .= ':' . $namespace;
27+
if (! empty($namespace)) {
28+
$key .= ':'.$namespace;
2929
}
3030

3131
return $key;
3232
}
3333

3434
protected static function baseKey(): string
3535
{
36-
return 'log-viewer:' . LogViewer::version();
36+
return 'log-viewer:'.LogViewer::version();
3737
}
3838
}

tests/Unit/GenerateCacheKeyTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$result = GenerateCacheKey::for($file);
1111

1212
expect($result)->toBe(
13-
'log-viewer:' . LogViewer::version() . ':file:' . md5($file->path)
13+
'log-viewer:'.LogViewer::version().':file:'.md5($file->path)
1414
);
1515
});
1616

@@ -20,7 +20,7 @@
2020
$result = GenerateCacheKey::for($file, $namespace = 'randomNamespace');
2121

2222
expect($result)->toBe(
23-
GenerateCacheKey::for($file) . ':' . $namespace
23+
GenerateCacheKey::for($file).':'.$namespace
2424
);
2525
});
2626

@@ -30,7 +30,7 @@
3030
$result = GenerateCacheKey::for($logIndex);
3131

3232
expect($result)->toBe(
33-
GenerateCacheKey::for($logIndex->file) . ':' . $logIndex->identifier
33+
GenerateCacheKey::for($logIndex->file).':'.$logIndex->identifier
3434
);
3535
});
3636

@@ -39,5 +39,5 @@
3939

4040
$result = GenerateCacheKey::for($string);
4141

42-
expect($result)->toBe('log-viewer:' . LogViewer::version() . ':' . $string);
42+
expect($result)->toBe('log-viewer:'.LogViewer::version().':'.$string);
4343
});

0 commit comments

Comments
 (0)