Skip to content

Commit 65be109

Browse files
committed
Add support for absolute path + try to fix windows errors
1 parent ebbfd0e commit 65be109

16 files changed

+81
-42
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: 22 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()->exists($this->path)
66+
? LogViewer::getFilesystem()->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,9 @@ public function search(string $query = null): LogReader
134147
public function delete(): void
135148
{
136149
$this->clearCache();
137-
LogViewer::getFilesystem()->delete($this->path);
150+
if (LogViewer::getFilesystem($this->absolutePath)->exists($this->path)) {
151+
LogViewer::getFilesystem($this->absolutePath)->delete($this->path);
152+
}
138153
LogFileDeleted::dispatch($this);
139154
}
140155
}

src/LogFolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function download(): BinaryFileResponse
123123
/** @var LogFile $file */
124124
foreach ($this->files() as $file) {
125125
if (Gate::check('downloadLogFile', $file)) {
126-
$zip->addFromString(name: $file->name, content: LogViewer::getFilesystem()->get($file->path));
126+
$zip->addFromString(name: $file->name, content: LogViewer::getFilesystem($file->absolutePath)->get($file->path));
127127
}
128128
}
129129

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: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,56 @@ protected function getFilePaths(): array
2929
$baseDir = str_replace(
3030
['[', ']'],
3131
['{LEFTBRACKET}', '{RIGHTBRACKET}'],
32-
str_replace('\\', '/', $this->basePathForLogs())
32+
str_replace('\\', '/', $this->basePathForLogs()),
3333
);
3434
$baseDir = str_replace(
35-
['{LEFTBRACKET}', '{RIGHTBRACKET}'],
36-
['[[]', '[]]'],
35+
['{LEFTBRACKET}', '{RIGHTBRACKET}', '\\'],
36+
['[[]', '[]]', '\\\\'],
3737
$baseDir
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()->files($this->basePathForLogs());
69+
} else {
70+
$pathInfo = pathinfo($pattern);
71+
$dirname = $pathInfo['dirname'];
72+
$pattern = $pathInfo['basename'];
73+
74+
$scannedFiles = $this->getFilesystem($dirname)->files();
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,8 +88,9 @@ protected function getFilePathsMatchingPattern($pattern)
7388
public function basePathForLogs(): string
7489
{
7590
$rootFolder = Str::of(config('log-viewer.filesystem.root'));
76-
return empty($rootFolder)
77-
? $rootFolder->finish('/')
91+
92+
return ($rootFolder != '')
93+
? $rootFolder->finish(DIRECTORY_SEPARATOR)
7894
: $rootFolder;
7995
}
8096

@@ -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/LogViewerServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function register()
2323

2424
$this->app['config']['filesystems.disks.log-viewer-local'] = [
2525
'driver' => 'local',
26-
'root' => storage_path('logs'),
26+
'root' => realpath(storage_path('logs')),
2727
];
2828

2929
$this->app->bind('log-viewer', LogViewerService::class);

0 commit comments

Comments
 (0)