Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying custom Curl options in curl_options config parameter #136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Factories/File.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace Codesleeve\Stapler\Factories;

use Codesleeve\Stapler\Stapler;
use Codesleeve\Stapler\File\Mime\MimeType;
use Codesleeve\Stapler\File\File as StaplerFile;
use Codesleeve\Stapler\Exceptions\FileException;
use Codesleeve\Stapler\File\UploadedFile as StaplerUploadedFile;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
Expand Down Expand Up @@ -61,11 +63,11 @@ protected static function createFromObject(SymfonyUploadedFile $file)

protected static function createFromDataURI($file) {
$fp = @fopen($file, 'r');

if (!$fp) {
throw new \Codesleeve\Stapler\Exceptions\FileException('Invalid data URI');
}

$meta = stream_get_meta_data($fp);
$extension = static::getMimeTypeExtensionGuesserInstance()->guess($meta['mediatype']);
$filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($meta['uri']) . "." . $extension;
Expand Down Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions tests/Codesleeve/Stapler/Factories/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,35 @@ 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
*
* @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);
Expand Down