Skip to content

Commit

Permalink
Fix directories with ReplaceFile
Browse files Browse the repository at this point in the history
Fix #1316
  • Loading branch information
UlrichThomasGabor committed Sep 5, 2022
1 parent e9c240f commit 76bbb31
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/Storage/FileSystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ protected function doUpload(PropertyMapping $mapping, File $file, ?string $dir,
{
$uploadDir = $mapping->getUploadDestination().\DIRECTORY_SEPARATOR.$dir;

if (!\file_exists($uploadDir)) {
if (!\mkdir($uploadDir, recursive: true)) {
throw new \Exception('Could not create directory "'.$uploadDir.'"');
}
}
if (!\is_dir($uploadDir)) {
throw new \Exception('Tried to move file to directory "'.$uploadDir.'" but it is a file');
}

if ($file instanceof UploadedFile) {
return $file->move($uploadDir, $name);
}
Expand Down
68 changes: 54 additions & 14 deletions tests/Storage/FileSystemStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ public function resolveUriDataProvider(): array
*/
public function testUploadedFileIsCorrectlyMoved(string $uploadDir, string $dir, string $expectedDir): void
{
$uploadDir = $this->root->url().\DIRECTORY_SEPARATOR.$uploadDir;
$expectedDir = $this->root->url().\DIRECTORY_SEPARATOR.$expectedDir;
$file = $this->getUploadedFileMock();

$file
->method('getClientOriginalName')
->willReturn('filename.txt');
->willReturn('test.txt');

$this->mapping
->expects(self::once())
Expand All @@ -285,7 +287,7 @@ public function testUploadedFileIsCorrectlyMoved(string $uploadDir, string $dir,
->expects(self::once())
->method('getUploadName')
->with($this->object)
->willReturn('filename.txt');
->willReturn('test.txt');

$this->mapping
->expects(self::once())
Expand All @@ -296,7 +298,7 @@ public function testUploadedFileIsCorrectlyMoved(string $uploadDir, string $dir,
$file
->expects(self::once())
->method('move')
->with($expectedDir, 'filename.txt');
->with($expectedDir, 'test.txt');

$this->storage->upload($this->object, $this->mapping);
}
Expand All @@ -307,10 +309,9 @@ public function testUploadedFileIsCorrectlyMoved(string $uploadDir, string $dir,
public function testReplacingFileIsCorrectlyUploaded(): void
{
$file = $this->getReplacingFileMock();

$file
->method('getClientOriginalName')
->willReturn('filename.txt');
->willReturn('test.txt');
$file
->method('getPathname')
->willReturn($this->getValidUploadDir().'/test.txt');
Expand Down Expand Up @@ -341,24 +342,63 @@ public function testReplacingFileIsCorrectlyUploaded(): void
$this->storage->upload($this->object, $this->mapping);
}

/**
* @group upload
*/
public function testReplacingFileWithDirectoryNamerIsCorrectlyUploaded(): void
{
$file = $this->getReplacingFileMock();
$file
->method('getClientOriginalName')
->willReturn('test.txt');
$file
->method('getPathname')
->willReturn($this->getValidUploadDir().'/test.txt');

$this->mapping
->expects(self::once())
->method('getFile')
->with($this->object)
->willReturn($file);

$this->mapping
->expects(self::once())
->method('getUploadDestination')
->willReturn($this->root->url().\DIRECTORY_SEPARATOR.'storage');

$this->mapping
->expects(self::once())
->method('getUploadName')
->with($this->object)
->willReturn('test.txt');

$this->mapping
->expects(self::once())
->method('getUploadDir')
->with($this->object)
->willReturn('vich_uploader_bundle/directoryNamer/1/');

$this->storage->upload($this->object, $this->mapping);
}

public function filenameWithDirectoriesDataProvider(): array
{
return [
// upload dir, dir, expected dir
[
'/root_dir',
'zero subdirectories' => [
'/storage/vich_uploader_bundle',
'',
'/root_dir/',
'/storage/vich_uploader_bundle/',
],
[
'/root_dir',
'one subdirectory' => [
'/storage/vich_uploader_bundle',
'dir_1',
'/root_dir/dir_1',
'/storage/vich_uploader_bundle/dir_1',
],
[
'/root_dir',
'two subdirectories' => [
'/storage/vich_uploader_bundle',
'dir_1/dir_2',
'/root_dir/dir_1/dir_2',
'/storage/vich_uploader_bundle/dir_1/dir_2',
],
];
}
Expand Down

0 comments on commit 76bbb31

Please sign in to comment.