geschenkgutscheine implementiert ("Paket verschenken"). Neuer Upload-Provider: Sparkbooth.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Photobooth\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class SparkboothUploadException extends RuntimeException
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $reason,
|
||||
string $message,
|
||||
public readonly ?int $statusCode = null
|
||||
) {
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class PhotoboothIngestService
|
||||
$this->hasPathColumn = Schema::hasColumn('photos', 'path');
|
||||
}
|
||||
|
||||
public function ingest(Event $event, ?int $maxFiles = null): array
|
||||
public function ingest(Event $event, ?int $maxFiles = null, string $ingestSource = Photo::SOURCE_PHOTOBOOTH): array
|
||||
{
|
||||
$tenant = $event->tenant;
|
||||
|
||||
@@ -90,7 +90,7 @@ class PhotoboothIngestService
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->importFile($event, $eventPackage, $disk, $importDisk, $file);
|
||||
$result = $this->importFile($event, $eventPackage, $disk, $importDisk, $file, $ingestSource);
|
||||
if ($result) {
|
||||
$processed++;
|
||||
Storage::disk($importDisk)->delete($file);
|
||||
@@ -116,6 +116,7 @@ class PhotoboothIngestService
|
||||
string $destinationDisk,
|
||||
string $importDisk,
|
||||
string $file,
|
||||
string $ingestSource,
|
||||
): bool {
|
||||
$stream = Storage::disk($importDisk)->readStream($file);
|
||||
|
||||
@@ -191,8 +192,8 @@ class PhotoboothIngestService
|
||||
'file_path' => $watermarkedPath,
|
||||
'thumbnail_path' => $watermarkedThumb,
|
||||
'status' => 'pending',
|
||||
'guest_name' => Photo::SOURCE_PHOTOBOOTH,
|
||||
'ingest_source' => Photo::SOURCE_PHOTOBOOTH,
|
||||
'guest_name' => $ingestSource,
|
||||
'ingest_source' => $ingestSource,
|
||||
'ip_address' => null,
|
||||
];
|
||||
|
||||
|
||||
@@ -124,6 +124,79 @@ class PhotoboothProvisioner
|
||||
});
|
||||
}
|
||||
|
||||
public function enableSparkbooth(Event $event, ?PhotoboothSetting $settings = null): Event
|
||||
{
|
||||
$settings ??= PhotoboothSetting::current();
|
||||
$event->loadMissing('tenant');
|
||||
|
||||
return DB::transaction(function () use ($event, $settings) {
|
||||
$username = $this->generateUniqueUsername($event, $settings);
|
||||
$password = $this->credentialGenerator->generatePassword();
|
||||
$path = $this->buildPath($event);
|
||||
$expiresAt = $this->resolveExpiry($event, $settings);
|
||||
|
||||
$event->forceFill([
|
||||
'photobooth_enabled' => true,
|
||||
'photobooth_mode' => 'sparkbooth',
|
||||
'sparkbooth_username' => $username,
|
||||
'sparkbooth_password' => $password,
|
||||
'sparkbooth_expires_at' => $expiresAt,
|
||||
'sparkbooth_status' => 'active',
|
||||
'photobooth_path' => $path,
|
||||
'sparkbooth_uploads_last_24h' => 0,
|
||||
])->save();
|
||||
|
||||
return tap($event->refresh(), function (Event $refreshed) use ($password) {
|
||||
$refreshed->setAttribute('plain_sparkbooth_password', $password);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function rotateSparkbooth(Event $event, ?PhotoboothSetting $settings = null): Event
|
||||
{
|
||||
$settings ??= PhotoboothSetting::current();
|
||||
|
||||
if ($event->photobooth_mode !== 'sparkbooth' || ! $event->sparkbooth_username) {
|
||||
return $this->enableSparkbooth($event, $settings);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($event, $settings) {
|
||||
$password = $this->credentialGenerator->generatePassword();
|
||||
$expiresAt = $this->resolveExpiry($event, $settings);
|
||||
|
||||
$event->forceFill([
|
||||
'sparkbooth_password' => $password,
|
||||
'sparkbooth_expires_at' => $expiresAt,
|
||||
'sparkbooth_status' => 'active',
|
||||
'photobooth_enabled' => true,
|
||||
'photobooth_mode' => 'sparkbooth',
|
||||
])->save();
|
||||
|
||||
return tap($event->refresh(), function (Event $refreshed) use ($password) {
|
||||
$refreshed->setAttribute('plain_sparkbooth_password', $password);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function disableSparkbooth(Event $event): Event
|
||||
{
|
||||
return DB::transaction(function () use ($event) {
|
||||
$event->forceFill([
|
||||
'photobooth_enabled' => false,
|
||||
'photobooth_mode' => 'ftp',
|
||||
'sparkbooth_username' => null,
|
||||
'sparkbooth_password' => null,
|
||||
'sparkbooth_expires_at' => null,
|
||||
'sparkbooth_status' => 'inactive',
|
||||
'sparkbooth_last_upload_at' => null,
|
||||
'sparkbooth_uploads_last_24h' => 0,
|
||||
'sparkbooth_uploads_total' => 0,
|
||||
])->save();
|
||||
|
||||
return $event->refresh();
|
||||
});
|
||||
}
|
||||
|
||||
protected function resolveExpiry(Event $event, PhotoboothSetting $settings): CarbonInterface
|
||||
{
|
||||
$eventEnd = $event->date ? Carbon::parse($event->date) : now();
|
||||
@@ -143,6 +216,7 @@ class PhotoboothProvisioner
|
||||
|
||||
$exists = Event::query()
|
||||
->where('photobooth_username', $username)
|
||||
->orWhere('sparkbooth_username', $username)
|
||||
->whereKeyNot($event->getKey())
|
||||
->exists();
|
||||
|
||||
|
||||
138
app/Services/Photobooth/SparkboothUploadService.php
Normal file
138
app/Services/Photobooth/SparkboothUploadService.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Photobooth;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\Photo;
|
||||
use App\Services\Photobooth\Exceptions\SparkboothUploadException;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SparkboothUploadService
|
||||
{
|
||||
public function __construct(private readonly PhotoboothIngestService $ingestService) {}
|
||||
|
||||
/**
|
||||
* @return array{event: Event, processed: int, skipped: int}
|
||||
*
|
||||
* @throws SparkboothUploadException
|
||||
*/
|
||||
public function handleUpload(UploadedFile $media, ?string $username, ?string $password): array
|
||||
{
|
||||
$event = $this->authenticate($username, $password);
|
||||
|
||||
$this->enforceExpiry($event);
|
||||
$this->enforceRateLimit($event);
|
||||
$this->assertValidFile($media);
|
||||
|
||||
$importDisk = config('photobooth.import.disk', 'photobooth');
|
||||
$basePath = ltrim((string) ($event->photobooth_path ?: $this->buildPath($event)), '/');
|
||||
$extension = strtolower($media->getClientOriginalExtension() ?: $media->extension() ?: 'jpg');
|
||||
$filename = Str::uuid().'.'.$extension;
|
||||
$relativePath = "{$basePath}/{$filename}";
|
||||
|
||||
if (! $event->photobooth_path) {
|
||||
$event->forceFill([
|
||||
'photobooth_path' => $basePath,
|
||||
])->save();
|
||||
}
|
||||
|
||||
Storage::disk($importDisk)->makeDirectory($basePath);
|
||||
Storage::disk($importDisk)->putFileAs($basePath, $media, $filename);
|
||||
|
||||
$summary = $this->ingestService->ingest($event->fresh(), 1, Photo::SOURCE_SPARKBOOTH);
|
||||
|
||||
if (($summary['processed'] ?? 0) < 1) {
|
||||
throw new SparkboothUploadException('ingest_failed', 'Upload failed, please retry.', 500);
|
||||
}
|
||||
|
||||
$event->forceFill([
|
||||
'sparkbooth_last_upload_at' => now(),
|
||||
'sparkbooth_uploads_last_24h' => ($event->sparkbooth_uploads_last_24h ?? 0) + 1,
|
||||
'sparkbooth_uploads_total' => ($event->sparkbooth_uploads_total ?? 0) + 1,
|
||||
])->save();
|
||||
|
||||
return [
|
||||
'event' => $event->fresh(),
|
||||
'processed' => $summary['processed'] ?? 0,
|
||||
'skipped' => $summary['skipped'] ?? 0,
|
||||
];
|
||||
}
|
||||
|
||||
protected function authenticate(?string $username, ?string $password): Event
|
||||
{
|
||||
if (! $username || ! $password) {
|
||||
throw new SparkboothUploadException('missing_credentials', 'Invalid credentials', 401);
|
||||
}
|
||||
|
||||
$normalizedUsername = strtolower(trim($username));
|
||||
|
||||
/** @var Event|null $event */
|
||||
$event = Event::query()
|
||||
->whereRaw('LOWER(sparkbooth_username) = ?', [$normalizedUsername])
|
||||
->first();
|
||||
|
||||
if (! $event) {
|
||||
throw new SparkboothUploadException('invalid_credentials', 'Invalid credentials', 401);
|
||||
}
|
||||
|
||||
if ($event->photobooth_mode !== 'sparkbooth' || ! $event->photobooth_enabled) {
|
||||
throw new SparkboothUploadException('disabled', 'Upload not active for this event', 403);
|
||||
}
|
||||
|
||||
if (! hash_equals($event->sparkbooth_password ?? '', $password ?? '')) {
|
||||
throw new SparkboothUploadException('invalid_credentials', 'Invalid credentials', 401);
|
||||
}
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
protected function enforceExpiry(Event $event): void
|
||||
{
|
||||
if ($event->sparkbooth_expires_at && $event->sparkbooth_expires_at->isPast()) {
|
||||
throw new SparkboothUploadException('expired', 'Upload access has expired', 403);
|
||||
}
|
||||
}
|
||||
|
||||
protected function enforceRateLimit(Event $event): void
|
||||
{
|
||||
$limit = (int) (config('photobooth.sparkbooth.rate_limit_per_minute') ?? 0);
|
||||
if ($limit <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$key = sprintf('sparkbooth:event:%d', $event->id);
|
||||
|
||||
if (RateLimiter::tooManyAttempts($key, $limit)) {
|
||||
throw new SparkboothUploadException('rate_limited', 'Upload limit reached; try again in a moment.', 429);
|
||||
}
|
||||
|
||||
RateLimiter::hit($key, 60);
|
||||
}
|
||||
|
||||
protected function assertValidFile(UploadedFile $file): void
|
||||
{
|
||||
$allowed = config('photobooth.sparkbooth.allowed_extensions', ['jpg', 'jpeg', 'png', 'webp']);
|
||||
$extension = strtolower($file->getClientOriginalExtension() ?: $file->extension() ?: '');
|
||||
|
||||
if (! $extension || ! in_array($extension, $allowed, true)) {
|
||||
throw new SparkboothUploadException('invalid_file', 'Unsupported file type', 400);
|
||||
}
|
||||
|
||||
$maxSize = (int) config('photobooth.sparkbooth.max_size_kb', 8192) * 1024;
|
||||
$size = $file->getSize() ?? 0;
|
||||
|
||||
if ($maxSize > 0 && $size > $maxSize) {
|
||||
throw new SparkboothUploadException('file_too_large', 'File too large', 400);
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildPath(Event $event): string
|
||||
{
|
||||
$tenantKey = $event->tenant?->slug ?? $event->tenant_id;
|
||||
|
||||
return trim((string) $tenantKey, '/').'/'.$event->getKey();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user