Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally group file list by subfolder #86

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/log-viewer.php
Expand Up @@ -47,6 +47,17 @@

'include_files' => ['*.log'],

'include_recursively' => false,

/*
|--------------------------------------------------------------------------
| File list grouping
|--------------------------------------------------------------------------
|
*/

'group_by_subfolder' => false,

/*
|--------------------------------------------------------------------------
| Exclude file patterns.
Expand Down
119 changes: 67 additions & 52 deletions resources/views/livewire/file-list.blade.php
@@ -1,13 +1,17 @@
<div class="relative h-full overflow-hidden" x-cloak>
<div class="pointer-events-none absolute z-10 top-0 h-6 w-full bg-gradient-to-b from-gray-100 dark:from-gray-900 to-transparent"></div>
<div class="file-list" x-ref="list">
@foreach($files as $logFile)
<div class="file-item-container"
x-bind:class="[selectedFileIdentifier && selectedFileIdentifier === '{{ $logFile->identifier }}' ? 'active' : '']"
wire:key="log-file-{{$logFile->identifier}}"
wire:click="selectFile('{{ $logFile->identifier }}')"
x-on:click="selectFile('{{ $logFile->identifier }}')"
x-data="{
@foreach($files as $subFolder => $logFiles)
@if($subFolder !== '_root')
<div class="text-gray-400">{{ $subFolder }}</div>
@endif
@foreach($logFiles as $logFile)
<div class="file-item-container"
x-bind:class="[selectedFileIdentifier && selectedFileIdentifier === '{{ $logFile->identifier }}' ? 'active' : '']"
wire:key="log-file-{{$logFile->identifier}}"
wire:click="selectFile('{{ $logFile->identifier }}')"
x-on:click="selectFile('{{ $logFile->identifier }}')"
x-data="{
open: false,
direction: 'down',
toggle() {
Expand All @@ -23,57 +27,68 @@
focusAfter?.focus()
}
}"
x-on:keydown.escape.prevent.stop="close($refs.button)"
x-on:focusin.window="! $refs.panel.contains($event.target) && close()"
x-id="['dropdown-button']"
>
<div class="file-item">
<p class="file-name"><span class="text-gray-400 dark:text-gray-500">{{ str_replace(DIRECTORY_SEPARATOR, ' '.DIRECTORY_SEPARATOR.' ', $logFile->subFolder) }}</span> {{ $logFile->name }}</p>
<span class="file-size">{{ $logFile->sizeFormatted() }}</span>
<button type="button" class="file-dropdown-toggle"
x-ref="button" x-on:click.stop="toggle()" :aria-expanded="open" :aria-controls="$id('dropdown-button')"
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><use href="#icon-more" /></svg>
</button>
</div>

<div
x-ref="panel"
x-show="open"
x-transition:enter="transition ease-out duration-100"
x-transition:enter-start="opacity-0 scale-90"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition ease-in duration-100"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-90"
x-on:click.outside="close($refs.button)"
:id="$id('dropdown-button')"
class="dropdown w-48"
:class="direction"
x-on:keydown.escape.prevent.stop="close($refs.button)"
x-on:focusin.window="! $refs.panel.contains($event.target) && close()"
x-id="['dropdown-button']"
>
<div class="py-2">
<button wire:click="clearCache('{{ $logFile->identifier }}')" x-on:click.stop="close($refs.button)">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><use href="#icon-database" /></svg>
Clear cache
<div class="file-item">
<p class="file-name">
<span class="text-gray-400 dark:text-gray-500">{{ str_replace(DIRECTORY_SEPARATOR, ' '.DIRECTORY_SEPARATOR.' ', $logFile->subFolder) }}</span> {{ $logFile->name }}
</p>
<span class="file-size">{{ $logFile->sizeFormatted() }}</span>
<button type="button" class="file-dropdown-toggle"
x-ref="button" x-on:click.stop="toggle()" :aria-expanded="open" :aria-controls="$id('dropdown-button')"
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<use href="#icon-more" />
</svg>
</button>
</div>

@can('downloadLogFile', $logFile)
<a href="{{ $logFile->downloadUrl() }}" x-on:click.stop="">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><use href="#icon-download" /></svg>
Download
</a>
@endcan
<div
x-ref="panel"
x-show="open"
x-transition:enter="transition ease-out duration-100"
x-transition:enter-start="opacity-0 scale-90"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition ease-in duration-100"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-90"
x-on:click.outside="close($refs.button)"
:id="$id('dropdown-button')"
class="dropdown w-48"
:class="direction"
>
<div class="py-2">
<button wire:click="clearCache('{{ $logFile->identifier }}')" x-on:click.stop="close($refs.button)">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<use href="#icon-database" />
</svg>
Clear cache
</button>

@can('deleteLogFile', $logFile)
<div class="divider"></div>
<button x-on:click.stop="if (confirm('Are you sure you want to delete the log file \'{{ $logFile->name }}\'')) { $wire.call('deleteFile', '{{ $logFile->identifier }}') }">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><use href="#icon-trashcan" /></svg>
Delete
</button>
@endcan
@can('downloadLogFile', $logFile)
<a href="{{ $logFile->downloadUrl() }}" x-on:click.stop="">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<use href="#icon-download" />
</svg>
Download
</a>
@endcan

@can('deleteLogFile', $logFile)
<div class="divider"></div>
<button x-on:click.stop="if (confirm('Are you sure you want to delete the log file \'{{ $logFile->name }}\'')) { $wire.call('deleteFile', '{{ $logFile->identifier }}') }">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<use href="#icon-trashcan" />
</svg>
Delete
</button>
@endcan
</div>
</div>
</div>
</div>
@endforeach
@endforeach
</div>
<div class="pointer-events-none absolute z-10 bottom-0 h-8 w-full bg-gradient-to-t from-gray-100 dark:from-gray-900 to-transparent"></div>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Expand Up @@ -21,7 +21,7 @@

Route::get('file/{fileIdentifier}/download', function (string $fileIdentifier) {
LogViewer::auth();

$file = LogViewer::getFile($fileIdentifier);

abort_if(is_null($file), 404);
Expand Down
11 changes: 10 additions & 1 deletion src/Http/Livewire/FileList.php
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Gate;
use Livewire\Component;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogFile;

class FileList extends Component
{
Expand All @@ -22,7 +23,15 @@ public function mount(string $selectedFileIdentifier = null)
public function render()
{
return view('log-viewer::livewire.file-list', [
'files' => LogViewer::getFiles(),
'files' => LogViewer::getFiles()->mapToGroups(function (LogFile $file) {
if (config('log-viewer.group_by_subfolder')) {
$group = $file->subFolder ?: '_root';
} else {
$group = '_root';
}

return [$group => $file];
})->sortKeys(),
]);
}

Expand Down
23 changes: 11 additions & 12 deletions src/LogViewerService.php
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;

class LogViewerService
{
Expand All @@ -20,19 +20,18 @@ class LogViewerService

protected function getFilePaths(): array
{
$files = [];

foreach (config('log-viewer.include_files', []) as $pattern) {
$files = array_merge($files, glob(Str::finish(storage_path('logs'), DIRECTORY_SEPARATOR).$pattern));
}

foreach (config('log-viewer.exclude_files', []) as $pattern) {
$files = array_diff($files, glob(Str::finish(storage_path('logs'), DIRECTORY_SEPARATOR).$pattern));
$finder = Finder::create()->in(storage_path('logs'))
->files()
->name(config('log-viewer.include_files', []))
->notName(config('log-viewer.exclude_files', []))
->sortByModifiedTime()
->reverseSorting();

if (! config('log-viewer.include_recursively', false)) {
$finder->depth('== 0');
}

$files = array_reverse($files);

return array_filter($files, 'is_file');
return array_keys(iterator_to_array($finder));
}

/**
Expand Down