Skip to content

Commit

Permalink
Add support for absolute path + try to fix windows errors
Browse files Browse the repository at this point in the history
  • Loading branch information
florisbosch committed Dec 19, 2022
1 parent ebbfd0e commit 65be109
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 42 deletions.
6 changes: 4 additions & 2 deletions config/log-viewer.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@
|--------------------------------------------------------------------------
|
*/
'disable_absolute_filepaths' => env('LOG_VIEWER_DISABLE_ABSOLUTE_FILEPATHS', false),

'filesystem' => [
'root' => env('LOG_VIEWER_FILESYSTEM_ROOT', ''),
'disk' => env('LOG_VIEWER_FILESYSTEM_DISK', 'log-viewer-local'),
'root' => env('LOG_VIEWER_FILESYSTEM_ROOT', ''),
'disk' => env('LOG_VIEWER_FILESYSTEM_DISK', 'log-viewer-local'),
],

/*
Expand Down
3 changes: 1 addition & 2 deletions src/Concerns/LogFile/CanCacheData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Opcodes\LogViewer\Concerns\LogFile;

use Carbon\CarbonInterface;
use Illuminate\Contracts\Cache\Repository;
use Opcodes\LogViewer\Facades\Cache;
use Opcodes\LogViewer\Utils\GenerateCacheKey;

trait CanCacheData
{
protected function indexCacheKeyForQuery(string $query = ''): string
{
return GenerateCacheKey::for($this, md5($query) . ':index');
return GenerateCacheKey::for($this, md5($query).':index');
}

public function clearCache(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/LogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @method static void clearFileCache()
* @method static string|null getRouteDomain()
* @method static array getRouteMiddleware()
* @method static Filesystem getFilesystem()
* @method static Filesystem getFilesystem($absolutePath = '')
* @method static string getRoutePrefix()
* @method static void auth($callback = null)
* @method static void setMaxLogSize(int $bytes)
Expand Down
1 change: 0 additions & 1 deletion src/Http/Livewire/FileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public function deleteFolder(string $folderIdentifier)
{
$folder = LogViewer::getFolder($folderIdentifier);


if ($folder) {
Gate::authorize('deleteLogFolder', $folder);

Expand Down
29 changes: 22 additions & 7 deletions src/LogFile.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Opcodes\LogViewer\Exceptions\InvalidRegularExpression;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\Utils\Utils;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;

class LogFile
Expand All @@ -17,21 +16,33 @@ class LogFile
use Concerns\LogFile\CanCacheData;

public string $path;

public string $name;

public string $identifier;

public string $absolutePath = '';

public string $subFolder = '';

private array $_logIndexCache;

public function __construct(string $path)
{
$pathInfo = pathinfo($path);
$this->path = $path;
$this->name = basename($path);
$this->name = $pathInfo['basename'];
$this->identifier = Str::substr(md5($path), -8, 8).'-'.$this->name;

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

// if (str_starts_with($path, DIRECTORY_SEPARATOR)) {
// $this->absolutePath = pathinfo($path)['dirname'];
// $this->path = pathinfo($path)['basename'];
// }

$this->loadMetadata();
}

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

public function size(): int
{
return LogViewer::getFilesystem()->size($this->path);
return LogViewer::getFilesystem()->exists($this->path)
? LogViewer::getFilesystem()->size($this->path)
: 0;
}

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

public function download(): StreamedResponse
{
return LogViewer::getFilesystem()->download($this->path);
return LogViewer::getFilesystem($this->absolutePath)->download($this->path);
}

public function addRelatedIndex(LogIndex $logIndex): void
Expand Down Expand Up @@ -104,13 +117,13 @@ public function getLastScannedFilePositionForQuery(?string $query = ''): ?int
public function earliestTimestamp(): int
{
return $this->getMetadata('earliest_timestamp')
?? LogViewer::getFilesystem()->lastModified($this->path);
?? LogViewer::getFilesystem($this->absolutePath)->exists($this->path) ? LogViewer::getFilesystem($this->absolutePath)->lastModified($this->path) : 0;
}

public function latestTimestamp(): int
{
return $this->getMetadata('latest_timestamp')
?? LogViewer::getFilesystem()->lastModified($this->path);
?? LogViewer::getFilesystem($this->absolutePath)->exists($this->path) ? LogViewer::getFilesystem($this->absolutePath)->lastModified($this->path) : 0;
}

public function scan(int $maxBytesToScan = null, bool $force = false): void
Expand All @@ -134,7 +147,9 @@ public function search(string $query = null): LogReader
public function delete(): void
{
$this->clearCache();
LogViewer::getFilesystem()->delete($this->path);
if (LogViewer::getFilesystem($this->absolutePath)->exists($this->path)) {
LogViewer::getFilesystem($this->absolutePath)->delete($this->path);
}
LogFileDeleted::dispatch($this);
}
}
2 changes: 1 addition & 1 deletion src/LogFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function download(): BinaryFileResponse
/** @var LogFile $file */
foreach ($this->files() as $file) {
if (Gate::check('downloadLogFile', $file)) {
$zip->addFromString(name: $file->name, content: LogViewer::getFilesystem()->get($file->path));
$zip->addFromString(name: $file->name, content: LogViewer::getFilesystem($file->absolutePath)->get($file->path));
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/LogIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class LogIndex
const DEFAULT_CHUNK_SIZE = 20_000;

public string $identifier;

protected int $nextLogIndexToCreate;

protected int $lastScannedFilePosition;

protected int $lastScannedIndex;

public function __construct(
Expand Down
2 changes: 1 addition & 1 deletion src/LogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function open(): self
return $this;
}

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

if ($this->fileHandle === false) {
throw new \Exception('Could not open "'.$this->file->path.'" for reading.');
Expand Down
49 changes: 37 additions & 12 deletions src/LogViewerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,56 @@ protected function getFilePaths(): array
$baseDir = str_replace(
['[', ']'],
['{LEFTBRACKET}', '{RIGHTBRACKET}'],
str_replace('\\', '/', $this->basePathForLogs())
str_replace('\\', '/', $this->basePathForLogs()),
);
$baseDir = str_replace(
['{LEFTBRACKET}', '{RIGHTBRACKET}'],
['[[]', '[]]'],
['{LEFTBRACKET}', '{RIGHTBRACKET}', '\\'],
['[[]', '[]]', '\\\\'],
$baseDir
);

foreach (config('log-viewer.include_files', []) as $pattern) {
$absolute = true;
if (! str_starts_with($pattern, DIRECTORY_SEPARATOR)) {
$pattern = $baseDir.$pattern;
$absolute = false;
}

$files = array_merge($files, $this->getFilePathsMatchingPattern($pattern));
$files = array_merge($files, $this->getFilePathsMatchingPattern($pattern, $absolute));
}

foreach (config('log-viewer.exclude_files', []) as $pattern) {
$absolute = true;
if (! str_starts_with($pattern, DIRECTORY_SEPARATOR)) {
$pattern = $baseDir.$pattern;
$absolute = false;
}

$files = array_diff($files, $this->getFilePathsMatchingPattern($pattern));
$files = array_diff($files, $this->getFilePathsMatchingPattern($pattern, $absolute));
}

return array_values(array_reverse($files));
}

protected function getFilePathsMatchingPattern($pattern)
protected function getFilePathsMatchingPattern($pattern, $absolute = false): array
{
$files = [];

foreach($this->getFilesystem()->allFiles($this->basePathForLogs()) as $file)
{
if (! $absolute) {
$scannedFiles = $this->getFilesystem()->files($this->basePathForLogs());
} else {
$pathInfo = pathinfo($pattern);
$dirname = $pathInfo['dirname'];
$pattern = $pathInfo['basename'];

$scannedFiles = $this->getFilesystem($dirname)->files();
}

foreach ($scannedFiles as $file) {
if (preg_match(pattern: Glob::toRegex(glob: $pattern), subject: $file)) {
$files[] = $file;
$files[] = isset($dirname)
? $dirname.DIRECTORY_SEPARATOR.$file
: $file;
}
}

Expand All @@ -73,8 +88,9 @@ protected function getFilePathsMatchingPattern($pattern)
public function basePathForLogs(): string
{
$rootFolder = Str::of(config('log-viewer.filesystem.root'));
return empty($rootFolder)
? $rootFolder->finish('/')

return ($rootFolder != '')
? $rootFolder->finish(DIRECTORY_SEPARATOR)
: $rootFolder;
}

Expand Down Expand Up @@ -153,8 +169,17 @@ public function getRouteMiddleware(): array
return config('log-viewer.middleware', []) ?: ['web'];
}

public function getFilesystem(): Filesystem
public function getFilesystem($absolutePath = ''): Filesystem
{
if (! config('disable_absolute_filepaths') && ($absolutePath !== '') && is_dir($absolutePath)) {
config()->set('filesystems.disks.log-viewer-absolute', [
'driver' => 'local',
'root' => $absolutePath,
]);

return Storage::disk('log-viewer-absolute');
}

return Storage::disk(config('log-viewer.filesystem.disk'));
}

Expand Down
2 changes: 1 addition & 1 deletion src/LogViewerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function register()

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

$this->app->bind('log-viewer', LogViewerService::class);
Expand Down
12 changes: 6 additions & 6 deletions src/Utils/GenerateCacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ public static function for(mixed $object, ?string $namespace = null): string
$key = '';

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

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

if (is_string($object)) {
$key = self::baseKey() . ':' . $object;
$key = self::baseKey().':'.$object;
}

if (!empty($namespace)) {
$key .= ':' . $namespace;
if (! empty($namespace)) {
$key .= ':'.$namespace;
}

return $key;
}

protected static function baseKey(): string
{
return 'log-viewer:' . LogViewer::version();
return 'log-viewer:'.LogViewer::version();
}
}
8 changes: 4 additions & 4 deletions tests/Unit/GenerateCacheKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$result = GenerateCacheKey::for($file);

expect($result)->toBe(
'log-viewer:' . LogViewer::version() . ':file:' . md5($file->path)
'log-viewer:'.LogViewer::version().':file:'.md5($file->path)
);
});

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

expect($result)->toBe(
GenerateCacheKey::for($file) . ':' . $namespace
GenerateCacheKey::for($file).':'.$namespace
);
});

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

expect($result)->toBe(
GenerateCacheKey::for($logIndex->file) . ':' . $logIndex->identifier
GenerateCacheKey::for($logIndex->file).':'.$logIndex->identifier
);
});

Expand All @@ -39,5 +39,5 @@

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

expect($result)->toBe('log-viewer:' . LogViewer::version() . ':' . $string);
expect($result)->toBe('log-viewer:'.LogViewer::version().':'.$string);
});
1 change: 0 additions & 1 deletion tests/Unit/LogIndex/ChunkedIndicesTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Opcodes\LogViewer\Exceptions\InvalidChunkSizeException;
use Opcodes\LogViewer\LogFile;

it('can set the chunk size for the log index', function () {
$logIndex = createLogIndex();
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/LogIndex/LogIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Opcodes\LogViewer\Facades\Cache;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogFile;
use Opcodes\LogViewer\Utils\GenerateCacheKey;

it('starts off with an empty index', function () {
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/LogReaderTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Illuminate\Support\Facades\File;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogFile;

Expand Down
1 change: 0 additions & 1 deletion tests/Unit/LogViewerCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Illuminate\Cache\FileStore;
use Illuminate\Cache\RedisStore;
use Illuminate\Cache\Repository;

it('it defaults to the app\'s default cache driver', function ($cacheType, $cacheStoreClass) {
config(['cache.default' => $cacheType]);
Expand Down

0 comments on commit 65be109

Please sign in to comment.