|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2025 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Actions\Photo; |
| 10 | + |
| 11 | +use App\Exceptions\Internal\LycheeLogicException; |
| 12 | +use App\Exceptions\Internal\QueryBuilderException; |
| 13 | +use App\Models\Photo; |
| 14 | +use Illuminate\Database\Query\Builder; |
| 15 | +use Illuminate\Support\Collection; |
| 16 | +use Illuminate\Support\Facades\DB; |
| 17 | + |
| 18 | +/** |
| 19 | + * Look for duplicates in the database. |
| 20 | + */ |
| 21 | +class DuplicateFinder |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Quickly count the number of duplicates candidates. |
| 25 | + * |
| 26 | + * @param bool $must_be_within_same_album Requires the duplicates to be in the same album |
| 27 | + * @param bool $must_have_same_checksum Requires the duplicates to have the same checksum |
| 28 | + * @param bool $must_have_same_title Requires the duplicates to have the same title |
| 29 | + * |
| 30 | + * @return int |
| 31 | + */ |
| 32 | + public function checkCount( |
| 33 | + bool $must_be_within_same_album, |
| 34 | + bool $must_have_same_checksum, |
| 35 | + bool $must_have_same_title, |
| 36 | + ): int { |
| 37 | + return $this->query($must_be_within_same_album, $must_have_same_checksum, $must_have_same_title) |
| 38 | + ->count(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Return the list of duplicates candidate. |
| 43 | + * |
| 44 | + * @param bool $must_be_within_same_album Requires the duplicates to be in the same album |
| 45 | + * @param bool $must_have_same_checksum Requires the duplicates to have the same checksum |
| 46 | + * @param bool $must_have_same_title Requires the duplicates to have the same title |
| 47 | + * |
| 48 | + * @return Collection<int,object{album_id:string,album_title:string,photo_id:string,photo_title:string,checksum:string,short_path:string|null,storage_disk:string|null}> |
| 49 | + */ |
| 50 | + public function search( |
| 51 | + bool $must_be_within_same_album, |
| 52 | + bool $must_have_same_checksum, |
| 53 | + bool $must_have_same_title, |
| 54 | + ): Collection { |
| 55 | + /** @var Collection<int,object{album_id:string,album_title:string,photo_id:string,photo_title:string,checksum:string,short_path:string|null,storage_disk:string|null}> */ |
| 56 | + return $this->query($must_be_within_same_album, $must_have_same_checksum, $must_have_same_title) |
| 57 | + ->get(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param bool $must_be_within_same_album Requires the duplicates to be in the same album |
| 62 | + * @param bool $must_have_same_checksum Requires the duplicates to have the same checksum |
| 63 | + * @param bool $must_have_same_title Requires the duplicates to have the same title |
| 64 | + * |
| 65 | + * @return Builder |
| 66 | + * |
| 67 | + * @throws LycheeLogicException |
| 68 | + * @throws QueryBuilderException |
| 69 | + */ |
| 70 | + private function query( |
| 71 | + bool $must_be_within_same_album, |
| 72 | + bool $must_have_same_checksum, |
| 73 | + bool $must_have_same_title, |
| 74 | + ): Builder { |
| 75 | + if (!$must_be_within_same_album && !$must_have_same_checksum && !$must_have_same_title) { |
| 76 | + throw new LycheeLogicException('At least one constraint must be enabled.'); |
| 77 | + } |
| 78 | + |
| 79 | + return Photo::query() |
| 80 | + ->join('base_albums', 'base_albums.id', '=', 'photos.album_id') |
| 81 | + ->join( |
| 82 | + 'size_variants', 'size_variants.photo_id', '=', 'photos.id', 'left' |
| 83 | + ) |
| 84 | + ->whereIn('photos.id', $this->getDuplicatesIdsQuery($must_be_within_same_album, $must_have_same_checksum, $must_have_same_title)) |
| 85 | + ->where('size_variants.type', '=', 4) |
| 86 | + ->select([ |
| 87 | + 'base_albums.id as album_id', |
| 88 | + 'base_albums.title as album_title', |
| 89 | + 'photos.id as photo_id', |
| 90 | + 'photos.title as photo_title', |
| 91 | + 'photos.created_at as photo_created_at', |
| 92 | + 'photos.checksum', |
| 93 | + 'size_variants.short_path as short_path', |
| 94 | + 'size_variants.storage_disk as storage_disk', |
| 95 | + ]) |
| 96 | + ->when($must_have_same_checksum, fn ($q) => $q->orderBy('photos.checksum', 'asc')) |
| 97 | + ->when(!$must_have_same_checksum, fn ($q) => $q->orderBy('photos.title', 'asc')) |
| 98 | + ->toBase(); |
| 99 | + } |
| 100 | + |
| 101 | + private function getDuplicatesIdsQuery( |
| 102 | + bool $must_be_within_same_album, |
| 103 | + bool $must_have_same_checksum, |
| 104 | + bool $must_have_same_title, |
| 105 | + ): Builder { |
| 106 | + return DB::table('photos', 'p1')->select('p1.id') |
| 107 | + ->join( |
| 108 | + 'photos as p2', |
| 109 | + fn ($join) => $join->on('p1.id', '<>', 'p2.id') |
| 110 | + ->when($must_have_same_title, fn ($q) => $q->on('p1.title', '=', 'p2.title')) |
| 111 | + ->when($must_have_same_checksum, fn ($q) => $q->on('p1.checksum', '=', 'p2.checksum')) |
| 112 | + ->when($must_be_within_same_album, fn ($q) => $q->on('p1.album_id', '=', 'p2.album_id')) |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
0 commit comments