diff --git a/Modules/Media/Repositories/Eloquent/EloquentFileRepository.php b/Modules/Media/Repositories/Eloquent/EloquentFileRepository.php index 33b1eeaa1..c90a7a10d 100644 --- a/Modules/Media/Repositories/Eloquent/EloquentFileRepository.php +++ b/Modules/Media/Repositories/Eloquent/EloquentFileRepository.php @@ -46,10 +46,10 @@ public function createFromFile(UploadedFile $file, int $parentId = 0) { $fileName = FileHelper::slug($file->getClientOriginalName()); - $exists = $this->model->whereFilename($fileName)->first(); + $exists = $this->model->where('filename', $fileName)->where('folder_id', $parentId)->first(); if ($exists) { - $fileName = $this->getNewUniqueFilename($fileName, $parentId); + $fileName = $this->getNewUniqueFilename($fileName); } $data = [ @@ -124,15 +124,14 @@ public function findMultipleFilesByZoneForEntity($zone, $entity) /** * @param $fileName - * @param int $parentId * @return string */ - private function getNewUniqueFilename($fileName, int $parentId = 0) + private function getNewUniqueFilename($fileName) { $fileNameOnly = pathinfo($fileName, PATHINFO_FILENAME); $extension = pathinfo($fileName, PATHINFO_EXTENSION); - $models = $this->model->where('filename', 'LIKE', "$fileNameOnly%")->where('folder_id', $parentId)->get(); + $models = $this->model->where('filename', 'LIKE', "$fileNameOnly%")->get(); $versionCurrent = $models->reduce(function ($carry, $model) { $latestFilename = pathinfo($model->filename, PATHINFO_FILENAME); @@ -145,9 +144,6 @@ private function getNewUniqueFilename($fileName, int $parentId = 0) return ($version > $carry) ? $version : $carry; }, 0); - if ($versionCurrent === 0 && $models->count() === 0) { - return $fileName; - } return $fileNameOnly . '_' . ($versionCurrent+1) . '.' . $extension; } diff --git a/Modules/Media/Tests/EloquentFileRepositoryTest.php b/Modules/Media/Tests/EloquentFileRepositoryTest.php index b6aefd637..a37a56c52 100644 --- a/Modules/Media/Tests/EloquentFileRepositoryTest.php +++ b/Modules/Media/Tests/EloquentFileRepositoryTest.php @@ -11,6 +11,7 @@ use Modules\Media\Events\FileWasUpdated; use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FolderRepository; +use Modules\Media\Services\FileService; use Symfony\Component\Finder\SplFileInfo; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -256,6 +257,26 @@ public function it_can_fetch_all_files_only() $this->assertCount(2, $this->file->allForGrid()); } + /** @test */ + public function it_can_store_same_filename_in_other_folder_with_no_suffix() + { + $folderRepository = app(FolderRepository::class); + $folder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]); + $file = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $folder->id); + $fileTwo = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg')); + + $subFolder = $folderRepository->create(['name' => 'My Sub Folder', 'parent_id' => $folder->id]); + $fileThree = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $subFolder->id); + + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.jpg'))); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/my-file.jpg'))); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/my-sub-folder/my-file.jpg'))); + + $this->assertEquals('/assets/media/my-folder/my-file.jpg', $file->path->getRelativeUrl()); + $this->assertEquals('/assets/media/my-file.jpg', $fileTwo->path->getRelativeUrl()); + $this->assertEquals('/assets/media/my-folder/my-sub-folder/my-file.jpg', $fileThree->path->getRelativeUrl()); + } + private function createFile($fileName = 'random/name.jpg') { return File::create([ diff --git a/Modules/Media/Tests/FileMoverTest.php b/Modules/Media/Tests/FileMoverTest.php index 3c3fce07b..acb271f63 100644 --- a/Modules/Media/Tests/FileMoverTest.php +++ b/Modules/Media/Tests/FileMoverTest.php @@ -123,26 +123,6 @@ public function it_can_move_file_with_thumbnails_back_to_root_folder() $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file_mediumThumb.jpg'))); } - /** @test */ - public function it_can_store_same_filename_in_other_folder_with_no_suffix() - { - $folderRepository = app(FolderRepository::class); - $folder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]); - $file = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $folder->id); - $fileTwo = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg')); - - $subFolder = $folderRepository->create(['name' => 'My Sub Folder', 'parent_id' => $folder->id]); - $fileThree = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $subFolder->id); - - $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.jpg'))); - $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/my-file.jpg'))); - $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/my-sub-folder/my-file.jpg'))); - - $this->assertEquals('/assets/media/my-folder/my-file.jpg', $file->path->getRelativeUrl()); - $this->assertEquals('/assets/media/my-file.jpg', $fileTwo->path->getRelativeUrl()); - $this->assertEquals('/assets/media/my-folder/my-sub-folder/my-file.jpg', $fileThree->path->getRelativeUrl()); - } - /** @test */ public function it_does_not_move_file_if_file_name_exists_at_location() {