diff --git a/README.md b/README.md index 58bb84e..862854e 100644 --- a/README.md +++ b/README.md @@ -74,12 +74,24 @@ Upload a file into storage: Storage::upload(Input::file('avatar'), 'user/avatar.jpg'); ``` +Upload a remote file into storage: + +```php +Storage::remoteUpload('http://laravel.com/favicon.ico', 'user/avatar.jpg'); +``` + Download a file from storage: ```php Storage::download('user/avatar.jpg', 'tmp/images/user-1/avatar.jpg'); ``` +Download a remote file locally: + +```php +Storage::remoteDownload('http://laravel.com/favicon.ico', 'tmp/images/user-1/avatar.jpg'); +``` + Delete a file from storage: ```php diff --git a/composer.json b/composer.json index ca19f76..42edd81 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "aws/aws-sdk-php": "2.*" }, "suggest": { - "aws/aws-sdk-php": "Required for AmazonS3 driver." + "aws/aws-sdk-php": "Required for AmazonS3 driver.", + "guzzlehttp/guzzle": "Required for remote uploads/downloads." }, "autoload": { "classmap": [ diff --git a/src/Dmyers/Storage/Adapter/Base.php b/src/Dmyers/Storage/Adapter/Base.php index 903acbe..15090f6 100644 --- a/src/Dmyers/Storage/Adapter/Base.php +++ b/src/Dmyers/Storage/Adapter/Base.php @@ -49,6 +49,26 @@ abstract public function put($path, $contents); */ abstract public function upload($path, $target); + /** + * Upload a remote file into storage. + * + * @param string $url The url to the remote file to upload. + * @param string $target The path to the file to store. + * + * @return bool + */ + public function remoteUpload($url, $target) + { + $tmp_name = md5($url); + $tmp_path = Storage::config('tmp_path').'/'.$tmp_name; + + if (!$this->remoteDownload($url, $tmp_path)) { + return false; + } + + return $this->upload($tmp_path, $target); + } + /** * Download a file from storage. * @@ -59,6 +79,28 @@ abstract public function upload($path, $target); */ abstract public function download($path, $target); + /** + * Download a remote file locally. + * + * @param string $url The url to the remote file to download. + * @param string $target The path to the local file to store. + * + * @return bool + */ + public function remoteDownload($url, $target) + { + $client = new \GuzzleHttp\Client(); + + try { + $client->get($url, array('save_to' => $target)); + } + catch (\GuzzleHttp\Exception\RequestException $e) { + return false; + } + + return true; + } + /** * Delete a file from storage. * @@ -168,4 +210,4 @@ public function render($path) 'Content-Type' => $mime, )); } -} \ No newline at end of file +}