Skip to content

[12.x] Add streamableUri function to FilesystemAdapter class #55375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,16 @@ public function providesTemporaryUrls()
return method_exists($this->adapter, 'getTemporaryUrl') || isset($this->temporaryUrlCallback);
}

/**
* Determine if streamable URIs can be generated.
*
* @return bool
*/
public function providesStreamableUris()
{
return isset($this->config['stream_wrapper']);
}

/**
* Get a temporary URL for the file at the given path.
*
Expand Down Expand Up @@ -837,6 +847,32 @@ public function temporaryUploadUrl($path, $expiration, array $options = [])
throw new RuntimeException('This driver does not support creating temporary upload URLs.');
}

/**
* Get a stream URI for the file at the given path.
*
* @param string $path
* @param array $options
* @return string
*
* @throws \RuntimeException
*/
public function streamableUri($path, array $options = [])
{
if (! $this->providesStreamableUris()) {
throw new RuntimeException('This driver does not support creating stream URIs.');
}

$wrapper = $this->config['stream_wrapper'];

$availableWrappers = stream_get_wrappers();

if (! in_array($wrapper, $availableWrappers)) {
throw new RuntimeException("The stream wrapper [{$wrapper}] is not available.");
}

return $wrapper.'://'.ltrim($this->path($path), '\\/');
}

/**
* Concatenate a path to a URL.
*
Expand Down
68 changes: 68 additions & 0 deletions tests/Filesystem/FilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Mockery as m;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\HttpFoundation\StreamedResponse;

class FilesystemAdapterTest extends TestCase
Expand Down Expand Up @@ -737,4 +738,71 @@ public function testGetChecksum()
$this->assertEquals('730bed78bccf58c2cfe44c29b71e5e6b', $filesystemAdapter->checksum('path.txt'));
$this->assertEquals('a5c3556d', $filesystemAdapter->checksum('path.txt', ['checksum_algo' => 'crc32']));
}

public function testProvidesStreamableUris()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter, [
'stream_wrapper' => 'file',
]);

$this->assertTrue($filesystemAdapter->providesStreamableUris());
}

public function testDoesNotProvideStreamableUrisWhenConfigurationIsMissing()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);

$this->assertFalse($filesystemAdapter->providesStreamableUris());
}

public function testStreamableUri()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter, [
'root' => $this->tempDir.DIRECTORY_SEPARATOR,
'stream_wrapper' => 'file',
]);

$filesystemAdapter->write('file.txt', 'contents of file');

$this->assertEquals('file://'.ltrim($this->tempDir, '/').DIRECTORY_SEPARATOR.'file.txt', $filesystemAdapter->streamableUri('file.txt'));
}

public function testStreamableUriWithPrefix()
{
$prefix = 'images'.DIRECTORY_SEPARATOR;

$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter, [
'stream_wrapper' => 'file',
'root' => $this->tempDir.DIRECTORY_SEPARATOR,
'prefix' => $prefix,
]);

$filesystemAdapter->write('file.txt', 'contents of file');

$this->assertEquals('file://'.ltrim($this->tempDir, '/').DIRECTORY_SEPARATOR.$prefix.'file.txt', $filesystemAdapter->streamableUri('file.txt'));
}

public function testThrowExceptionsForStramableUri()
{
$this->expectException(RuntimeException::class);

$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);

$filesystemAdapter->write('file.txt', 'contents of file');

$filesystemAdapter->streamableUri('file.txt');
}

public function testThrowExceptionsWhenStreamWrapperIsNotSupported()
{
$this->expectException(RuntimeException::class);

$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter, [
'stream_wrapper' => 'wrapper_that_does_not_exist',
]);

$filesystemAdapter->write('file.txt', 'contents of file');

$filesystemAdapter->streamableUri('file.txt');
}
}
Loading