Skip to content

Commit

Permalink
Simplify thumbs (#2672)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Nov 13, 2024
1 parent bbdd883 commit fec7f90
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/AlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(Album $album)

// thumb
$this->cover_id = $album->cover_id;
$this->thumb = ThumbResource::make($album->thumb?->id, $album->thumb?->type, $album->thumb?->thumbUrl, $album->thumb?->thumb2xUrl, $album->thumb?->placeholderUrl);
$this->thumb = ThumbResource::fromModel($album->thumb);

// security
$this->policy = AlbumProtectionPolicy::ofBaseAlbum($album);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/SmartAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(BaseSmartAlbum $smartAlbum)
$this->photos = $smartAlbum->relationLoaded('photos') ? PhotoResource::collect($smartAlbum->getPhotos()) : null;
$this->prepPhotosCollection();

$this->thumb = ThumbResource::make($smartAlbum->thumb?->id, $smartAlbum->thumb?->type, $smartAlbum->thumb?->thumbUrl, $smartAlbum->thumb?->thumb2xUrl, $smartAlbum->thumb?->placeholderUrl);
$this->thumb = ThumbResource::fromModel($smartAlbum->thumb);
$this->policy = AlbumProtectionPolicy::ofSmartAlbum($smartAlbum);
$this->rights = new AlbumRightsResource($smartAlbum);
$url = $this->getHeaderUrl($smartAlbum);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/TagAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(TagAlbum $tagAlbum)
$this->prepPhotosCollection();

// thumb
$this->thumb = ThumbResource::make($tagAlbum->thumb?->id, $tagAlbum->thumb?->type, $tagAlbum->thumb?->thumbUrl, $tagAlbum->thumb?->thumb2xUrl, $tagAlbum->thumb?->placeholderUrl);
$this->thumb = ThumbResource::fromModel($tagAlbum->thumb);

// security
$this->policy = AlbumProtectionPolicy::ofBaseAlbum($tagAlbum);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/ThumbAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(AbstractAlbum $data)
$date_format = Configs::getValueAsString('date_format_album_thumb');

$this->id = $data->id;
$this->thumb = $data->thumb === null ? null : new ThumbResource($data->thumb->id, $data->thumb->type, $data->thumb->thumbUrl, $data->thumb->thumb2xUrl, $data->thumb->placeholderUrl);
$this->thumb = ThumbResource::fromModel($data->thumb);
$this->title = $data->title;

if ($data instanceof BaseSmartAlbum) {
Expand Down
21 changes: 8 additions & 13 deletions app/Http/Resources/Models/ThumbResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Resources\Models;

use App\Models\Extensions\Thumb;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

Expand All @@ -24,24 +25,18 @@ public function __construct(string $id, string $type, string $thumbUrl, ?string
}

/**
* @param string|null $id
* @param string|null $type
* @param string|null $thumbUrl
* @param string|null $thumb2xUrl
* @param string|null $placeholderUrl
* Produce a thumb resource from a Thumb object if existing.
*
* @return ($id is null ? null : ThumbResource)
* @param Thumb|null $thumb
*
* @return ThumbResource|null
*/
public static function make(?string $id, ?string $type, ?string $thumbUrl, ?string $thumb2xUrl = null, ?string $placeholderUrl = null): ?self
public static function fromModel(?Thumb $thumb): ?self
{
if ($id === null) {
if ($thumb === null) {
return null;
}

/** @var string $id */
/** @var string $type */
/** @var string $thumbUrl */
/** @var string $placeholderUrl */
return new self($id, $type, $thumbUrl, $thumb2xUrl, $placeholderUrl);
return new self($thumb->id, $thumb->type, $thumb->thumbUrl, $thumb->thumb2xUrl, $thumb->placeholderUrl);
}
}

0 comments on commit fec7f90

Please sign in to comment.