diff --git a/src/Filesystem.php b/src/Filesystem.php index 1a6b6107b..0c1a97a0a 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -29,11 +29,13 @@ public function __construct( array $config = [], PathNormalizer $pathNormalizer = null, PublicUrlGenerator $publicUrlGenerator = null, + TemporaryUrlGenerator $temporaryUrlGenerator = null, ) { $this->adapter = $adapter; $this->config = new Config($config); $this->pathNormalizer = $pathNormalizer ?: new WhitespacePathNormalizer(); $this->publicUrlGenerator = $publicUrlGenerator; + $this->temporaryUrlGenerator = $temporaryUrlGenerator; } public function fileExists(string $location): bool diff --git a/src/FilesystemTest.php b/src/FilesystemTest.php index 6f2746328..580b8a377 100644 --- a/src/FilesystemTest.php +++ b/src/FilesystemTest.php @@ -4,13 +4,14 @@ namespace League\Flysystem; -use Aws\S3\S3Client; +use DateTimeImmutable; +use DateTimeInterface; use Generator; use IteratorAggregate; -use League\Flysystem\AwsS3V3\AwsS3V3Adapter; use League\Flysystem\InMemory\InMemoryFilesystemAdapter; use League\Flysystem\Local\LocalFilesystemAdapter; use League\Flysystem\UrlGeneration\PublicUrlGenerator; +use League\Flysystem\UrlGeneration\TemporaryUrlGenerator; use LogicException; use PHPUnit\Framework\TestCase; @@ -556,6 +557,40 @@ public function unable_to_get_checksum_for_for_file_that_does_not_exist(): void $filesystem->checksum('path.txt'); } + /** + * @test + */ + public function generating_temporary_urls(): void + { + $filesystem = new Filesystem( + new InMemoryFilesystemAdapter(), + temporaryUrlGenerator: new class() implements TemporaryUrlGenerator { + public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string + { + return 'https://flysystem.thephpleague.com/' . $path . '?exporesAt=' . $expiresAt->format('U'); + } + } + ); + + $now = \time(); + $temporaryUrl = $filesystem->temporaryUrl('some/file.txt', new DateTimeImmutable('@' . $now)); + $expectedUrl = 'https://flysystem.thephpleague.com/some/file.txt?exporesAt=' . $now; + + self::assertEquals($expectedUrl, $temporaryUrl); + } + + /** + * @test + */ + public function not_being_able_to_generate_temporary_urls(): void + { + $filesystem = new Filesystem(new InMemoryFilesystemAdapter()); + + $this->expectException(UnableToGenerateTemporaryUrl::class); + + $filesystem->temporaryUrl('some/file.txt', new DateTimeImmutable()); + } + /** * @test */