From 273443af79830c30f147ac91d3d21aecc4638ba0 Mon Sep 17 00:00:00 2001 From: Ryan Hayle Date: Thu, 9 Jul 2015 17:29:11 -0500 Subject: [PATCH 1/4] Allow specifying custom Curl options in curl_options config parameter. Added error handling for Curl failures. --- src/Factories/File.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Factories/File.php b/src/Factories/File.php index a975b49..934699f 100644 --- a/src/Factories/File.php +++ b/src/Factories/File.php @@ -1,7 +1,9 @@ guess($meta['mediatype']); $filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($meta['uri']) . "." . $extension; @@ -105,7 +107,12 @@ protected static function createFromUrl($file) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - $rawFile = curl_exec($ch); + curl_setopt($ch, CURLOPT_FAILONERROR, 1); + curl_setopt_array($ch, Stapler::getConfigInstance()->get('stapler.curl_options')); + if ($rawFile = curl_exec($ch) === false) { + $errMsg = "Unable to download file: $file\n"; + throw new FileException($errMsg . curl_error($ch), curl_errno($ch)); + } curl_close($ch); // Remove the query string if it exists From c41e6903e89607735ae1e4790f5b1816951088f5 Mon Sep 17 00:00:00 2001 From: Ryan Hayle Date: Thu, 9 Jul 2015 18:13:00 -0500 Subject: [PATCH 2/4] Only call curl_setopt_array if config value exists. --- src/Factories/File.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Factories/File.php b/src/Factories/File.php index 934699f..30d7791 100644 --- a/src/Factories/File.php +++ b/src/Factories/File.php @@ -108,7 +108,9 @@ protected static function createFromUrl($file) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_FAILONERROR, 1); - curl_setopt_array($ch, Stapler::getConfigInstance()->get('stapler.curl_options')); + if ($curl_options = Stapler::getConfigInstance()->get('stapler.curl_options')) { + curl_setopt_array($ch, $curl_options); + } if ($rawFile = curl_exec($ch) === false) { $errMsg = "Unable to download file: $file\n"; throw new FileException($errMsg . curl_error($ch), curl_errno($ch)); From 69fa49aa3b8738ebb4e86562a980913a26238e0a Mon Sep 17 00:00:00 2001 From: Ryan Hayle Date: Thu, 9 Jul 2015 18:41:08 -0500 Subject: [PATCH 3/4] Added test for curl error handler. Fixed invalid redirect URLs in tests. --- .../Codesleeve/Stapler/Factories/FileTest.php | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/Codesleeve/Stapler/Factories/FileTest.php b/tests/Codesleeve/Stapler/Factories/FileTest.php index f285a20..cc4d62b 100644 --- a/tests/Codesleeve/Stapler/Factories/FileTest.php +++ b/tests/Codesleeve/Stapler/Factories/FileTest.php @@ -83,11 +83,26 @@ public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_a * @return void */ public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_a_redirect_url() + { + $uploadedFile = File::create('https://graph.facebook.com/10102210419817761/picture?type=large'); + + $this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile); + } + + /** + * Test that the file factory throws an exception when an invalid URL is specified + * + * @test + * @expectedException Codesleeve\Stapler\Exceptions\FileException + * @expectedExceptionMessageRegExp #Unable to download file:.*# + * @return void + */ + public function it_should_fail_when_url_is_invalid() { $uploadedFile = File::create('https://graph.facebook.com/zuck/picture?type=large'); $this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile); - } + } /** * Test that file created by file factory is not containing unnecessary quer string @@ -95,8 +110,8 @@ public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_a * @test * @return void */ - public function it_should_be_able_to_build_a_stapler_uploaded_file_object_without_following_querystring_in_basename() { - $url = "https://graph.facebook.com/zuck/picture?type=large"; + public function it_should_be_able_to_build_a_stapler_uploaded_file_object_without_following_querystring_in_basename() { + $url = "https://graph.facebook.com/10102210419817761/picture?type=large"; $uploadedFile = File::create($url); $ch = curl_init($url); From 7f78085a1997d71a2681dce7dd48d852659cfcab Mon Sep 17 00:00:00 2001 From: Ryan Hayle Date: Thu, 9 Jul 2015 20:23:27 -0500 Subject: [PATCH 4/4] Fixed stupid logic error --- src/Factories/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factories/File.php b/src/Factories/File.php index 30d7791..92812db 100644 --- a/src/Factories/File.php +++ b/src/Factories/File.php @@ -111,7 +111,7 @@ protected static function createFromUrl($file) if ($curl_options = Stapler::getConfigInstance()->get('stapler.curl_options')) { curl_setopt_array($ch, $curl_options); } - if ($rawFile = curl_exec($ch) === false) { + if (!$rawFile = curl_exec($ch)) { $errMsg = "Unable to download file: $file\n"; throw new FileException($errMsg . curl_error($ch), curl_errno($ch)); }