diff --git a/app/Console/Commands/FillOriginalFileNames.php b/app/Console/Commands/FillOriginalFileNames.php new file mode 100644 index 00000000..b3c7d73c --- /dev/null +++ b/app/Console/Commands/FillOriginalFileNames.php @@ -0,0 +1,21 @@ +whereNull('original_file_name') + ->update([ + 'original_file_name' => DB::raw('replace(path, "photos/", "")'), + ]); + } +} diff --git a/app/Http/Requests/StorePhotosRequest.php b/app/Http/Requests/StorePhotosRequest.php index ad213df2..0cf700e7 100644 --- a/app/Http/Requests/StorePhotosRequest.php +++ b/app/Http/Requests/StorePhotosRequest.php @@ -26,12 +26,15 @@ public function rules(): array ]; } + /** + * @return array + */ public function after(): array { return [ function (Validator $validator) { $photoExists = auth()->user() - ->photos() + ?->photos() ->where('original_file_name', $this->photo->getClientOriginalName()) ->exists(); diff --git a/database/migrations/2024_01_19_123230_add_original_file_name_to_photos.php b/database/migrations/2024_01_19_123230_add_original_file_name_to_photos.php index 08947ce5..c211efe4 100644 --- a/database/migrations/2024_01_19_123230_add_original_file_name_to_photos.php +++ b/database/migrations/2024_01_19_123230_add_original_file_name_to_photos.php @@ -1,7 +1,9 @@ string('original_file_name')->after('path'); + + $table->unique(['user_id', 'original_file_name']); }); + + Artisan::call(FillOriginalFileNames::class); } }; diff --git a/database/seeders/UserSeeder.php b/database/seeders/UserSeeder.php index cd7ec0ae..eb8f36d2 100644 --- a/database/seeders/UserSeeder.php +++ b/database/seeders/UserSeeder.php @@ -47,6 +47,7 @@ public function run(): void ])); $user->photos()->create([ 'path' => 'photos/default.jpg', + 'original_file_name' => 'default.jpg', ]); File::copy( storage_path('app/default.jpg'),