Upload beautify

This commit is contained in:
2026-02-14 15:14:12 +01:00
parent e129618910
commit 79192345e3
249 changed files with 24436 additions and 1021 deletions

View File

@@ -0,0 +1,117 @@
<?php
declare(strict_types=1);
namespace App\Uploads\Jobs;
use App\Services\Upload\PreviewService;
use App\Uploads\Jobs\TagAnalysisJob;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
final class PreviewGenerationJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public int $tries = 3;
public int $timeout = 45;
public function __construct(private readonly string $uploadId)
{
}
public function handle(PreviewService $previewService): void
{
$upload = DB::table('uploads')->where('id', $this->uploadId)->first();
if (! $upload) {
return;
}
if ((string) $upload->status !== 'draft' || ! (bool) $upload->is_scanned) {
return;
}
$this->advanceProcessingState('generating_preview', ['pending_scan', 'scanning', 'generating_preview']);
$previewData = null;
if ((string) $upload->type === 'image') {
$main = DB::table('upload_files')
->where('upload_id', $this->uploadId)
->where('type', 'main')
->orderBy('id')
->first(['path']);
if (! $main || ! Storage::disk('local')->exists((string) $main->path)) {
return;
}
$previewData = $previewService->generateFromImage($this->uploadId, (string) $main->path);
} elseif ((string) $upload->type === 'archive') {
$screenshot = DB::table('upload_files')
->where('upload_id', $this->uploadId)
->where('type', 'screenshot')
->orderBy('id')
->first(['path']);
$previewData = $previewService->generateFromArchive($this->uploadId, $screenshot?->path ? (string) $screenshot->path : null);
} else {
return;
}
$previewPath = (string) ($previewData['preview_path'] ?? '');
if ($previewPath === '') {
return;
}
DB::table('uploads')->where('id', $this->uploadId)->update([
'preview_path' => $previewPath,
'updated_at' => now(),
]);
$this->advanceProcessingState('analyzing_tags', ['pending_scan', 'scanning', 'generating_preview', 'analyzing_tags']);
DB::table('upload_files')
->where('upload_id', $this->uploadId)
->where('type', 'preview')
->delete();
DB::table('upload_files')->insert([
'upload_id' => $this->uploadId,
'path' => $previewPath,
'type' => 'preview',
'hash' => null,
'size' => Storage::disk('local')->exists($previewPath) ? Storage::disk('local')->size($previewPath) : null,
'mime' => 'image/webp',
'created_at' => now(),
]);
TagAnalysisJob::dispatch($this->uploadId);
}
/**
* @param array<int, string> $allowedCurrentStates
*/
private function advanceProcessingState(string $targetState, array $allowedCurrentStates): void
{
DB::table('uploads')
->where('id', $this->uploadId)
->where('status', 'draft')
->where(function ($query) use ($allowedCurrentStates): void {
$query->whereNull('processing_state')
->orWhereIn('processing_state', $allowedCurrentStates);
})
->update([
'processing_state' => $targetState,
'updated_at' => now(),
]);
}
}

View File

@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace App\Uploads\Jobs;
use App\Services\Upload\TagAnalysisService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
final class TagAnalysisJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public int $tries = 3;
public int $timeout = 30;
public function __construct(private readonly string $uploadId)
{
}
public function handle(TagAnalysisService $analysis): void
{
$upload = DB::table('uploads')->where('id', $this->uploadId)->first();
if (! $upload) {
return;
}
if ((string) $upload->status !== 'draft') {
return;
}
if (! (bool) $upload->is_scanned) {
return;
}
if (empty($upload->preview_path)) {
return;
}
$this->advanceProcessingState('analyzing_tags', ['pending_scan', 'scanning', 'generating_preview', 'analyzing_tags']);
$main = DB::table('upload_files')
->where('upload_id', $this->uploadId)
->where('type', 'main')
->orderBy('id')
->first(['path']);
$filename = $main ? basename((string) $main->path) : '';
$categoryContext = null;
if (! empty($upload->category_id)) {
$category = DB::table('categories')->where('id', (int) $upload->category_id)->first(['name', 'slug']);
if ($category) {
$categoryContext = (string) ($category->name ?: $category->slug);
}
}
$tags = $analysis->analyze($filename, (string) $upload->preview_path, $categoryContext);
DB::transaction(function () use ($tags): void {
DB::table('upload_tags')->where('upload_id', $this->uploadId)->delete();
foreach ($tags as $row) {
$tagName = (string) ($row['tag'] ?? '');
if ($tagName === '') {
continue;
}
$slug = $tagName;
$tag = DB::table('tags')->where('slug', $slug)->first(['id']);
if (! $tag) {
$tagId = DB::table('tags')->insertGetId([
'name' => $tagName,
'slug' => $slug,
'usage_count' => 0,
'is_active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
} else {
$tagId = (int) $tag->id;
}
DB::table('upload_tags')->insert([
'upload_id' => $this->uploadId,
'tag_id' => $tagId,
'confidence' => (float) ($row['confidence'] ?? 0.0),
'source' => (string) ($row['source'] ?? 'manual'),
'created_at' => now(),
'updated_at' => now(),
]);
}
DB::table('uploads')->where('id', $this->uploadId)->update([
'has_tags' => true,
'processing_state' => DB::raw("CASE WHEN processing_state IN ('ready','published','rejected') THEN processing_state ELSE 'ready' END"),
'updated_at' => now(),
]);
});
}
/**
* @param array<int, string> $allowedCurrentStates
*/
private function advanceProcessingState(string $targetState, array $allowedCurrentStates): void
{
DB::table('uploads')
->where('id', $this->uploadId)
->where('status', 'draft')
->where(function ($query) use ($allowedCurrentStates): void {
$query->whereNull('processing_state')
->orWhereIn('processing_state', $allowedCurrentStates);
})
->update([
'processing_state' => $targetState,
'updated_at' => now(),
]);
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace App\Uploads\Jobs;
use App\Services\Uploads\UploadScanService;
use App\Uploads\Jobs\PreviewGenerationJob;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
final class VirusScanJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public int $tries = 3;
public int $timeout = 30;
public function __construct(private readonly string $uploadId)
{
}
public function handle(UploadScanService $scanner): void
{
$upload = DB::table('uploads')->where('id', $this->uploadId)->first();
if (! $upload || (string) $upload->status !== 'draft') {
return;
}
$this->advanceProcessingState('scanning', ['pending_scan', 'scanning']);
$files = DB::table('upload_files')
->where('upload_id', $this->uploadId)
->whereIn('type', ['main', 'screenshot'])
->get(['path']);
foreach ($files as $file) {
$path = (string) ($file->path ?? '');
if ($path === '' || ! Storage::disk('local')->exists($path)) {
continue;
}
$absolute = Storage::disk('local')->path($path);
$result = $scanner->scan($absolute);
if (! $result->ok) {
DB::table('uploads')->where('id', $this->uploadId)->update([
'status' => 'rejected',
'processing_state' => 'rejected',
'updated_at' => now(),
]);
Storage::disk('local')->deleteDirectory('tmp/drafts/' . $this->uploadId);
return;
}
}
DB::table('uploads')->where('id', $this->uploadId)->update([
'is_scanned' => true,
'updated_at' => now(),
]);
$this->advanceProcessingState('generating_preview', ['pending_scan', 'scanning', 'generating_preview']);
PreviewGenerationJob::dispatch($this->uploadId);
}
/**
* @param array<int, string> $allowedCurrentStates
*/
private function advanceProcessingState(string $targetState, array $allowedCurrentStates): void
{
DB::table('uploads')
->where('id', $this->uploadId)
->where('status', 'draft')
->where(function ($query) use ($allowedCurrentStates): void {
$query->whereNull('processing_state')
->orWhereIn('processing_state', $allowedCurrentStates);
})
->update([
'processing_state' => $targetState,
'updated_at' => now(),
]);
}
}