68 lines
2.2 KiB
PHP
68 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\Tenant;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class PhotoResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
$tenantId = $request->attributes->get('tenant_id');
|
|
$showSensitive = $this->event->tenant_id === $tenantId;
|
|
$fullUrl = $this->getSignedUrl('full');
|
|
$thumbnailUrl = $this->getSignedUrl('thumbnail');
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'filename' => $this->filename,
|
|
'original_name' => $this->original_name,
|
|
'mime_type' => $this->mime_type,
|
|
'size' => (int) ($this->size ?? 0),
|
|
'url' => $showSensitive ? ($fullUrl ?? $thumbnailUrl) : ($thumbnailUrl ?? $fullUrl),
|
|
'thumbnail_url' => $thumbnailUrl ?? $fullUrl,
|
|
'width' => $this->width,
|
|
'height' => $this->height,
|
|
'is_featured' => (bool) ($this->is_featured ?? false),
|
|
'status' => $showSensitive ? $this->status : 'approved',
|
|
'moderation_notes' => $showSensitive ? $this->moderation_notes : null,
|
|
'likes_count' => (int) ($this->likes_count ?? $this->likes()->count()),
|
|
'is_liked' => false,
|
|
'uploaded_at' => $this->created_at->toISOString(),
|
|
'uploader_name' => $this->guest_name ?? null,
|
|
'ingest_source' => $this->ingest_source,
|
|
'event' => [
|
|
'id' => $this->event->id,
|
|
'name' => $this->event->name,
|
|
'slug' => $this->event->slug,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get signed URL for variant
|
|
*/
|
|
private function getSignedUrl(string $variant): ?string
|
|
{
|
|
if (empty($this->id) || empty($this->event?->slug)) {
|
|
return null;
|
|
}
|
|
|
|
return \URL::temporarySignedRoute(
|
|
'api.v1.tenant.events.photos.asset',
|
|
now()->addMinutes(30),
|
|
[
|
|
'event' => $this->event->slug,
|
|
'photo' => $this->id,
|
|
'variant' => $variant === 'thumbnail' ? 'thumbnail' : 'full',
|
|
]
|
|
);
|
|
}
|
|
}
|