From 5c6fd4cdf341b4e144736f4ab6faca36ffa65386 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Thu, 8 Nov 2018 08:27:47 -0800 Subject: [PATCH] Fix #555: Remove temporary file when download fails --- src/Curl/Curl.php | 12 +++++++++++- tests/PHPCurlClass/PHPCurlClassTest.php | 12 ++++++++++++ tests/PHPCurlClass/server.php | 4 ++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index a580ee4..1a0373c 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -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; diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 66db23c..b823d95 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -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( diff --git a/tests/PHPCurlClass/server.php b/tests/PHPCurlClass/server.php index 0bcbd0a..5de4a3e 100644 --- a/tests/PHPCurlClass/server.php +++ b/tests/PHPCurlClass/server.php @@ -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');