diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..0d46efd --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,65 @@ + + + + + + + + + $PROJECT_DIR$/composer.json + + + + + + + + + + + + + + + + + + + + + 1584114192585 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/composer.json b/composer.json index 595d617..5081696 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "imagine/imagine": "^0.7", "myclabs/php-enum": "^1.4", "psr/container": "^1.0", - "symfony/http-foundation": "^3.0|^4.0", + "symfony/http-foundation": "^3.0|^4.0|^5.0", "ext-curl": "*" }, "require-dev": { diff --git a/src/Contracts/Storage/StreamableFileInterface.php b/src/Contracts/Storage/StreamableFileInterface.php new file mode 100644 index 0000000..93c476f --- /dev/null +++ b/src/Contracts/Storage/StreamableFileInterface.php @@ -0,0 +1,11 @@ +file->getRealPath(); } + public function stream($function) + { + $stream = fopen($this->file->getRealPath(), 'r+'); + $output = $function($stream); + fclose($stream); + return $output; + } } diff --git a/src/Storage/File/SplFileInfoStorableFile.php b/src/Storage/File/SplFileInfoStorableFile.php index 0b90553..def58dd 100644 --- a/src/Storage/File/SplFileInfoStorableFile.php +++ b/src/Storage/File/SplFileInfoStorableFile.php @@ -1,12 +1,14 @@ file || ! file_exists($this->file->getRealPath())) { + if (!$this->file || !file_exists($this->file->getRealPath())) { throw new RuntimeException("Local file not found at '{$this->file->getPath()}'"); } @@ -85,7 +88,7 @@ public function delete() ); } - if ( ! $success) { + if (!$success) { // @codeCoverageIgnoreStart throw new StorableFileCouldNotBeDeletedException("Failed to unlink '{$this->path()}'"); // @codeCoverageIgnoreEnd @@ -100,4 +103,11 @@ public function path() return $this->file->getRealPath(); } + public function stream($function) + { + $stream = fopen($this->file->getRealPath(), 'r+'); + $output = $function($stream); + fclose($stream); + return $output; + } } diff --git a/src/Storage/Laravel/LaravelStorage.php b/src/Storage/Laravel/LaravelStorage.php index b699ff0..c8cd88d 100644 --- a/src/Storage/Laravel/LaravelStorage.php +++ b/src/Storage/Laravel/LaravelStorage.php @@ -1,9 +1,11 @@ filesystem = $filesystem; - $this->isLocal = $isLocal; - $this->baseUrl = trim($baseUrl ?: '', '/'); + $this->isLocal = $isLocal; + $this->baseUrl = trim($baseUrl ?: '', '/'); } - + /** * Returns whether a stored file exists. * * @param string $path + * * @return bool */ public function exists($path) @@ -61,6 +65,7 @@ public function exists($path) * Returns a public URL to the stored file. * * @param string $path + * * @return string */ public function url($path) @@ -74,6 +79,7 @@ public function url($path) * Note that the mimetype is not filled in here. Tackle this manually if it is required. * * @param string $path + * * @return StoredFileInterface */ public function get($path) @@ -93,14 +99,25 @@ public function get($path) * Stores a file. * * @param StorableFileInterface $file mixed content to store - * @param string $path where the file should be stored, including the filename + * @param string $path where the file should be stored, including the filename + * * @return StoredFileInterface * @throws FileStorageException */ public function store(StorableFileInterface $file, $path) { - if ( ! $this->filesystem->put($path, $file->content())) { - throw new FileStorageException("Failed to store '{$file->name()}' to '{$path}'"); + if ($file instanceof StreamableFileInterface) { + $file->stream(function ($stream) use ($file, $path) { + if ($this->filesystem->exists($path)) { + $this->filesystem->delete($path); + } + if (!$this->filesystem->writeStream($path, $stream)) + throw new FileStorageException("Failed to store streamable '{$file->name()}' to '{$path}'"); + }); + } else { + if (!$this->filesystem->put($path, $file->content())) { + throw new FileStorageException("Failed to store '{$file->name()}' to '{$path}'"); + } } $stored = new DecoratorStoredFile($file); @@ -113,6 +130,7 @@ public function store(StorableFileInterface $file, $path) * Deletes a stored media file. * * @param string $path + * * @return bool */ public function delete($path) @@ -122,11 +140,12 @@ public function delete($path) /** * @param string $path + * * @return string */ protected function prefixBaseUrl($path) { return $this->baseUrl . '/' . ltrim($path, '/'); } - + } diff --git a/src/Support/Download/UrlDownloader.php b/src/Support/Download/UrlDownloader.php index cbe55dd..442ec88 100644 --- a/src/Support/Download/UrlDownloader.php +++ b/src/Support/Download/UrlDownloader.php @@ -1,4 +1,5 @@ getCode(), - $e - ); - } } /** * @param string $path * @param string $name + * * @return string * @throws CouldNotRetrieveRemoteFileException */ @@ -146,6 +142,7 @@ protected function makeLocalTemporaryPath() * * @param string $path * @param string $newName + * * @return string * @throws CouldNotRetrieveRemoteFileException */ @@ -163,7 +160,7 @@ protected function renameFile($path, $newName) } // @codeCoverageIgnoreStart - if ( ! $success) { + if (!$success) { throw new CouldNotRetrieveRemoteFileException("Failed to rename '{$path}' to '{$newName}'"); } // @codeCoverageIgnoreEnd @@ -175,6 +172,7 @@ protected function renameFile($path, $newName) * Normalizes URL for safe cURL use. * * @param string $url + * * @return string */ protected function normalizeUrl($url)