Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Added new remote upload/download methods to base adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmyers committed Mar 14, 2016
1 parent 340a8af commit 2c4d3b2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
44 changes: 43 additions & 1 deletion src/Dmyers/Storage/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -168,4 +210,4 @@ public function render($path)
'Content-Type' => $mime,
));
}
}
}

0 comments on commit 2c4d3b2

Please sign in to comment.