Skip to content

Commit

Permalink
support chunk_size in cl_upload_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
itaibenari committed Mar 25, 2015
1 parent 2002c38 commit dfb15a1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
function cl_upload_url($options = array())
{
if (!@$options["resource_type"]) $options["resource_type"] = "auto";
return Cloudinary::cloudinary_api_url("upload", $options);
$endpoint = array_key_exists('chunk_size', $options) ? 'upload_chunked' : 'upload';
return Cloudinary::cloudinary_api_url($endpoint, $options);
}

function cl_upload_tag_params($options = array())
Expand All @@ -24,6 +25,11 @@ function cl_unsigned_image_upload_tag($field, $upload_preset, $options = array()
}

function cl_image_upload_tag($field, $options = array())
{
return cl_upload_tag($field, $options);
}

function cl_upload_tag($field, $options = array())
{
$html_options = Cloudinary::option_get($options, "html", array());

Expand All @@ -37,6 +43,7 @@ function cl_image_upload_tag($field, $options = array())
"data-cloudinary-field" => $field,
"class" => implode(" ", $classes),
));
if (array_key_exists('chunk_size', $options)) $tag_options['data-max-chunk-size'] = $options['chunk_size'];
return '<input ' . Cloudinary::html_attrs($tag_options) . '/>';
}

Expand Down Expand Up @@ -182,7 +189,7 @@ function cl_video_thumbnail_path($source, $options = array()) {
# cl_video_tag("mymovie.ogv", array('poster' => "myspecialplaceholder.jpg"))
# cl_video_tag("mymovie.webm", array('source_types' => array('webm', 'mp4'), 'poster' => array('effect' => 'sepia')))
function cl_video_tag($source, $options = array()) {
$source = preg_replace('/' . implode('|', default_source_types()) . '$/', '', $source);
$source = preg_replace('/\.' . implode('|', default_source_types()) . '$/', '', $source);

$video_attributes = array('autoplay','controls','loop','muted','poster', 'preload', 'width', 'height');

Expand All @@ -191,11 +198,7 @@ function cl_video_tag($source, $options = array()) {
$fallback = Cloudinary::option_consume($options, 'fallback_content', '');

if (empty($source_types)) {
if (!empty($source_transformation)) {
$source_types = array_keys($source_transformation);
} else {
$source_types = default_source_types();
}
$source_types = default_source_types();
}
$video_options = $options;

Expand All @@ -216,8 +219,7 @@ function cl_video_tag($source, $options = array()) {

$html = '<video ';

$video_options['resource_type'] = 'video';
$video_attributes = array_merge($video_attributes, array('width', 'height'));
if (!array_key_exists('resource_type', $video_options)) $video_options['resource_type'] = 'video';
if (!is_array($source_types)){
array_push($video_attributes, 'src');
$source .= '.' . $source_types;
Expand Down
1 change: 1 addition & 0 deletions src/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public static function explicit($public_id, $options = array())
"callback" => \Cloudinary::option_get($options, "callback"),
"eager" => Uploader::build_eager(\Cloudinary::option_get($options, "eager")),
"eager_async" => \Cloudinary::option_get($options, "eager_async"),
"eager_notification_url" => \Cloudinary::option_get($options, "eager_notification_url"),
"headers" => Uploader::build_custom_headers(\Cloudinary::option_get($options, "headers")),
"tags" => \Cloudinary::encode_array(\Cloudinary::option_get($options, "tags")),
"face_coordinates" => \Cloudinary::encode_double_array(\Cloudinary::option_get($options, "face_coordinates")),
Expand Down
31 changes: 29 additions & 2 deletions tests/CloudinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CloudinaryTest extends PHPUnit_Framework_TestCase {
const VIDEO_UPLOAD_PATH = 'http://res.cloudinary.com/test123/video/upload/';

public function setUp() {
Cloudinary::config(array("cloud_name"=>"test123", "api_secret"=>"b", "secure_distribution" => NULL, "private_cdn" => FALSE));
Cloudinary::config(array("cloud_name"=>"test123", "api_key" => "a", "api_secret"=>"b", "secure_distribution" => NULL, "private_cdn" => FALSE));
}

public function test_cloud_name() {
Expand Down Expand Up @@ -645,8 +645,9 @@ public function test_cl_video_tag_with_source_transformation(){
'ogv' => array('quality' => 70),
'mp4' => array('quality' => 30)))),
"<video poster=\"$expected_url.jpg\" width=\"100\">" .
"<source src=\"$expected_ogv_url.ogv\" type=\"video/ogg\">" .
"<source src=\"$expected_url.webm\" type=\"video/webm\">" .
"<source src=\"$expected_mp4_url.mp4\" type=\"video/mp4\">" .
"<source src=\"$expected_ogv_url.ogv\" type=\"video/ogg\">" .
"</video>");

$this->assertEquals(cl_video_tag('movie', array('width' => 100, 'transformation' => array(array('quality' => 50)), 'source_transformation' => array(
Expand Down Expand Up @@ -681,6 +682,32 @@ public function test_cl_video_tag_with_poster(){
"<video src=\"$expected_url.mp4\"></video>");
}

public function test_upload_tag(){
$pattern = "/<input class=\"cloudinary-fileupload\" ".
"data-cloudinary-field=\"image\" ".
"data-form-data=\"{\"timestamp\":\d+,\"signature\":\"\w+\",\"api_key\":\"a\"}\" ".
"data-url=\"http[^\"]+\/v1_1\/test123\/auto\/upload\" ".
"name=\"file\" type=\"file\"\/>/";
$this->assertRegExp( $pattern, cl_upload_tag('image'));
$this->assertRegExp( $pattern, cl_image_upload_tag('image'));

$pattern = "/<input class=\"cloudinary-fileupload\" ".
"data-cloudinary-field=\"image\" ".
"data-form-data=\"{\"timestamp\":\d+,\"signature\":\"\w+\",\"api_key\":\"a\"}\" ".
"data-max-chunk-size=\"5000000\" ".
"data-url=\"http[^\"]+\/v1_1\/test123\/auto\/upload_chunked\" ".
"name=\"file\" type=\"file\"\/>/";
$this->assertRegExp( $pattern,
cl_upload_tag('image', array('chunk_size' => 5000000)));

$pattern = "/<input class=\"classy cloudinary-fileupload\" ".
"data-cloudinary-field=\"image\" ".
"data-form-data=\"{\"timestamp\":\d+,\"signature\":\"\w+\",\"api_key\":\"a\"}\" ".
"data-url=\"http[^\"]+\/v1_1\/test123\/auto\/upload\" ".
"name=\"file\" type=\"file\"\/>/";
$this->assertRegExp( $pattern,
cl_upload_tag('image', array("html" => array('class' => 'classy'))));
}


private function cloudinary_url_assertion($source, $options, $expected) {
Expand Down

0 comments on commit dfb15a1

Please sign in to comment.