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

Commit 2c4d3b2

Browse files
committed
Added new remote upload/download methods to base adapter
1 parent 340a8af commit 2c4d3b2

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,24 @@ Upload a file into storage:
7474
Storage::upload(Input::file('avatar'), 'user/avatar.jpg');
7575
```
7676

77+
Upload a remote file into storage:
78+
79+
```php
80+
Storage::remoteUpload('http://laravel.com/favicon.ico', 'user/avatar.jpg');
81+
```
82+
7783
Download a file from storage:
7884

7985
```php
8086
Storage::download('user/avatar.jpg', 'tmp/images/user-1/avatar.jpg');
8187
```
8288

89+
Download a remote file locally:
90+
91+
```php
92+
Storage::remoteDownload('http://laravel.com/favicon.ico', 'tmp/images/user-1/avatar.jpg');
93+
```
94+
8395
Delete a file from storage:
8496

8597
```php

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"aws/aws-sdk-php": "2.*"
1818
},
1919
"suggest": {
20-
"aws/aws-sdk-php": "Required for AmazonS3 driver."
20+
"aws/aws-sdk-php": "Required for AmazonS3 driver.",
21+
"guzzlehttp/guzzle": "Required for remote uploads/downloads."
2122
},
2223
"autoload": {
2324
"classmap": [

src/Dmyers/Storage/Adapter/Base.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ abstract public function put($path, $contents);
4949
*/
5050
abstract public function upload($path, $target);
5151

52+
/**
53+
* Upload a remote file into storage.
54+
*
55+
* @param string $url The url to the remote file to upload.
56+
* @param string $target The path to the file to store.
57+
*
58+
* @return bool
59+
*/
60+
public function remoteUpload($url, $target)
61+
{
62+
$tmp_name = md5($url);
63+
$tmp_path = Storage::config('tmp_path').'/'.$tmp_name;
64+
65+
if (!$this->remoteDownload($url, $tmp_path)) {
66+
return false;
67+
}
68+
69+
return $this->upload($tmp_path, $target);
70+
}
71+
5272
/**
5373
* Download a file from storage.
5474
*
@@ -59,6 +79,28 @@ abstract public function upload($path, $target);
5979
*/
6080
abstract public function download($path, $target);
6181

82+
/**
83+
* Download a remote file locally.
84+
*
85+
* @param string $url The url to the remote file to download.
86+
* @param string $target The path to the local file to store.
87+
*
88+
* @return bool
89+
*/
90+
public function remoteDownload($url, $target)
91+
{
92+
$client = new \GuzzleHttp\Client();
93+
94+
try {
95+
$client->get($url, array('save_to' => $target));
96+
}
97+
catch (\GuzzleHttp\Exception\RequestException $e) {
98+
return false;
99+
}
100+
101+
return true;
102+
}
103+
62104
/**
63105
* Delete a file from storage.
64106
*
@@ -168,4 +210,4 @@ public function render($path)
168210
'Content-Type' => $mime,
169211
));
170212
}
171-
}
213+
}

0 commit comments

Comments
 (0)