From 001e7845ba9e7134fde7a55580d036235b18d6ce Mon Sep 17 00:00:00 2001 From: Joseph Date: Mon, 21 Oct 2019 09:24:43 +0330 Subject: [PATCH 1/5] add streamble and improve url downloader --- composer.json | 4 +- .../Storage/StreamableFileInterface.php | 11 ++++++ src/Storage/File/ProcessableFile.php | 10 ++++- src/Storage/File/SplFileInfoStorableFile.php | 18 +++++++-- src/Storage/Laravel/LaravelStorage.php | 37 ++++++++++++++----- src/Support/Download/UrlDownloader.php | 17 ++++++++- 6 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 src/Contracts/Storage/StreamableFileInterface.php diff --git a/composer.json b/composer.json index 595d617..006b362 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { - "name": "czim/file-handling", + "name": "sibche/file-handling", "description": "File Handling Helper", "keywords": [ "file", "uploads", "storage", "s3" ], - "homepage": "https://github.com/czim", + "homepage": "https://github.com/sibche", "require": { "php": ">=5.5.9", "imagine/imagine": "^0.7", 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..5938ff2 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,23 @@ 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->writeStream($path, $stream)) { + throw new FileStorageException("Failed to store '{$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 +128,7 @@ public function store(StorableFileInterface $file, $path) * Deletes a stored media file. * * @param string $path + * * @return bool */ public function delete($path) @@ -122,11 +138,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..424456e 100644 --- a/src/Support/Download/UrlDownloader.php +++ b/src/Support/Download/UrlDownloader.php @@ -1,4 +1,5 @@ Date: Mon, 21 Oct 2019 09:53:04 +0330 Subject: [PATCH 2/5] fix :( --- src/Support/Download/UrlDownloader.php | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/Support/Download/UrlDownloader.php b/src/Support/Download/UrlDownloader.php index 424456e..442ec88 100644 --- a/src/Support/Download/UrlDownloader.php +++ b/src/Support/Download/UrlDownloader.php @@ -81,10 +81,7 @@ protected function downloadToTempLocalPath($url, $localPath) $rawFile = curl_exec($ch); - die($rawFile); - - curl_close($ch); - fclose($fp); + fclose($fileStream); if ($rawFile === false) { $curlError = curl_error($ch); @@ -105,20 +102,6 @@ protected function downloadToTempLocalPath($url, $localPath) "curl_exec failed while downloading '{$url}': " . $curlError ); } - - try { - if (false === file_put_contents($localPath, $rawFile)) { - throw new CouldNotRetrieveRemoteFileException('file_put_contents call failed'); - } - - } catch (Exception $e) { - - throw new CouldNotRetrieveRemoteFileException( - 'file_put_contents call threw an exception', - $e->getCode(), - $e - ); - } } /** From dca42455f7541b5579f572ac6ffaaf9c3474ef99 Mon Sep 17 00:00:00 2001 From: Joseph Date: Tue, 12 Nov 2019 02:19:37 +0330 Subject: [PATCH 3/5] delete file if exists in streamable put file --- src/Storage/Laravel/LaravelStorage.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Storage/Laravel/LaravelStorage.php b/src/Storage/Laravel/LaravelStorage.php index 5938ff2..c8cd88d 100644 --- a/src/Storage/Laravel/LaravelStorage.php +++ b/src/Storage/Laravel/LaravelStorage.php @@ -108,9 +108,11 @@ public function store(StorableFileInterface $file, $path) { if ($file instanceof StreamableFileInterface) { $file->stream(function ($stream) use ($file, $path) { - if (!$this->filesystem->writeStream($path, $stream)) { - throw new FileStorageException("Failed to store '{$file->name()}' to '{$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())) { From da89895279becde738a5f0cba7cfaf0ef4494f43 Mon Sep 17 00:00:00 2001 From: Mahdi Youseftabar Date: Fri, 13 Mar 2020 19:11:00 +0330 Subject: [PATCH 4/5] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 006b362..335f621 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": { From f6b9d4fbfc5d9de14e2ddf7a5ba05e5b0cd3a507 Mon Sep 17 00:00:00 2001 From: Joseph Date: Fri, 13 Mar 2020 19:15:40 +0330 Subject: [PATCH 5/5] rollback package name to czim --- .idea/workspace.xml | 65 +++++++++++++++++++++++++++++++++++++++++++++ composer.json | 4 +-- 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 .idea/workspace.xml 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 335f621..5081696 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { - "name": "sibche/file-handling", + "name": "czim/file-handling", "description": "File Handling Helper", "keywords": [ "file", "uploads", "storage", "s3" ], - "homepage": "https://github.com/sibche", + "homepage": "https://github.com/czim", "require": { "php": ">=5.5.9", "imagine/imagine": "^0.7",