Merge pull request #556 from zachborboa/master

Remove temporary file when download fails
This commit is contained in:
Zach Borboa
2018-11-08 12:12:32 -08:00
committed by GitHub
4 changed files with 28 additions and 1 deletions
+1
View File
@@ -215,6 +215,7 @@ Curl::getCurl()
Curl::getCurlErrorCode()
Curl::getCurlErrorMessage()
Curl::getDownloadCompleteCallback()
Curl::getDownloadFileName()
Curl::getErrorCallback()
Curl::getErrorCode()
Curl::getErrorMessage()
+11 -1
View File
@@ -39,6 +39,7 @@ class Curl
public $errorCallback = null;
public $completeCallback = null;
public $fileHandle = null;
private $downloadFileName = null;
public $attempts = 0;
public $retries = 0;
@@ -281,6 +282,7 @@ class Curl
{
if (is_callable($mixed_filename)) {
$this->downloadCompleteCallback = $mixed_filename;
$this->downloadFileName = null;
$this->fileHandle = tmpfile();
} else {
$filename = $mixed_filename;
@@ -299,6 +301,7 @@ class Curl
$range = $first_byte_position . '-';
$this->setOpt(CURLOPT_RANGE, $range);
}
$this->downloadFileName = $download_filename;
$this->fileHandle = fopen($download_filename, $mode);
// Move the downloaded temporary file to the destination save path.
@@ -1382,6 +1385,11 @@ class Curl
return $this->downloadCompleteCallback;
}
public function getDownloadFileName()
{
return $this->downloadFileName;
}
public function getSuccessCallback()
{
return $this->successCallback;
@@ -1541,7 +1549,9 @@ class Curl
*/
private function downloadComplete($fh)
{
if (!$this->error && $this->downloadCompleteCallback) {
if ($this->error && is_file($this->downloadFileName)) {
@unlink($this->downloadFileName);
} elseif (!$this->error && $this->downloadCompleteCallback) {
rewind($fh);
$this->call($this->downloadCompleteCallback, $fh);
$this->downloadCompleteCallback = null;
+12
View File
@@ -810,6 +810,18 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertFalse(file_exists($filename));
}
public function testDownloadErrorDeleteTemporaryFile()
{
$destination = \Helper\get_tmp_file_path();
$test = new Test();
$test->curl->setHeader('X-DEBUG-TEST', '404');
$test->curl->download(Test::TEST_URL, $destination);
$this->assertFalse(file_exists($test->curl->getDownloadFileName()));
$this->assertFalse(file_exists($destination));
}
public function testMaxFilesize()
{
$tests = array(
+4
View File
@@ -346,6 +346,10 @@ if ($test === 'http_basic_auth') {
echo '202 Accepted';
echo ' (remaining failures: ' . $_SESSION['failures_remaining'] . ')';
exit;
} elseif ($test === '404') {
header('HTTP/1.1 404 Not Found');
echo '404 Not Found';
exit;
}
header('Content-Type: text/plain');