Skip to content

Commit 34d2b95

Browse files
committed
Move download complete to unified method
1 parent d279e4b commit 34d2b95

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Curl::close()
156156
Curl::complete($callback)
157157
Curl::delete($url, $data = array())
158158
Curl::download($url, $mixed_filename)
159+
Curl::downloadComplete($fh)
159160
Curl::error($callback)
160161
Curl::exec($ch = null)
161162
Curl::get($url, $data = array())

src/Curl/Curl.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,12 @@ public function delete($url, $data = array())
158158
return $this->exec();
159159
}
160160

161-
public function download($url, $mixed_filename)
161+
public function downloadComplete($fh)
162162
{
163-
$callback = false;
164-
if (is_callable($mixed_filename)) {
165-
$callback = $mixed_filename;
166-
$fh = tmpfile();
167-
} else {
168-
$filename = $mixed_filename;
169-
$fh = fopen($filename, 'wb');
170-
}
171-
172-
$this->setOpt(CURLOPT_FILE, $fh);
173-
$this->get($url);
174-
175-
if (!$this->error && $callback) {
163+
if (!$this->error && $this->download_complete_function) {
176164
rewind($fh);
177-
$this->call($callback, $fh);
165+
$this->call($this->download_complete_function, $fh);
166+
$this->download_complete_function = null;
178167
}
179168

180169
if (is_resource($fh)) {
@@ -197,6 +186,22 @@ public function download($url, $mixed_filename)
197186
// responses as the return value of curl_exec(). Without this,
198187
// curl_exec() will revert to returning boolean values.
199188
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
189+
}
190+
191+
public function download($url, $mixed_filename)
192+
{
193+
$callback = false;
194+
if (is_callable($mixed_filename)) {
195+
$this->download_complete_function = $mixed_filename;
196+
$fh = tmpfile();
197+
} else {
198+
$filename = $mixed_filename;
199+
$fh = fopen($filename, 'wb');
200+
}
201+
202+
$this->setOpt(CURLOPT_FILE, $fh);
203+
$this->get($url);
204+
$this->downloadComplete($fh);
200205

201206
return ! $this->error;
202207
}

src/Curl/MultiCurl.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,24 +280,7 @@ public function start()
280280

281281
// Close open file handles and reset the curl instance.
282282
if (isset($this->curl_fhs[$ch->id])) {
283-
$fh = $this->curl_fhs[$ch->id];
284-
285-
if (!$ch->error && $ch->download_complete_function) {
286-
rewind($fh);
287-
$ch->call($ch->download_complete_function, $fh);
288-
}
289-
290-
if (is_resource($fh)) {
291-
fclose($fh);
292-
}
293-
294-
if (!defined('STDOUT')) {
295-
define('STDOUT', null);
296-
}
297-
298-
$ch->setOpt(CURLOPT_FILE, STDOUT);
299-
$ch->setOpt(CURLOPT_RETURNTRANSFER, true);
300-
283+
$ch->downloadComplete($this->curl_fhs[$ch->id]);
301284
unset($this->curl_fhs[$ch->id]);
302285
}
303286
break;

0 commit comments

Comments
 (0)