diff --git a/src/Factories/File.php b/src/Factories/File.php index a975b49..92812db 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,14 @@ 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); + if ($curl_options = Stapler::getConfigInstance()->get('stapler.curl_options')) { + curl_setopt_array($ch, $curl_options); + } + if (!$rawFile = curl_exec($ch)) { + $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 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);