Skip to content

Commit

Permalink
Make sure a file can be stored with the same name in another location…
Browse files Browse the repository at this point in the history
…, without having a _1 suffix.
  • Loading branch information
nWidart committed Oct 10, 2017
1 parent 5e16955 commit ebac68e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function createFromFile(UploadedFile $file, int $parentId = 0)
$exists = $this->model->whereFilename($fileName)->first();

if ($exists) {
$fileName = $this->getNewUniqueFilename($fileName);
$fileName = $this->getNewUniqueFilename($fileName, $parentId);
}

$data = [
Expand Down Expand Up @@ -123,14 +123,15 @@ public function findMultipleFilesByZoneForEntity($zone, $entity)

/**
* @param $fileName
* @param int $parentId
* @return string
*/
private function getNewUniqueFilename($fileName)
private function getNewUniqueFilename($fileName, int $parentId = 0)
{
$fileNameOnly = pathinfo($fileName, PATHINFO_FILENAME);
$extension = pathinfo($fileName, PATHINFO_EXTENSION);

$models = $this->model->where('filename', 'LIKE', "$fileNameOnly%")->get();
$models = $this->model->where('filename', 'LIKE', "$fileNameOnly%")->where('folder_id', $parentId)->get();

$versionCurrent = $models->reduce(function ($carry, $model) {
$latestFilename = pathinfo($model->filename, PATHINFO_FILENAME);
Expand All @@ -143,6 +144,9 @@ private function getNewUniqueFilename($fileName)

return ($version > $carry) ? $version : $carry;
}, 0);
if ($versionCurrent === 0 && $models->count() === 0) {
return $fileName;
}

return $fileNameOnly . '_' . ($versionCurrent+1) . '.' . $extension;
}
Expand Down
20 changes: 20 additions & 0 deletions Modules/Media/Tests/EloquentFileRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,26 @@ 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());
}

private function createFile($fileName = 'random/name.jpg')
{
return File::create([
Expand Down

0 comments on commit ebac68e

Please sign in to comment.