feat: system tools and admin enhancements
This commit is contained in:
93
app/Console/Commands/CronRun.php
Normal file
93
app/Console/Commands/CronRun.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Attachment;
|
||||
use App\Services\AttachmentThumbnailService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class CronRun extends Command
|
||||
{
|
||||
protected $signature = 'speedbb:cron {--force : Recreate thumbnails even if already present} {--dry-run : Report without writing}';
|
||||
|
||||
protected $description = 'Run periodic maintenance tasks (currently: attachment thumbnail recreation).';
|
||||
|
||||
public function handle(AttachmentThumbnailService $thumbnailService): int
|
||||
{
|
||||
$force = (bool) $this->option('force');
|
||||
$dryRun = (bool) $this->option('dry-run');
|
||||
|
||||
$stats = [
|
||||
'checked' => 0,
|
||||
'created' => 0,
|
||||
'skipped' => 0,
|
||||
'missing' => 0,
|
||||
'non_image' => 0,
|
||||
];
|
||||
|
||||
$this->info('Processing attachment thumbnails...');
|
||||
|
||||
Attachment::query()
|
||||
->orderBy('id')
|
||||
->chunkById(200, function ($attachments) use ($thumbnailService, $force, $dryRun, &$stats) {
|
||||
foreach ($attachments as $attachment) {
|
||||
$stats['checked']++;
|
||||
|
||||
$mime = $attachment->mime_type ?? '';
|
||||
if (!str_starts_with($mime, 'image/')) {
|
||||
$stats['non_image']++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$disk = Storage::disk($attachment->disk);
|
||||
if (!$disk->exists($attachment->path)) {
|
||||
$stats['missing']++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$needsThumbnail = $force
|
||||
|| !$attachment->thumbnail_path
|
||||
|| !$disk->exists($attachment->thumbnail_path);
|
||||
|
||||
if (!$needsThumbnail) {
|
||||
$stats['skipped']++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($dryRun) {
|
||||
$stats['created']++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($force && $attachment->thumbnail_path && $disk->exists($attachment->thumbnail_path)) {
|
||||
$disk->delete($attachment->thumbnail_path);
|
||||
}
|
||||
|
||||
$payload = $thumbnailService->createForAttachment($attachment, $force);
|
||||
if (!$payload) {
|
||||
$stats['skipped']++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$attachment->thumbnail_path = $payload['path'] ?? null;
|
||||
$attachment->thumbnail_mime_type = $payload['mime'] ?? null;
|
||||
$attachment->thumbnail_size_bytes = $payload['size'] ?? null;
|
||||
$attachment->save();
|
||||
|
||||
$stats['created']++;
|
||||
}
|
||||
});
|
||||
|
||||
$this->info(sprintf(
|
||||
'Checked: %d | Created: %d | Skipped: %d | Missing: %d | Non-image: %d',
|
||||
$stats['checked'],
|
||||
$stats['created'],
|
||||
$stats['skipped'],
|
||||
$stats['missing'],
|
||||
$stats['non_image']
|
||||
));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
89
app/Console/Commands/VersionBump.php
Normal file
89
app/Console/Commands/VersionBump.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class VersionBump extends Command
|
||||
{
|
||||
protected $signature = 'version:bump';
|
||||
|
||||
protected $description = 'Bump the patch version (e.g. 26.0.1 -> 26.0.2).';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$current = Setting::query()->where('key', 'version')->value('value');
|
||||
if (!$current) {
|
||||
$this->error('Unable to determine current version from settings.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$next = $this->bumpPatch($current);
|
||||
if ($next === null) {
|
||||
$this->error('Version format must be X.Y.Z (optionally with suffix).');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
Setting::updateOrCreate(['key' => 'version'], ['value' => $next]);
|
||||
|
||||
if (!$this->syncComposerVersion($next)) {
|
||||
$this->error('Failed to sync version to composer.json.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->info("Version bumped: {$current} -> {$next}");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function bumpPatch(string $version): ?string
|
||||
{
|
||||
if (!preg_match('/^(\d+)\.(\d+)\.(\d+)(.*)?$/', $version, $matches)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$major = $matches[1];
|
||||
$minor = $matches[2];
|
||||
$patch = $matches[3];
|
||||
$suffix = $matches[4] ?? '';
|
||||
|
||||
$patchWidth = strlen($patch);
|
||||
$nextPatch = (string) ((int) $patch + 1);
|
||||
if ($patchWidth > 1) {
|
||||
$nextPatch = str_pad($nextPatch, $patchWidth, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
return "{$major}.{$minor}.{$nextPatch}{$suffix}";
|
||||
}
|
||||
|
||||
private function syncComposerVersion(string $version): bool
|
||||
{
|
||||
$composerPath = base_path('composer.json');
|
||||
|
||||
if (!is_file($composerPath) || !is_readable($composerPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$raw = file_get_contents($composerPath);
|
||||
if ($raw === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data['version'] = $version;
|
||||
|
||||
$encoded = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
if ($encoded === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$encoded .= "\n";
|
||||
|
||||
return file_put_contents($composerPath, $encoded) !== false;
|
||||
}
|
||||
}
|
||||
113
app/Console/Commands/VersionRelease.php
Normal file
113
app/Console/Commands/VersionRelease.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class VersionRelease extends Command
|
||||
{
|
||||
protected $signature = 'version:release {--prerelease : Mark this release as a prerelease} {--target= : Override target commit (defaults to env GITEA_TARGET_COMMIT or master)}';
|
||||
|
||||
protected $description = 'Create or update a Gitea release for the current version.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$version = Setting::query()->where('key', 'version')->value('value');
|
||||
if (!$version) {
|
||||
$this->error('Unable to determine version from settings.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$token = env('GITEA_TOKEN');
|
||||
$owner = env('GITEA_OWNER');
|
||||
$repo = env('GITEA_REPO');
|
||||
$apiBase = rtrim((string) env('GITEA_API_BASE', 'https://git.24unix.net/api/v1'), '/');
|
||||
$target = $this->option('target') ?: env('GITEA_TARGET_COMMIT', 'master');
|
||||
$prerelease = $this->option('prerelease') || filter_var(env('GITEA_PRERELEASE', false), FILTER_VALIDATE_BOOLEAN);
|
||||
|
||||
if (!$token || !$owner || !$repo) {
|
||||
$this->error('Missing Gitea config. Set GITEA_TOKEN, GITEA_OWNER, and GITEA_REPO in .env.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$tag = "v{$version}";
|
||||
$body = $this->resolveChangelogBody($version);
|
||||
|
||||
$client = Http::withHeaders([
|
||||
'Authorization' => "token {$token}",
|
||||
'Accept' => 'application/json',
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'tag_name' => $tag,
|
||||
'target_commitish' => $target,
|
||||
'name' => $tag,
|
||||
'body' => $body,
|
||||
'prerelease' => (bool) $prerelease,
|
||||
];
|
||||
|
||||
$createUrl = "{$apiBase}/repos/{$owner}/{$repo}/releases";
|
||||
$response = $client->post($createUrl, $payload);
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->info("Release created: {$tag}");
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
if ($response->status() === 409 || $response->status() === 422) {
|
||||
$getUrl = "{$apiBase}/repos/{$owner}/{$repo}/releases/tags/{$tag}";
|
||||
$existing = $client->get($getUrl);
|
||||
if (!$existing->successful()) {
|
||||
$this->error('Release already exists, but failed to fetch it for update.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
$id = $existing->json('id');
|
||||
if (!$id) {
|
||||
$this->error('Release already exists, but no ID was returned.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$updateUrl = "{$apiBase}/repos/{$owner}/{$repo}/releases/{$id}";
|
||||
$updatePayload = [
|
||||
'name' => $tag,
|
||||
'body' => $body,
|
||||
'prerelease' => (bool) $prerelease,
|
||||
'target_commitish' => $target,
|
||||
];
|
||||
$updated = $client->patch($updateUrl, $updatePayload);
|
||||
if ($updated->successful()) {
|
||||
$this->info("Release updated: {$tag}");
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$this->error("Failed to update release: {$updated->status()}");
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->error("Failed to create release: {$response->status()}");
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
private function resolveChangelogBody(string $version): string
|
||||
{
|
||||
$path = base_path('CHANGELOG.md');
|
||||
if (!is_file($path) || !is_readable($path)) {
|
||||
return 'See commit history for details.';
|
||||
}
|
||||
|
||||
$raw = file_get_contents($path);
|
||||
if ($raw === false) {
|
||||
return 'See commit history for details.';
|
||||
}
|
||||
|
||||
$pattern = '/^##\\s+' . preg_quote($version, '/') . '\\s*\\R(.*?)(?=^##\\s+|\\z)/ms';
|
||||
if (preg_match($pattern, $raw, $matches)) {
|
||||
$body = trim($matches[1] ?? '');
|
||||
return $body !== '' ? $body : 'See commit history for details.';
|
||||
}
|
||||
|
||||
return 'See commit history for details.';
|
||||
}
|
||||
}
|
||||
73
app/Console/Commands/VersionSet.php
Normal file
73
app/Console/Commands/VersionSet.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class VersionSet extends Command
|
||||
{
|
||||
protected $signature = 'version:set {version}';
|
||||
|
||||
protected $description = 'Set the forum version (e.g. 26.0.1).';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$version = trim((string) $this->argument('version'));
|
||||
if (!$this->isValidVersion($version)) {
|
||||
$this->error('Version format must be X.Y or X.Y.Z (optionally with suffix).');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$current = Setting::query()->where('key', 'version')->value('value');
|
||||
Setting::updateOrCreate(['key' => 'version'], ['value' => $version]);
|
||||
|
||||
if (!$this->syncComposerVersion($version)) {
|
||||
$this->error('Failed to sync version to composer.json.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if ($current) {
|
||||
$this->info("Version updated: {$current} -> {$version}");
|
||||
} else {
|
||||
$this->info("Version set to {$version}");
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function isValidVersion(string $version): bool
|
||||
{
|
||||
return (bool) preg_match('/^\d+\.\d+(?:\.\d+)?(?:[-._][0-9A-Za-z.-]+)?$/', $version);
|
||||
}
|
||||
|
||||
private function syncComposerVersion(string $version): bool
|
||||
{
|
||||
$composerPath = base_path('composer.json');
|
||||
|
||||
if (!is_file($composerPath) || !is_readable($composerPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$raw = file_get_contents($composerPath);
|
||||
if ($raw === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data['version'] = $version;
|
||||
|
||||
$encoded = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
if ($encoded === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$encoded .= "\n";
|
||||
|
||||
return file_put_contents($composerPath, $encoded) !== false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user