Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Illuminate/Contracts/Filesystem/FileExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Contracts\Filesystem;

use Exception;

class FileExistsException extends Exception
{
//
}
23 changes: 23 additions & 0 deletions src/Illuminate/Contracts/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public function exists($path);
*/
public function get($path);

/**
* Get a resource to read the file.
*
* @param string $path
* @return resource|null The path resource or null on failure.
*
* @throws FileNotFoundException
*/
public function readStream($path);

/**
* Write the contents of a file.
*
Expand All @@ -46,6 +56,19 @@ public function get($path);
*/
public function put($path, $contents, $options = []);

/**
* Write a new file using a stream.
*
* @param string $path
* @param resource $resource
* @param mixed $options
* @return bool
*
* @throws \InvalidArgumentException If $resource is not a file handle.
* @throws FileExistsException
*/
public function writeStream($path, $resource, array $options = []);

/**
* Get the visibility for the given path.
*
Expand Down
30 changes: 29 additions & 1 deletion src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Collection;
use League\Flysystem\AdapterInterface;
use PHPUnit\Framework\Assert as PHPUnit;
use League\Flysystem\FileExistsException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Cached\CachedAdapter;
Expand All @@ -20,6 +21,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Illuminate\Contracts\Filesystem\Cloud as CloudFilesystemContract;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
use Illuminate\Contracts\Filesystem\FileExistsException as ContractFileExistsException;
use Illuminate\Contracts\Filesystem\FileNotFoundException as ContractFileNotFoundException;

/**
Expand Down Expand Up @@ -132,7 +134,7 @@ public function response($path, $name = null, array $headers = [], $disposition
]);

$response->setCallback(function () use ($path) {
$stream = $this->driver->readStream($path);
$stream = $this->readStream($path);
fpassthru($stream);
fclose($stream);
});
Expand Down Expand Up @@ -390,6 +392,32 @@ public function url($path)
}
}

/**
* {@inheritdoc}
*/
public function readStream($path)
{
try {
$resource = $this->driver->readStream($path);

return $resource ? $resource : null;
} catch (FileNotFoundException $e) {
throw new ContractFileNotFoundException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* {@inheritdoc}
*/
public function writeStream($path, $resource, array $options = [])
{
try {
return $this->driver->writeStream($path, $resource, $options);
} catch (FileExistsException $e) {
throw new ContractFileExistsException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* Get the URL for the file at the given path.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Filesystem/FilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use League\Flysystem\Adapter\Local;
use Illuminate\Filesystem\FilesystemAdapter;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Illuminate\Contracts\Filesystem\FileExistsException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class FilesystemAdapterTest extends TestCase
Expand Down Expand Up @@ -145,4 +146,48 @@ public function testMove()
$this->assertFileExists($this->tempDir.'/foo/foo2.txt');
$this->assertEquals($data, file_get_contents($this->tempDir.'/foo/foo2.txt'));
}

public function testStream()
{
$this->filesystem->write('file.txt', $original_content = 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$readStream = $filesystemAdapter->readStream('file.txt');
$filesystemAdapter->writeStream('copy.txt', $readStream);
$this->assertEquals($original_content, $filesystemAdapter->get('copy.txt'));
}

public function testStreamBetweenFilesystems()
{
$secondFilesystem = new Filesystem(new Local($this->tempDir.'/second'));
$this->filesystem->write('file.txt', $original_content = 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$secondFilesystemAdapter = new FilesystemAdapter($secondFilesystem);
$readStream = $filesystemAdapter->readStream('file.txt');
$secondFilesystemAdapter->writeStream('copy.txt', $readStream);
$this->assertEquals($original_content, $secondFilesystemAdapter->get('copy.txt'));
}

public function testStreamToExistingFileThrows()
{
$this->expectException(FileExistsException::class);
$this->filesystem->write('file.txt', 'Hello World');
$this->filesystem->write('existing.txt', 'Dear Kate');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$readStream = $filesystemAdapter->readStream('file.txt');
$filesystemAdapter->writeStream('existing.txt', $readStream);
}

public function testReadStreamNonExistentFileThrows()
{
$this->expectException(FileNotFoundException::class);
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->readStream('nonexistent.txt');
}

public function testStreamInvalidResourceThrows()
{
$this->expectException(\InvalidArgumentException::class);
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->writeStream('file.txt', 'foo bar');
}
}