Skip to content

Commit

Permalink
fixup! Don't allow duplicate photos from users
Browse files Browse the repository at this point in the history
  • Loading branch information
GeniJaho committed Jan 20, 2024
1 parent df33f23 commit 6db3e46
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/Console/Commands/FillOriginalFileNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Console\Commands;

use App\Models\Photo;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class FillOriginalFileNames extends Command
{
protected $signature = 'app:fill-original-file-names';

public function handle(): void
{
Photo::query()
->whereNull('original_file_name')
->update([
'original_file_name' => DB::raw('replace(path, "photos/", "")'),
]);
}
}
5 changes: 4 additions & 1 deletion app/Http/Requests/StorePhotosRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public function rules(): array
];
}

/**
* @return array<int, callable>
*/
public function after(): array
{
return [
function (Validator $validator) {
$photoExists = auth()->user()
->photos()
?->photos()
->where('original_file_name', $this->photo->getClientOriginalName())
->exists();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use App\Console\Commands\FillOriginalFileNames;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
Expand All @@ -10,6 +12,10 @@ public function up(): void
{
Schema::table('photos', function (Blueprint $table) {
$table->string('original_file_name')->after('path');

$table->unique(['user_id', 'original_file_name']);
});

Artisan::call(FillOriginalFileNames::class);
}
};
1 change: 1 addition & 0 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 6db3e46

Please sign in to comment.