|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Admin\Pages; |
| 4 | + |
| 5 | +use Boquizo\FilamentLogViewer\Actions\DeleteAction; |
| 6 | +use Boquizo\FilamentLogViewer\Actions\DownloadAction; |
| 7 | +use Boquizo\FilamentLogViewer\Actions\ViewLogAction; |
| 8 | +use Boquizo\FilamentLogViewer\Pages\ListLogs as BaseListLogs; |
| 9 | +use Boquizo\FilamentLogViewer\Tables\Columns\LevelColumn; |
| 10 | +use Boquizo\FilamentLogViewer\Tables\Columns\NameColumn; |
| 11 | +use Boquizo\FilamentLogViewer\Utils\Level; |
| 12 | +use Filament\Actions\Action; |
| 13 | +use Filament\Notifications\Notification; |
| 14 | +use Filament\Support\Enums\IconSize; |
| 15 | +use Filament\Tables\Table; |
| 16 | +use Illuminate\Support\Facades\Http; |
| 17 | + |
| 18 | +class ListLogs extends BaseListLogs |
| 19 | +{ |
| 20 | + protected string $view = 'filament.components.list-logs'; |
| 21 | + |
| 22 | + public function getHeading(): string|null|\Illuminate\Contracts\Support\Htmlable |
| 23 | + { |
| 24 | + return trans('admin/log.navigation.panel_logs'); |
| 25 | + } |
| 26 | + |
| 27 | + public static function table(Table $table): Table |
| 28 | + { |
| 29 | + return parent::table($table) |
| 30 | + ->emptyStateHeading(trans('admin/log.empty_table')) |
| 31 | + ->emptyStateIcon('tabler-check') |
| 32 | + ->columns([ |
| 33 | + NameColumn::make('date'), |
| 34 | + LevelColumn::make(Level::ALL) |
| 35 | + ->tooltip(trans('admin/log.total_logs')), |
| 36 | + LevelColumn::make(Level::Error) |
| 37 | + ->tooltip(trans('admin/log.error')), |
| 38 | + LevelColumn::make(Level::Warning) |
| 39 | + ->tooltip(trans('admin/log.warning')), |
| 40 | + LevelColumn::make(Level::Notice) |
| 41 | + ->tooltip(trans('admin/log.notice')), |
| 42 | + LevelColumn::make(Level::Info) |
| 43 | + ->tooltip(trans('admin/log.info')), |
| 44 | + LevelColumn::make(Level::Debug) |
| 45 | + ->tooltip(trans('admin/log.debug')), |
| 46 | + ]) |
| 47 | + ->recordActions([ |
| 48 | + ViewLogAction::make() |
| 49 | + ->icon('tabler-file-description')->iconSize(IconSize::Medium), |
| 50 | + DownloadAction::make() |
| 51 | + ->icon('tabler-file-download')->iconSize(IconSize::Medium), |
| 52 | + Action::make('uploadLogs') |
| 53 | + ->button() |
| 54 | + ->hiddenLabel() |
| 55 | + ->icon('tabler-world-upload')->iconSize(IconSize::Medium) |
| 56 | + ->requiresConfirmation() |
| 57 | + ->modalHeading(trans('admin/log.actions.upload_logs')) |
| 58 | + ->modalDescription(fn ($record) => trans('admin/log.actions.upload_logs_description', ['file' => $record['date'], 'url' => 'https://logs.pelican.dev'])) |
| 59 | + ->action(function ($record) { |
| 60 | + $logPath = storage_path('logs/' . $record['date']); |
| 61 | + |
| 62 | + if (!file_exists($logPath)) { |
| 63 | + Notification::make() |
| 64 | + ->title(trans('admin/log.actions.log_not_found')) |
| 65 | + ->body(trans('admin/log.actions.log_not_found_description', ['filename' => $record['date']])) |
| 66 | + ->danger() |
| 67 | + ->send(); |
| 68 | + |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
| 73 | + $totalLines = count($lines); |
| 74 | + $uploadLines = $totalLines <= 1000 ? $lines : array_slice($lines, -1000); |
| 75 | + $content = implode("\n", $uploadLines); |
| 76 | + |
| 77 | + $logUrl = 'https://logs.pelican.dev'; |
| 78 | + try { |
| 79 | + $response = Http::timeout(10)->asMultipart()->post($logUrl, [ |
| 80 | + [ |
| 81 | + 'name' => 'c', |
| 82 | + 'contents' => $content, |
| 83 | + ], |
| 84 | + [ |
| 85 | + 'name' => 'e', |
| 86 | + 'contents' => '14d', |
| 87 | + ], |
| 88 | + ]); |
| 89 | + |
| 90 | + if ($response->failed()) { |
| 91 | + Notification::make() |
| 92 | + ->title(trans('admin/log.actions.failed_to_upload')) |
| 93 | + ->body(trans('admin/log.actions.failed_to_upload_description', ['status' => $response->status()])) |
| 94 | + ->danger() |
| 95 | + ->send(); |
| 96 | + |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + $data = $response->json(); |
| 101 | + $url = $data['url']; |
| 102 | + |
| 103 | + Notification::make() |
| 104 | + ->title(trans('admin/log.actions.log_upload')) |
| 105 | + ->body("{$url}") |
| 106 | + ->success() |
| 107 | + ->actions([ |
| 108 | + Action::make('viewLogs') |
| 109 | + ->label(trans('admin/log.actions.view_logs')) |
| 110 | + ->url($url) |
| 111 | + ->openUrlInNewTab(true), |
| 112 | + ]) |
| 113 | + ->persistent() |
| 114 | + ->send(); |
| 115 | + |
| 116 | + } catch (\Exception $e) { |
| 117 | + Notification::make() |
| 118 | + ->title(trans('admin/log.actions.failed_to_upload')) |
| 119 | + ->body($e->getMessage()) |
| 120 | + ->danger() |
| 121 | + ->send(); |
| 122 | + |
| 123 | + return; |
| 124 | + } |
| 125 | + }), |
| 126 | + DeleteAction::make() |
| 127 | + ->icon('tabler-trash')->iconSize(IconSize::Medium), |
| 128 | + ]); |
| 129 | + } |
| 130 | +} |
0 commit comments