Skip to content

Commit 17cc3ca

Browse files
committed
add support for absolute paths
1 parent e197af4 commit 17cc3ca

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

config/log-viewer.php

100644100755
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@
6666
|--------------------------------------------------------------------------
6767
|
6868
*/
69+
'disable_absolute_filepaths' => env('LOG_VIEWER_DISABLE_ABSOLUTE_FILEPATHS', false),
70+
6971
'filesystem' => [
70-
'root' => env('LOG_VIEWER_FILESYSTEM_ROOT', ''),
72+
'root' => env('LOG_VIEWER_FILESYSTEM_ROOT',''),
7173
'disk' => env('LOG_VIEWER_FILESYSTEM_DISK', 'log-viewer-local'),
7274
],
7375

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/LogFile.php

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

1314
class LogFile
@@ -16,25 +17,28 @@ class LogFile
1617
use Concerns\LogFile\CanCacheData;
1718

1819
public string $path;
19-
2020
public string $name;
21-
2221
public string $identifier;
23-
22+
public string $absolutePath = '';
2423
public string $subFolder = '';
25-
2624
private array $_logIndexCache;
2725

2826
public function __construct(string $path)
2927
{
28+
$pathInfo = pathinfo($path);
3029
$this->path = $path;
31-
$this->name = basename($path);
30+
$this->name = $pathInfo['basename'];
3231
$this->identifier = Str::substr(md5($path), -8, 8).'-'.$this->name;
3332

3433
// Let's remove the file name because we already know it.
3534
$this->subFolder = str_replace($this->name, '', $path);
3635
$this->subFolder = rtrim($this->subFolder, DIRECTORY_SEPARATOR);
3736

37+
if (str_starts_with($path, DIRECTORY_SEPARATOR)) {
38+
$this->absolutePath = pathinfo($path)['dirname'];
39+
$this->path = pathinfo($path)['basename'];
40+
}
41+
3842
$this->loadMetadata();
3943
}
4044

@@ -54,9 +58,7 @@ public function logs(): LogReader
5458

5559
public function size(): int
5660
{
57-
return LogViewer::getFilesystem()->exists($this->path)
58-
? LogViewer::getFilesystem()->size($this->path)
59-
: 0;
61+
return LogViewer::getFilesystem($this->absolutePath)->size($this->path);
6062
}
6163

6264
public function sizeInMB(): float
@@ -81,7 +83,7 @@ public function downloadUrl(): string
8183

8284
public function download(): StreamedResponse
8385
{
84-
return LogViewer::getFilesystem()->download($this->path);
86+
return LogViewer::getFilesystem($this->absolutePath)->download($this->path);
8587
}
8688

8789
public function addRelatedIndex(LogIndex $logIndex): void
@@ -109,15 +111,13 @@ public function getLastScannedFilePositionForQuery(?string $query = ''): ?int
109111
public function earliestTimestamp(): int
110112
{
111113
return $this->getMetadata('earliest_timestamp')
112-
?? 0;
113-
// ?? LogViewer::getFilesystem()->exists($this->path) ? LogViewer::getFilesystem()->lastModified($this->path) : 0;
114+
?? LogViewer::getFilesystem($this->absolutePath)->lastModified($this->path);
114115
}
115116

116117
public function latestTimestamp(): int
117118
{
118119
return $this->getMetadata('latest_timestamp')
119-
?? 0;
120-
// ?? LogViewer::getFilesystem()->exists($this->path) ? LogViewer::getFilesystem()->lastModified($this->path) : 0;
120+
?? LogViewer::getFilesystem($this->absolutePath)->lastModified($this->path);
121121
}
122122

123123
public function scan(int $maxBytesToScan = null, bool $force = false): void
@@ -141,7 +141,7 @@ public function search(string $query = null): LogReader
141141
public function delete(): void
142142
{
143143
$this->clearCache();
144-
LogViewer::getFilesystem()->delete($this->path);
144+
LogViewer::getFilesystem($this->absolutePath)->delete($this->path);
145145
LogFileDeleted::dispatch($this);
146146
}
147147
}

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,31 +38,48 @@ 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) {
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)
78+
{
6479
if (preg_match(pattern: Glob::toRegex(glob: $pattern), subject: $file)) {
65-
$files[] = $file;
80+
$files[] = isset($dirname)
81+
? $dirname . DIRECTORY_SEPARATOR . $file
82+
: $file;
6683
}
6784
}
6885

@@ -72,7 +89,6 @@ protected function getFilePathsMatchingPattern($pattern)
7289
public function basePathForLogs(): string
7390
{
7491
$rootFolder = Str::of(config('log-viewer.filesystem.root'));
75-
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

0 commit comments

Comments
 (0)