fixed gallery
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Services\ThumbnailPresenter;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Http\Resources\MissingValue;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class ArtworkResource extends JsonResource
|
||||
{
|
||||
@@ -11,68 +13,105 @@ class ArtworkResource extends JsonResource
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
if ($this instanceof MissingValue || $this->resource instanceof MissingValue) {
|
||||
return [];
|
||||
$md = ThumbnailPresenter::present($this->resource, 'md');
|
||||
$lg = ThumbnailPresenter::present($this->resource, 'lg');
|
||||
$xl = ThumbnailPresenter::present($this->resource, 'xl');
|
||||
$sq = ThumbnailPresenter::present($this->resource, 'sq');
|
||||
|
||||
$canonicalSlug = \Illuminate\Support\Str::slug((string) ($this->slug ?: $this->title));
|
||||
if ($canonicalSlug === '') {
|
||||
$canonicalSlug = (string) $this->id;
|
||||
}
|
||||
$get = function ($key) {
|
||||
$r = $this->resource;
|
||||
if ($r instanceof MissingValue || $r === null) {
|
||||
return null;
|
||||
}
|
||||
// Eloquent model: prefer getAttribute to avoid magic proxies
|
||||
if (method_exists($r, 'getAttribute')) {
|
||||
return $r->getAttribute($key);
|
||||
}
|
||||
if (is_array($r)) {
|
||||
return $r[$key] ?? null;
|
||||
}
|
||||
if (is_object($r)) {
|
||||
return $r->{$key} ?? null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
$hash = (string) ($get('hash') ?? '');
|
||||
$fileExt = (string) ($get('file_ext') ?? '');
|
||||
$filesBase = rtrim((string) config('cdn.files_url', ''), '/');
|
||||
|
||||
$buildOriginalUrl = static function (string $hashValue, string $extValue) use ($filesBase): ?string {
|
||||
$normalizedHash = strtolower((string) preg_replace('/[^a-f0-9]/', '', $hashValue));
|
||||
$normalizedExt = strtolower((string) preg_replace('/[^a-z0-9]/', '', $extValue));
|
||||
if ($normalizedHash === '' || $normalizedExt === '') return null;
|
||||
$h1 = substr($normalizedHash, 0, 2);
|
||||
$h2 = substr($normalizedHash, 2, 2);
|
||||
if ($h1 === '' || $h2 === '' || $filesBase === '') return null;
|
||||
$followerCount = (int) ($this->user?->profile?->followers_count ?? 0);
|
||||
if (($followerCount <= 0) && Schema::hasTable('friends_list') && !empty($this->user?->id)) {
|
||||
$followerCount = (int) DB::table('friends_list')
|
||||
->where('friend_id', (int) $this->user->id)
|
||||
->count();
|
||||
}
|
||||
|
||||
return sprintf('%s/originals/%s/%s/%s.%s', $filesBase, $h1, $h2, $normalizedHash, $normalizedExt);
|
||||
};
|
||||
$viewerId = (int) optional($request->user())->id;
|
||||
$isLiked = false;
|
||||
$isFavorited = false;
|
||||
$isFollowing = false;
|
||||
|
||||
if ($viewerId > 0) {
|
||||
if (Schema::hasTable('artwork_likes')) {
|
||||
$isLiked = DB::table('artwork_likes')
|
||||
->where('user_id', $viewerId)
|
||||
->where('artwork_id', (int) $this->id)
|
||||
->exists();
|
||||
}
|
||||
|
||||
if (Schema::hasTable('user_favorites')) {
|
||||
$isFavorited = DB::table('user_favorites')
|
||||
->where('user_id', $viewerId)
|
||||
->where('artwork_id', (int) $this->id)
|
||||
->exists();
|
||||
}
|
||||
|
||||
if (Schema::hasTable('friends_list') && !empty($this->user?->id)) {
|
||||
$isFollowing = DB::table('friends_list')
|
||||
->where('user_id', $viewerId)
|
||||
->where('friend_id', (int) $this->user->id)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'slug' => $get('slug'),
|
||||
'title' => $get('title'),
|
||||
'description' => $get('description'),
|
||||
'width' => $get('width'),
|
||||
'height' => $get('height'),
|
||||
|
||||
// File URLs are derived from hash/ext (no DB path dependency)
|
||||
'id' => (int) $this->id,
|
||||
'slug' => (string) $this->slug,
|
||||
'title' => (string) $this->title,
|
||||
'description' => (string) ($this->description ?? ''),
|
||||
'dimensions' => [
|
||||
'width' => (int) ($this->width ?? 0),
|
||||
'height' => (int) ($this->height ?? 0),
|
||||
],
|
||||
'published_at' => optional($this->published_at)->toIsoString(),
|
||||
'canonical_url' => route('art.show', ['id' => (int) $this->id, 'slug' => $canonicalSlug]),
|
||||
'thumbs' => [
|
||||
'md' => $md,
|
||||
'lg' => $lg,
|
||||
'xl' => $xl,
|
||||
'sq' => $sq,
|
||||
],
|
||||
'file' => [
|
||||
'name' => $get('file_name') ?? null,
|
||||
'url' => $this->when(! empty($hash) && ! empty($fileExt), fn() => $buildOriginalUrl($hash, $fileExt)),
|
||||
'size' => $get('file_size') ?? null,
|
||||
'mime_type' => $get('mime_type') ?? null,
|
||||
'url' => $lg['url'] ?? null,
|
||||
'srcset' => ThumbnailPresenter::srcsetForArtwork($this->resource),
|
||||
'mime_type' => 'image/webp',
|
||||
],
|
||||
|
||||
'categories' => $this->whenLoaded('categories', function () {
|
||||
return $this->categories->map(fn($c) => [
|
||||
'slug' => $c->slug ?? null,
|
||||
'name' => $c->name ?? null,
|
||||
])->values();
|
||||
}),
|
||||
|
||||
'published_at' => $this->whenNotNull($get('published_at') ? $this->published_at->toAtomString() : null),
|
||||
|
||||
'urls' => [
|
||||
'canonical' => $get('canonical_url') ?? null,
|
||||
'user' => [
|
||||
'id' => (int) ($this->user?->id ?? 0),
|
||||
'name' => (string) ($this->user?->name ?? ''),
|
||||
'username' => (string) ($this->user?->username ?? ''),
|
||||
'profile_url' => $this->user?->username ? '/@' . $this->user->username : null,
|
||||
'avatar_url' => $this->user?->profile?->avatar_url,
|
||||
'followers_count' => $followerCount,
|
||||
],
|
||||
'viewer' => [
|
||||
'is_liked' => $isLiked,
|
||||
'is_favorited' => $isFavorited,
|
||||
'is_following_author' => $isFollowing,
|
||||
'is_authenticated' => $viewerId > 0,
|
||||
'id' => $viewerId > 0 ? $viewerId : null,
|
||||
],
|
||||
'stats' => [
|
||||
'views' => (int) ($this->stats?->views ?? 0),
|
||||
'downloads' => (int) ($this->stats?->downloads ?? 0),
|
||||
'favorites' => (int) ($this->stats?->favorites ?? 0),
|
||||
'likes' => (int) ($this->stats?->rating_count ?? 0),
|
||||
],
|
||||
'categories' => $this->categories->map(fn ($category) => [
|
||||
'id' => (int) $category->id,
|
||||
'slug' => (string) $category->slug,
|
||||
'name' => (string) $category->name,
|
||||
'content_type_slug' => (string) ($category->contentType?->slug ?? ''),
|
||||
])->values(),
|
||||
'tags' => $this->tags->map(fn ($tag) => [
|
||||
'id' => (int) $tag->id,
|
||||
'slug' => (string) $tag->slug,
|
||||
'name' => (string) $tag->name,
|
||||
])->values(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user