feat: Implement public event and photo API

This commit is contained in:
2025-09-09 21:22:20 +02:00
parent 8162af842f
commit 7743d91df6
2 changed files with 44 additions and 1 deletions

View File

@@ -13,6 +13,30 @@ use App\Support\ImageHelper;
class EventPublicController extends BaseController
{
private function toPublicUrl(?string $path): ?string
{
if (! $path) return null;
// Already absolute URL
if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) return $path;
// Already a public storage URL
if (str_starts_with($path, '/storage/')) return $path;
if (str_starts_with($path, 'storage/')) return '/' . $path;
// Common relative paths stored in DB (e.g. 'photos/...', 'thumbnails/...', 'events/...')
if (str_starts_with($path, 'photos/') || str_starts_with($path, 'thumbnails/') || str_starts_with($path, 'events/')) {
return Storage::url($path);
}
// Absolute server paths pointing into storage/app/public (Linux/Windows)
$normalized = str_replace('\\', '/', $path);
$needle = '/storage/app/public/';
if (str_contains($normalized, $needle)) {
$rel = substr($normalized, strpos($normalized, $needle) + strlen($needle));
return '/storage/' . ltrim($rel, '/');
}
return $path; // fallback as-is
}
public function event(string $slug)
{
$event = DB::table('events')->where('slug', $slug)->first([
@@ -90,7 +114,11 @@ class EventPublicController extends BaseController
$query->where('created_at', '>', $since);
}
$rows = $query->get();
$rows = $query->get()->map(function ($r) {
$r->file_path = $this->toPublicUrl((string)($r->file_path ?? ''));
$r->thumbnail_path = $this->toPublicUrl((string)($r->thumbnail_path ?? ''));
return $r;
});
$latestPhotoAt = DB::table('photos')->where('event_id', $eventId)->max('created_at');
$payload = [
'data' => $rows,
@@ -116,6 +144,8 @@ class EventPublicController extends BaseController
if (! $row) {
return response()->json(['error' => ['code' => 'not_found', 'message' => 'Photo not found']], 404);
}
$row->file_path = $this->toPublicUrl((string)($row->file_path ?? ''));
$row->thumbnail_path = $this->toPublicUrl((string)($row->thumbnail_path ?? ''));
return response()->json($row)->header('Cache-Control', 'no-store');
}