diff --git a/video_embed_field.module b/video_embed_field.module index 8826ead..0264472 100644 --- a/video_embed_field.module +++ b/video_embed_field.module @@ -88,7 +88,7 @@ function video_embed_field_menu() { $items['vef/load/%'] = array( 'title' => 'Video Embed Field - Load Video', 'page callback' => '_video_embed_field_show_video', - 'page arguments' => array(2), + 'page arguments' => array(2, 3), 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); @@ -547,10 +547,8 @@ function video_embed_field_get_ajax_url($video_url, $video_style) { $data = $style->data[$handler['name']]; // Write values for later AJAX load. - $hash = _video_embed_field_store_video($video_url, $video_style); - return array( - 'path' => 'vef/load/' . $hash, + 'path' => 'vef/load/' . $video_style . '/' . _video_embed_field_base64url_encode($video_url), 'options' => array( 'attributes' => array( 'class' => array( @@ -625,52 +623,22 @@ function _video_embed_code_get_settings_str($settings = array()) { } /** - * Stores a video to be loaded later from an _video_embed_field_load_video. + * Renders a video for an AJAX call. * - * @param string $video_url - * The video URL. * @param string $video_style * The video style. * - * @return string - * The hash generated for the video URL and the given style. - */ -function _video_embed_field_store_video($video_url, $video_style) { - // Create a hash key. - $hash = _video_embed_field_hash($video_url, $video_style); - - // Check that this record doesn't already exist before saving it. - if (!_video_embed_field_load_video($hash)) { - $record = array( - 'vhash' => $hash, - 'video_url' => $video_url, - 'video_style' => $video_style, - ); - - cache_set('vef-store-' . $hash, $record); - - // Add it to our static cache so we won't have to go to the database. - $static_cache = &drupal_static('vef_video_store', array()); - $static_cache[$hash] = $record; - } - return $hash; -} - -/** - * Renders a video for an AJAX call. - * - * @param string $hash - * The video hash. + * @param string $video_url + * The URL address to a video. * * @return Null * Null because prints an AJAX output. */ -function _video_embed_field_show_video($hash) { - $data = _video_embed_field_load_video($hash); +function _video_embed_field_show_video($video_style, $video_url) { $video = array( '#theme' => 'video_embed_field_embed_code', - '#style' => $data['video_style'], - '#url' => $data['video_url'], + '#style' => $video_style, + '#url' => _video_embed_field_base64url_decode($video_url), ); print drupal_render($video); @@ -678,45 +646,31 @@ function _video_embed_field_show_video($hash) { } /** - * Loads a video from the video store given its hash. + * Encode a video URL to be usable as path part for loading a video. * - * @param string $hash - * The video hash. + * @param string $string + * The video URL. * - * @return array|bool - * An array with video definition, FALSE if the hash does not exist. + * @return string + * The base64 encoded URL. */ -function _video_embed_field_load_video($hash) { - $static_cache = &drupal_static('vef_video_store', array()); - // Check if we've already loaded it. - if (isset($static_cache[$hash])) { - return $static_cache[$hash]; +function _video_embed_field_base64url_encode($string) { + $data = base64_encode($string); + // Modify the output, so it's safe to use in path parts. + return strtr($data, array('+' => '-', '/' => '_')); } - else { - $result = cache_get('vef-store-' . $hash); - if ($result) { - // Cache it before returning. - $data = $result->data; - $static_cache[$hash] = $data; - return $data; - } - else { - return FALSE; - } - } -} + /** - * Creates a hash for storing or looking up a video in the store table. + * Decode encoded string to the video URL for loading a video. * - * @param string $video_url - * The video URL. - * @param string $video_style - * The video style. + * @param string $data + * The base64 encoded URL. * - * @return string - * The hash generated for the video URL and the given style. + * @return false|string + * The video URL. */ -function _video_embed_field_hash($video_url, $video_style) { - return md5('vef' . $video_url . $video_style); +function _video_embed_field_base64url_decode($data) { + $string = strtr($data, array('-' => '+', '_' => '/')); + return base64_decode($string); }