-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to use flysystem to manage uploaded files
- Loading branch information
1 parent
6b1722a
commit 4959fc9
Showing
9 changed files
with
181 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Decorator; | ||
|
||
use League\Flysystem\FilesystemOperator; | ||
use Symfony\Component\HttpFoundation\File\File as File; | ||
|
||
class FlysystemFile extends File | ||
{ | ||
private FilesystemOperator $filesystemOperator; | ||
|
||
public function __construct(FilesystemOperator $filesystemOperator, string $path) | ||
{ | ||
$this->filesystemOperator = $filesystemOperator; | ||
|
||
parent::__construct($path, false); | ||
} | ||
|
||
public function getSize(): int | ||
{ | ||
return $this->filesystemOperator->fileSize($this->getPathname()); | ||
} | ||
|
||
public function getMTime(): int | ||
{ | ||
return $this->filesystemOperator->lastModified($this->getPathname()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,27 +2,31 @@ | |
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Form\DataTransformer; | ||
|
||
use League\Flysystem\FilesystemOperator; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
use Symfony\Component\HttpFoundation\File\File; | ||
use EasyCorp\Bundle\EasyAdminBundle\Decorator\FlysystemFile; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
||
/** | ||
* @author Yonel Ceruto <[email protected]> | ||
*/ | ||
class StringToFileTransformer implements DataTransformerInterface | ||
{ | ||
private string $uploadDir; | ||
private ?string $uploadDir; | ||
private $uploadFilename; | ||
private $uploadValidate; | ||
private bool $multiple; | ||
private ?FilesystemOperator $filesystemOperator; | ||
|
||
public function __construct(string $uploadDir, callable $uploadFilename, callable $uploadValidate, bool $multiple) | ||
public function __construct(?string $uploadDir, callable $uploadFilename, callable $uploadValidate, bool $multiple, ?FilesystemOperator $filesystemOperator = null) | ||
{ | ||
$this->uploadDir = $uploadDir; | ||
$this->uploadFilename = $uploadFilename; | ||
$this->uploadValidate = $uploadValidate; | ||
$this->multiple = $multiple; | ||
$this->filesystemOperator = $filesystemOperator; | ||
} | ||
|
||
public function transform($value): mixed | ||
|
@@ -73,7 +77,11 @@ private function doTransform($value): ?File | |
throw new TransformationFailedException('Expected a string or null.'); | ||
} | ||
|
||
if (is_file($this->uploadDir.$value)) { | ||
if (null !== $this->filesystemOperator) { | ||
if ($this->filesystemOperator->fileExists($value)) { | ||
return new FlysystemFile($this->filesystemOperator, $value); | ||
} | ||
} elseif (is_file($this->uploadDir.$value)) { | ||
return new File($this->uploadDir.$value); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Field; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\ImageConfigurator; | ||
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField; | ||
use League\Flysystem\FilesystemOperator; | ||
|
||
class ImageFieldTest extends AbstractFieldTest | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$projectDir = __DIR__.'/../TestApplication'; | ||
$this->configurator = new ImageConfigurator($projectDir); | ||
} | ||
|
||
public function testFilesystemOperator(): void | ||
{ | ||
$filesystemOperator = $this->createStub(FilesystemOperator::class); | ||
|
||
$field = ImageField::new('foo')->setFilesystemOperator($filesystemOperator); | ||
$fieldDto = $this->configure($field); | ||
|
||
self::assertNotNull($fieldDto->getCustomOption(ImageField::OPTION_FILESYSTEM_OPERATOR)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/Form/DataTransformer/StringToFileTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Form\DataTransformer; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Decorator\FlysystemFile; | ||
use EasyCorp\Bundle\EasyAdminBundle\Form\DataTransformer\StringToFileTransformer; | ||
use League\Flysystem\FilesystemOperator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class StringToFileTransformerTest extends TestCase | ||
{ | ||
|
||
public function testTransform(): void | ||
{ | ||
$uploadFilename = static fn($value) => 'foo'; | ||
$uploadValidate = static fn($filename) => 'foo'; | ||
$filesystemOperatorMock = $this->createStub(FilesystemOperator::class); | ||
$filesystemOperatorMock | ||
->method('fileExists') | ||
->willReturn(true) | ||
; | ||
|
||
$stringToFileTransformer = new StringToFileTransformer(null, $uploadFilename, $uploadValidate, false, $filesystemOperatorMock); | ||
|
||
$transformedFile = $stringToFileTransformer->transform('bar'); | ||
|
||
self::assertInstanceOf(FlysystemFile::class, $transformedFile); | ||
} | ||
} |