Make sure a file can be stored with the same name in another location, without having a _1 suffix.

parent 5e169555
......@@ -49,7 +49,7 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
$exists = $this->model->whereFilename($fileName)->first();
if ($exists) {
$fileName = $this->getNewUniqueFilename($fileName);
$fileName = $this->getNewUniqueFilename($fileName, $parentId);
}
$data = [
......@@ -123,14 +123,15 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
/**
* @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);
......@@ -143,6 +144,9 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
return ($version > $carry) ? $version : $carry;
}, 0);
if ($versionCurrent === 0 && $models->count() === 0) {
return $fileName;
}
return $fileNameOnly . '_' . ($versionCurrent+1) . '.' . $extension;
}
......
......@@ -343,6 +343,26 @@ class EloquentFileRepositoryTest extends MediaTestCase
$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([
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment