Move download complete to unified method

This commit is contained in:
Zach Borboa
2015-04-01 10:38:37 -07:00
parent d279e4b598
commit 34d2b95d01
3 changed files with 22 additions and 33 deletions
+1
View File
@@ -156,6 +156,7 @@ Curl::close()
Curl::complete($callback)
Curl::delete($url, $data = array())
Curl::download($url, $mixed_filename)
Curl::downloadComplete($fh)
Curl::error($callback)
Curl::exec($ch = null)
Curl::get($url, $data = array())
+20 -15
View File
@@ -158,23 +158,12 @@ class Curl
return $this->exec();
}
public function download($url, $mixed_filename)
public function downloadComplete($fh)
{
$callback = false;
if (is_callable($mixed_filename)) {
$callback = $mixed_filename;
$fh = tmpfile();
} else {
$filename = $mixed_filename;
$fh = fopen($filename, 'wb');
}
$this->setOpt(CURLOPT_FILE, $fh);
$this->get($url);
if (!$this->error && $callback) {
if (!$this->error && $this->download_complete_function) {
rewind($fh);
$this->call($callback, $fh);
$this->call($this->download_complete_function, $fh);
$this->download_complete_function = null;
}
if (is_resource($fh)) {
@@ -197,6 +186,22 @@ class Curl
// responses as the return value of curl_exec(). Without this,
// curl_exec() will revert to returning boolean values.
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
}
public function download($url, $mixed_filename)
{
$callback = false;
if (is_callable($mixed_filename)) {
$this->download_complete_function = $mixed_filename;
$fh = tmpfile();
} else {
$filename = $mixed_filename;
$fh = fopen($filename, 'wb');
}
$this->setOpt(CURLOPT_FILE, $fh);
$this->get($url);
$this->downloadComplete($fh);
return ! $this->error;
}
+1 -18
View File
@@ -280,24 +280,7 @@ class MultiCurl
// Close open file handles and reset the curl instance.
if (isset($this->curl_fhs[$ch->id])) {
$fh = $this->curl_fhs[$ch->id];
if (!$ch->error && $ch->download_complete_function) {
rewind($fh);
$ch->call($ch->download_complete_function, $fh);
}
if (is_resource($fh)) {
fclose($fh);
}
if (!defined('STDOUT')) {
define('STDOUT', null);
}
$ch->setOpt(CURLOPT_FILE, STDOUT);
$ch->setOpt(CURLOPT_RETURNTRANSFER, true);
$ch->downloadComplete($this->curl_fhs[$ch->id]);
unset($this->curl_fhs[$ch->id]);
}
break;