Skip to content

Commit

Permalink
Simplifying solution to store same filename in other locations.
Browse files Browse the repository at this point in the history
Moving testcase back to the eloquent file repository test, from the FileMoverTest.
  • Loading branch information
nWidart committed Oct 10, 2017
1 parent 1c1f974 commit ad80d34
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
12 changes: 4 additions & 8 deletions Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions Modules/Media/Tests/EloquentFileRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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([
Expand Down
20 changes: 0 additions & 20 deletions Modules/Media/Tests/FileMoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit ad80d34

Please sign in to comment.