Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Actions/Album/CreateTagAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use App\Exceptions\ModelDBException;
use App\Exceptions\UnauthenticatedException;
use App\Models\Tag;
use App\Models\TagAlbum;
use Illuminate\Support\Facades\Auth;

Expand All @@ -33,7 +34,7 @@ public function create(string $title, array $show_tags): TagAlbum

$album = new TagAlbum();
$album->title = $title;
$album->show_tags = $show_tags;
$album->show_tags = Tag::from($show_tags)->all();
$album->owner_id = $user_id;
$album->save();
$this->setStatistics($album);
Expand Down
1 change: 1 addition & 0 deletions app/Actions/Album/PositionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function get(AbstractAlbum $album, bool $include_sub_albums = false): Pos
$r->whereBetween('type', [SizeVariantType::SMALL2X, SizeVariantType::THUMB]);
},
'palette',
'tags',
])
->whereNotNull('latitude')
->whereNotNull('longitude');
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Albums/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function getQuery(Album|null $base, bool $with_relations): AlbumBuilder

$base_query = Album::query();
if ($with_relations) {
$base_query->with(['cover', 'cover.size_variants', 'statistics', 'photos', 'photos.statistics', 'photos.size_variants', 'photos.palette']);
$base_query->with(['cover', 'cover.size_variants', 'statistics', 'photos', 'photos.statistics', 'photos.size_variants', 'photos.palette', 'photos.tags']);
}

// Only join what we need for ordering.
Expand Down
1 change: 1 addition & 0 deletions app/Actions/Albums/PositionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function do(): PositionDataResource
$r->whereBetween('type', [SizeVariantType::SMALL2X, SizeVariantType::THUMB]);
},
'palette',
'tags',
])
->whereNotNull('latitude')
->whereNotNull('longitude'),
Expand Down
3 changes: 2 additions & 1 deletion app/Actions/Photo/Pipes/Shared/HydrateMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Contracts\PhotoCreate\SharedPipe;
use App\DTO\PhotoCreate\DuplicateDTO;
use App\DTO\PhotoCreate\StandaloneDTO;
use App\Models\Tag;

/**
* Hydrates meta-info of the media file from the
Expand Down Expand Up @@ -38,7 +39,7 @@ public function handle(DuplicateDTO|StandaloneDTO $state, \Closure $next): Dupli
$state->photo->description = $state->exif_info->description;
}
if (count($state->photo->tags) === 0) {
$state->photo->tags = $state->exif_info->tags;
$state->tags = Tag::from($state->exif_info->tags);
}
if ($state->photo->type === null) {
$state->photo->type = $state->exif_info->type;
Expand Down
1 change: 1 addition & 0 deletions app/Actions/Photo/Pipes/Shared/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Save implements PhotoPipe
public function handle(PhotoDTO $state, \Closure $next): PhotoDTO
{
$state->getPhoto()->save();
$state->getPhoto()->tags()->sync($state->getTags()->pluck('id')->all());

return $next($state);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Search/PhotoSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function query(array $terms): Collection
public function sqlQuery(array $terms, ?Album $album = null): Builder
{
$query = $this->photoQueryPolicy->applySearchabilityFilter(
query: Photo::query()->with(['albums', 'statistics', 'size_variants', 'palette']),
query: Photo::query()->with(['albums', 'statistics', 'size_variants', 'palette', 'tags']),
origin: $album,
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_search')
);
Expand All @@ -67,7 +67,7 @@ public function sqlQuery(array $terms, ?Album $album = null): Builder
fn (FixedQueryBuilder $query) => $query
->where('title', 'like', '%' . $term . '%')
->orWhere('description', 'like', '%' . $term . '%')
->orWhere('tags', 'like', '%' . $term . '%')
// ->orWhere('tags', 'like', '%' . $term . '%')
->orWhere('location', 'like', '%' . $term . '%')
->orWhere('model', 'like', '%' . $term . '%')
->orWhere('taken_at', 'like', '%' . $term . '%')
Expand Down
27 changes: 16 additions & 11 deletions app/Casts/ArrayCast.php → app/Casts/TagArrayCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,53 @@

namespace App\Casts;

use App\Models\Tag;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

/**
* @implements CastsAttributes<array,(string|null)[]>
* @implements CastsAttributes<array,(Tag|null)[]>
*/
class ArrayCast implements CastsAttributes
class TagArrayCast implements CastsAttributes
{
/**
* @param Model $model the associated model class
* @param string $key the name of the SQL column holding the stringified array
* @param mixed $value the stringified array
* @param array<string,mixed> $attributes all SQL attributes of the entity
*
* @return array<int,string> the array
* @return array<int,Tag> the array
*/
public function get(Model $model, string $key, mixed $value, array $attributes): array
{
return ($value === null || $value === '') ? [] : explode(',', strval($value));
if ($value === null || $value === '') {
return [];
}

// Split the string by ' OR ' and return an array of Tag objects
return Tag::whereIn('id', explode(' OR ', strval($value)))->get()->all();
}

/**
* @param Model $model the associated model class
* @param string $key the name of the SQL column holding the stringified array
* @param (string|null)[]|null $value the array
* @param array<string,mixed> $attributes
* @param Model $model the associated model class
* @param string $key the name of the SQL column holding the stringified array
* @param (Tag|null)[]|null $value the array
* @param array<string,mixed> $attributes
*
* @return array<string,mixed> An associative map of SQL columns and their values
*/
public function set(Model $model, string $key, mixed $value, array $attributes): array
{
// Normalize the input value
// The array must not contain empty tags and tags which contain a comma
// TODO: Either use a separate table to store the tags or another encoding (e.g. JSON) which also allows commas in tags

$arr = !is_array($value) ? [] : array_values(array_filter(
$value,
fn ($elem) => ($elem !== null && $elem !== '' && !str_contains($elem, ',')),
fn ($elem) => ($elem !== null && $elem !== '' && $elem instanceof Tag && $elem->name !== ''),
));

return [
$key => count($arr) === 0 ? null : implode(',', $arr),
$key => count($arr) === 0 ? null : implode(' OR ', array_map(fn ($t) => $t->id, $arr)),
];
}
}
7 changes: 7 additions & 0 deletions app/Contracts/PhotoCreate/PhotoDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
namespace App\Contracts\PhotoCreate;

use App\Models\Photo;
use App\Models\Tag;
use Illuminate\Support\Collection;

interface PhotoDTO
{
public function getPhoto(): Photo;

/**
* @return Collection<int,Tag>
*/
public function getTags(): Collection;
}
9 changes: 9 additions & 0 deletions app/DTO/PhotoCreate/DuplicateDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Contracts\PhotoCreate\PhotoDTO;
use App\Metadata\Extractor;
use App\Models\Photo;
use Illuminate\Support\Collection;

/**
* DTO used when dealing with duplicates.
Expand All @@ -21,6 +22,8 @@ class DuplicateDTO implements PhotoDTO
{
public bool $has_been_resynced;

public Collection $tags;

public function __construct(
public readonly bool $shall_resync_metadata,
public readonly bool $shall_skip_duplicates,
Expand All @@ -39,6 +42,7 @@ public function __construct(
// During initial steps if duplicate is found, it will be placed here.
public Photo $photo,
) {
$this->tags = $this->photo->tags;
}

public static function ofInit(InitDTO $init_dto): DuplicateDTO
Expand All @@ -59,6 +63,11 @@ public function getPhoto(): Photo
return $this->photo;
}

public function getTags(): Collection
{
return $this->tags;
}

public function setHasBeenResync(bool $val): void
{
$this->has_been_resynced = $val;
Expand Down
6 changes: 6 additions & 0 deletions app/DTO/PhotoCreate/PhotoPartnerDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Contracts\Image\StreamStats;
use App\Contracts\PhotoCreate\PhotoDTO;
use App\Models\Photo;
use Illuminate\Support\Collection;

class PhotoPartnerDTO implements PhotoDTO
{
Expand All @@ -27,4 +28,9 @@ public function getPhoto(): Photo
{
return $this->photo;
}

public function getTags(): Collection
{
return $this->photo->tags;
}
}
8 changes: 8 additions & 0 deletions app/DTO/PhotoCreate/StandaloneDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Image\Files\TemporaryLocalFile;
use App\Metadata\Extractor;
use App\Models\Photo;
use Illuminate\Support\Collection;

class StandaloneDTO implements PhotoDTO
{
Expand All @@ -27,6 +28,7 @@ class StandaloneDTO implements PhotoDTO
public FlysystemFile $target_file;
public StreamStats|null $stream_stat;
public FlysystemFile|null $backup_file = null;
public Collection $tags;

public function __construct(
// The resulting photo
Expand All @@ -44,6 +46,7 @@ public function __construct(
public readonly bool $shall_import_via_symlink,
public readonly bool $shall_delete_imported,
) {
$this->tags = new Collection();
}

public static function ofInit(InitDTO $init_dto): StandaloneDTO
Expand All @@ -64,4 +67,9 @@ public function getPhoto(): Photo
{
return $this->photo;
}

public function getTags(): Collection
{
return $this->tags;
}
}
6 changes: 6 additions & 0 deletions app/DTO/PhotoCreate/VideoPartnerDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Contracts\PhotoCreate\PhotoDTO;
use App\Image\Files\BaseMediaFile;
use App\Models\Photo;
use Illuminate\Support\Collection;

class VideoPartnerDTO implements PhotoDTO
{
Expand All @@ -32,6 +33,11 @@ public function getPhoto(): Photo
return $this->photo;
}

public function getTags(): Collection
{
return $this->photo->tags;
}

public static function ofInit(InitDTO $init_dto): VideoPartnerDTO
{
return new VideoPartnerDTO(
Expand Down
4 changes: 2 additions & 2 deletions app/Factories/AlbumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public function findBaseAlbumOrFail(string $album_id, bool $with_relations = tru
$tag_album_query = TagAlbum::query();

if ($with_relations) {
$album_query->with(['access_permissions', 'photos', 'children', 'children.owner', 'photos.size_variants', 'photos.statistics', 'photos.palette']);
$tag_album_query->with(['photos', 'photos.size_variants', 'photos.statistics', 'photos.palette']);
$album_query->with(['access_permissions', 'photos', 'children', 'children.owner', 'photos.size_variants', 'photos.statistics', 'photos.palette', 'photos.tags']);
$tag_album_query->with(['photos', 'photos.size_variants', 'photos.statistics', 'photos.palette', 'photos.tags']);
}

$ret = $album_query->find($album_id) ?? $tag_album_query->find($album_id);
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use App\Http\Resources\Models\Utils\AlbumProtectionPolicy;
use App\Models\Album;
use App\Models\Extensions\BaseAlbum;
use App\Models\Tag;
use App\Models\TagAlbum;
use App\SmartAlbums\BaseSmartAlbum;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -165,7 +166,7 @@ public function updateTagAlbum(UpdateTagAlbumRequest $request): EditableBaseAlbu
}
$album->title = $request->title();
$album->description = $request->description();
$album->show_tags = $request->tags();
$album->show_tags = Tag::from($request->tags())->all();
$album->copyright = $request->copyright();
$album->photo_sorting = $request->photoSortingCriterion();
$album->photo_layout = $request->photoLayout();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Gallery/FrameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ private function loadPhoto(AbstractAlbum|null $album, int $retries = 5): ?Photo
// default query
if ($album === null) {
$query = $this->photo_query_policy->applySearchabilityFilter(
query: Photo::query()->with(['albums', 'size_variants', 'palette']),
query: Photo::query()->with(['albums', 'size_variants', 'palette', 'tags']),
origin: null,
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_frame')
);
} else {
$query = $album->photos()->with(['albums', 'size_variants', 'palette']);
$query = $album->photos()->with(['albums', 'size_variants', 'palette', 'tags']);
}

/** @var ?Photo $photo */
Expand Down
29 changes: 20 additions & 9 deletions app/Http/Controllers/Gallery/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
use App\Jobs\ProcessImageJob;
use App\Models\Configs;
use App\Models\Photo;
use App\Models\Tag;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

/**
Expand Down Expand Up @@ -122,7 +124,10 @@ public function update(EditPhotoRequest $request): PhotoResource
$photo->title = $request->title();
$photo->description = $request->description();
$photo->created_at = $request->uploadDate();
$photo->tags = $request->tags();

$existing_tags = Tag::from($request->tags());
$photo->tags()->sync($existing_tags->pluck('id'));
$photo->load('tags');
$photo->license = $request->license()->value;

// if the request takenAt is null, then we set the initial value back.
Expand Down Expand Up @@ -205,15 +210,21 @@ public function rename(RenamePhotoRequest $request): void
public function tags(SetPhotosTagsRequest $request): void
{
$tags = $request->tags();
$photos = $request->photos();

/** @var Photo $photo */
foreach ($request->photos() as $photo) {
if ($request->shall_override) {
$photo->tags = $tags;
} else {
$photo->tags = array_unique(array_merge($photo->tags, $tags));
}
$photo->save();
// Fetch existing tags
$existing_tags = Tag::from($tags);

if ($request->shall_override) {
// Delete existing associations for those photos ids if we override the tags
DB::table('photos_tags')
->whereIn('photo_id', $photos->pluck('id'))
->delete();
}

// Associate the existing tags with the photos
$existing_tags->each(function (Tag $tag) use ($photos): void {
$tag->photos()->syncWithoutDetaching($photos->pluck('id'));
});
}
}
1 change: 1 addition & 0 deletions app/Http/Requests/Album/AddTagAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasTagsTrait;
use App\Http\Requests\Traits\HasTitleTrait;
use App\Models\Tag;
use App\Policies\AlbumPolicy;
use App\Rules\TitleRule;
use Illuminate\Support\Facades\Gate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct(Album|TagAlbum $album)
}

if ($album instanceof TagAlbum) {
$this->tags = $album->show_tags;
$this->tags = array_map(fn ($t) => $t->name, $album->show_tags);
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/PhotoResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function __construct(Photo $photo, ?AbstractAlbum $album)
$this->original_checksum = $photo->original_checksum;
$this->shutter = $photo->shutter;
$this->size_variants = new SizeVariantsResouce($photo, $album);
$this->tags = $photo->tags;
$this->tags = $photo->tags->pluck('name')->all();
$this->taken_at = $photo->taken_at?->toIso8601String();
$this->taken_at_orig_tz = $photo->taken_at_orig_tz;
$this->title = (Configs::getValueAsBool('file_name_hidden') && Auth::guest()) ? '' : $photo->title;
Expand Down
Loading