-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arxy\FilesBundle\Utility; | ||
|
||
use Arxy\FilesBundle\ErrorHandler; | ||
use Arxy\FilesBundle\ManagerInterface; | ||
use Arxy\FilesBundle\Model\File; | ||
use SplFileInfo; | ||
|
||
use function fclose; | ||
use function fopen; | ||
use function stream_copy_to_stream; | ||
|
||
class FileDownloader | ||
{ | ||
private ManagerInterface $manager; | ||
|
||
public function __construct(ManagerInterface $manager) | ||
{ | ||
$this->manager = $manager; | ||
} | ||
|
||
/** | ||
* @throws \ErrorException | ||
*/ | ||
public function downloadAsSplFile(File $file): SplFileInfo | ||
{ | ||
$tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('file-downloader', true); | ||
|
||
ErrorHandler::wrap(static fn (): bool => mkdir($tempDir)); | ||
|
||
$tmpFile = $tempDir . DIRECTORY_SEPARATOR . $file->getOriginalFilename(); | ||
|
||
$destinationStream = ErrorHandler::wrap(static fn () => fopen($tmpFile, 'w')); | ||
ErrorHandler::wrap(fn () => stream_copy_to_stream($this->manager->readStream($file), $destinationStream)); | ||
|
||
fclose($destinationStream); | ||
|
||
return new SplFileInfo($tmpFile); | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arxy\FilesBundle\Tests; | ||
|
||
use Arxy\FilesBundle\Manager; | ||
use Arxy\FilesBundle\ManagerInterface; | ||
use Arxy\FilesBundle\NamingStrategy; | ||
use Arxy\FilesBundle\Storage\FlysystemStorage; | ||
use Arxy\FilesBundle\Utility\FileDownloader; | ||
use League\Flysystem\Filesystem; | ||
use League\Flysystem\FilesystemOperator; | ||
use League\Flysystem\InMemory\InMemoryFilesystemAdapter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FileDownloaderTest extends TestCase | ||
{ | ||
private ManagerInterface $manager; | ||
private FilesystemOperator $filesystem; | ||
|
||
private FileDownloader $downloader; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->filesystem = new Filesystem(new InMemoryFilesystemAdapter()); | ||
|
||
$this->manager = new Manager( | ||
File::class, | ||
new FlysystemStorage($this->filesystem), | ||
/** @implements NamingStrategy<File> */ | ||
new class implements NamingStrategy { | ||
public function getDirectoryName(\Arxy\FilesBundle\Model\File $file): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function getFileName(\Arxy\FilesBundle\Model\File $file): string | ||
{ | ||
return (string)$file->getId(); | ||
} | ||
}, | ||
new FileRepository(), | ||
); | ||
|
||
$this->downloader = new FileDownloader($this->manager); | ||
} | ||
|
||
public function testDownloadAsSplFilePersisted(): void | ||
{ | ||
$file = $this->manager->upload(new \SplFileObject(__DIR__ . '/files/image1.jpg')); | ||
$this->manager->moveFile($file); | ||
|
||
$splFile = $this->downloader->downloadAsSplFile($file); | ||
|
||
self::assertFileEquals($splFile->getPathname(), __DIR__ . '/files/image1.jpg'); | ||
} | ||
|
||
public function testDownloadAsSplFileNotPersisted(): void | ||
{ | ||
$file = $this->manager->upload(new \SplFileObject(__DIR__ . '/files/image1.jpg')); | ||
|
||
$splFile = $this->downloader->downloadAsSplFile($file); | ||
|
||
self::assertFileEquals($splFile->getPathname(), __DIR__ . '/files/image1.jpg'); | ||
} | ||
} | ||