-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathUploadLogsAction.php
More file actions
115 lines (92 loc) · 3.65 KB
/
UploadLogsAction.php
File metadata and controls
115 lines (92 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace FlexKleks\PasteFoxShare\Filament\Components\Actions;
use App\Models\Server;
use Exception;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Filament\Support\Enums\Size;
use Illuminate\Support\Facades\Http;
class UploadLogsAction extends Action
{
public static function getDefaultName(): ?string
{
return 'upload_logs_pastefox';
}
protected function setUp(): void
{
parent::setUp();
$this->hidden(function () {
/** @var Server $server */
$server = Filament::getTenant();
return $server->retrieveStatus()->isOffline();
});
$this->label(fn () => trans('pastefox-share::messages.share_logs'));
$this->icon('tabler-share');
$this->color('primary');
$this->size(Size::ExtraLarge);
$this->action(function () {
/** @var Server $server */
$server = Filament::getTenant();
try {
$logs = Http::daemon($server->node)
->get("/api/servers/{$server->uuid}/logs", [
'size' => 5000,
])
->throw()
->json('data');
$logs = is_array($logs) ? implode(PHP_EOL, $logs) : $logs;
$apiKey = config('pastefox-share.api_key');
$hasApiKey = filled($apiKey);
$headers = ['Content-Type' => 'application/json'];
$payload = [
'content' => $logs,
'title' => 'Console Logs: ' . $server->name . ' - ' . now()->format('Y-m-d H:i:s'),
'language' => 'log',
'effect' => config('pastefox-share.effect'),
'theme' => config('pastefox-share.theme'),
];
if ($hasApiKey) {
$headers['X-API-Key'] = $apiKey;
$payload['visibility'] = config('pastefox-share.visibility');
$password = config('pastefox-share.password');
if (filled($password)) {
$payload['password'] = $password;
}
}
$response = Http::withHeaders($headers)
->post('https://pastefox.com/api/pastes', $payload)
->timeout(30)
->connectTimeout(5)
->throw()
->json();
if ($response['success']) {
$url = 'https://pastefox.com/'.$response['data']['slug'];
$body = $url;
if (!$hasApiKey) {
$body .= "\n".trans('pastefox-share::messages.expires_7_days');
}
Notification::make()
->title(trans('pastefox-share::messages.uploaded'))
->body($body)
->persistent()
->success()
->send();
} else {
Notification::make()
->title(trans('pastefox-share::messages.upload_failed'))
->body($response['error'] ?? 'Unknown error')
->danger()
->send();
}
} catch (Exception $exception) {
report($exception);
Notification::make()
->title(trans('pastefox-share::messages.upload_failed'))
->body($exception->getMessage())
->danger()
->send();
}
});
}
}