Skip to content
Snippets Groups Projects
Commit cbc88f39 authored by Kevin Reynen's avatar Kevin Reynen
Browse files

adding files from #1327270

parent ce927010
Branches
Tags
No related merge requests found
<?php
/**
* @file
* Create a Archive Stream Wrapper class for the Media/Resource module.
*/
/**
* Create an instance like this:
* $archive = new ResourceArchiveStreamWrapper('archive://?v=[video-code]');
*/
class MediaArchiveStreamWrapper extends MediaReadOnlyStreamWrapper {
// Overrides $base_url defined in MediaReadOnlyStreamWrapper.
protected $base_url = 'http://archive.org/file';
protected function _parse_url($url) {
$path = explode('://', $url);
$parts = explode('/', $url[1]);
$properties = MediaInternetArchiveHandler::getVideoProperties($path[1]);
$params['v'] = $path[1];
$params['video_thumb'] = $properties['misc']['image'];
preg_match('@archive\.org/([0-9]+)/items/([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)\.gif$@i', $properties['misc']['image'], $matches);
$params['video_dir'] = $matches[1];
$params['video_videofirst'] = $matches[2];
$params['video_videosecond'] = $matches[3];
return $params;
}
/**
* Returns a url in the format "http://archive.org/id".
*
* Overrides interpolateUrl() defined in MediaReadOnlyStreamWrapper.
* This is an exact copy of the function in MediaReadOnlyStreamWrapper,
* here in case that example is redefined or removed.
*/
function interpolateUrl() {
if ($parameters = $this->get_parameters()) {
return $this->base_url . '?' . http_build_query($parameters);
}
}
static function getMimeType($uri, $mapping = NULL) {
return 'video/archive';
}
function getTarget($f) {
return FALSE;
}
function getOriginalThumbnailPath() {
$parts = $this->get_parameters();
return check_plain($parts['video_thumb']);
}
function getLocalThumbnailPath() {
$parts = $this->get_parameters();
$local_path = 'public://media-archive/' . check_plain($parts['video_videofirst']) . '.jpg';
if (!file_exists($local_path)) {
$dirname = drupal_dirname($local_path);
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$response = drupal_http_request($this->getOriginalThumbnailPath());
if (!isset($response->error)) {
file_save_data($response->data, $local_path, TRUE);
}
else {
@copy($this->getOriginalThumbnailPath(), $local_path);
}
}
return $local_path;
}
}
<?php
/**
* @file
* Contains MediaInternetArchiveHandler.
* @TODO: implement Archive.org JSON API.
* @see: http://archive.org/help/json.php
*/
/**
* Implementation of MediaInternetBaseHandler.
*
* @see hook_media_internet_providers().
*/
class MediaInternetArchiveHandler extends MediaInternetBaseHandler {
/**
* Call the Archive.org API to fetch the video information.
*
*
*
* @return
* Array of properties.
*/
static public function getVideoProperties($id, $refresh = FALSE) {
$ids = &drupal_static(__FUNCTION__, array());
if (!$refresh && isset($ids[$id])) {
return $ids[$id];
}
elseif (!$refresh && $cache = cache_get('media_archive:rawid:' . $id, 'cache_media_xml')) {
$ids[$id] = $cache->data;
return $ids[$id];
}
$response = drupal_http_request('http://' . MEDIA_ARCHIVE_BASE_URL . '/embed/' . $id . MEDIA_ARCHIVE_REST_API);
$data = drupal_json_decode($response->data);
$ids[$id] = $data;
cache_set('media_archive:rawid:' . $id, $ids[$id], 'cache_media_xml', media_variable_get('xml_cache_expire', 3600));
return $data;
}
/**
* Check if an Archive.org video id is valid.
*
* This is a bit diffrent from other providers,
* we also return the file_name on success. (?)
*
* @return
* string $file_name.
*/
public function validId($video_id) {
// TODO.
return TRUE;
}
/**
* Parse the embed code provided by the user.
*/
public function parse($embedCode) {
$scheme = 'archive://';
preg_match('@archive\.org/details/([^"\& ]+)@i', $embedCode, $matches);
if (!empty($matches)) {
$org = self::validId($matches[1]);
return file_stream_wrapper_uri_normalize('archive://' . $matches[1]);
}
return FALSE;
}
public function claim($embedCode) {
if ($this->parse($embedCode)) {
return TRUE;
}
}
public function getFileObject() {
$uri = $this->parse($this->embedCode);
$file = file_uri_to_object($uri, TRUE);
if (empty($file->fid)) {
// TODO: fix up this with a real human readable filename.
// $title = $path[1];
// $file->filename = truncate_utf8($title, 255);
}
return $file;
}
/**
* Returns information about the media. See http://video.search.yahoo.com/mrss.
*
* @return
* If ATOM+MRSS information is available, a SimpleXML element containing
* ATOM and MRSS elements, as per those respective specifications.
*
* @todo Would be better for the return value to be an array rather than a
* SimpleXML element, but media_retrieve_xml() needs to be upgraded to
* handle namespaces first.
* http://archive.org/file/4035623?skin=rss
*/
public function getMRSS() {
$uri = media_archive_media_parse($this->embedCode);
$video_id = arg(1, file_uri_target($uri));
$rss_url = url('http://gdata.archive.org/feeds/api/videos/' . $video_id, array('query' => array('v' => '2')));
// @todo Use media_retrieve_xml() once it's upgraded to include elements
// from all namespaces, not just the document default namespace.
$entry = simplexml_load_file($rss_url);
return $entry;
}
/**
* Returns information about the media. See http://www.oembed.com/.
*
* @return
* If oEmbed information is available, an array containing 'title', 'type',
* 'url', and other information as specified by the oEmbed standard.
* Otherwise, NULL.
* http://archive.org/oembed/?url=http://archive.org/file/12345
*/
public function getOEmbed() {
$uri = media_archive_media_parse($this->embedCode);
$external_url = drupal_realpath($uri);
$oembed_url = url('http://archive.org/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
$response = drupal_http_request($oembed_url);
if (!isset($response->error)) {
return drupal_json_decode($response->data);
}
}
}
<?php
/**
* @file
* Integration with File Entity module for display.
* Formatters for Media: Archive.
*/
/**
* Implements hook_file_formatter_info().
*/
function media_archive_file_formatter_info() {
$formatters['media_archive_video'] = array(
'label' => t('Archive Video'),
'file types' => array('video'),
'default settings' => array(),
'view callback' => 'media_archive_file_formatter_video_view',
'settings callback' => 'media_archive_file_formatter_video_settings',
);
foreach (array('width', 'height', 'autoplay', 'controls', 'controls_hide', 'loop') as $setting) {
$formatters['media_archive_video']['default settings'][$setting] = media_archive_variable_get($setting);
}
$formatters['media_archive_image'] = array(
'label' => t('Archive Preview Image'),
'file types' => array('video'),
'default settings' => array(
'image_style' => '',
),
'view callback' => 'media_archive_file_formatter_image_view',
'settings callback' => 'media_archive_file_formatter_image_settings',
);
return $formatters;
}
/**
* Implements hook_file_formatter_FORMATTER_view().
*/
function media_archive_file_formatter_video_view($file, $display, $langcode) {
$scheme = file_uri_scheme($file->uri);
// WYSIWYG does not yet support video inside a running editor instance.
if ($scheme == 'archive' && empty($file->override['wysiwyg'])) {
$element = array(
'#theme' => 'media_archive_video',
'#uri' => $file->uri,
'#options' => array(),
);
// Fake a default for attributes so the ternary doesn't choke.
$display['settings']['attributes'] = array();
foreach (array('width', 'height', 'autoplay', 'controls', 'controls_hide', 'loop') as $setting) {
$element['#options'][$setting] = isset($file->override[$setting]) ? $file->override[$setting] : $display['settings'][$setting];
}
return $element;
}
}
/**
* Implements hook_file_formatter_FORMATTER_settings().
*/
function media_archive_file_formatter_video_settings($form, &$form_state, $settings) {
$element = array();
$element['width'] = array(
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $settings['width'],
'#element_validate' => array('_archive_validate_video_width_and_height'),
);
$element['height'] = array(
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $settings['height'],
'#element_validate' => array('_archive_validate_video_width_and_height'),
);
$element['autoplay'] = array(
'#title' => t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $settings['autoplay'],
);
$element['controls'] = array(
'#title' => t('Show controls'),
'#type' => 'checkbox',
'#default_value' => $settings['controls'],
);
$element['controls_hide'] = array(
'#title' => t('Auto hide controls'),
'#type' => 'checkbox',
'#default_value' => $settings['controls_hide'],
);
$element['loop'] = array(
'#title' => t('Loop video'),
'#type' => 'checkbox',
'#default_value' => $settings['loop'],
);
return $element;
}
/**
* Validation for width and height.
*/
function _archive_validate_video_width_and_height($element, &$form_state, $form) {
// Check if the value is a number with an optional decimal or percentage sign, or "auto".
if (!empty($element['#value']) && !preg_match('/^(auto|([0-9]*(\.[0-9]+)?%?))$/', $element['#value'])) {
form_error($element, t("The value entered for @dimension is invalid. Please insert a unitless integer for pixels, a percent, or \"auto\". Note that percent and auto may not function correctly depending on the browser and doctype.", array('@dimension' => $element['#title'])));
}
}
/**
* Implements hook_file_formatter_FORMATTER_view().
*/
function media_archive_file_formatter_image_view($file, $display, $langcode) {
$scheme = file_uri_scheme($file->uri);
if ($scheme == 'archive') {
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$image_style = $display['settings']['image_style'];
$valid_image_styles = image_style_options(FALSE);
// @TODO: If autosubmit is removed and we allow view modes that insert
// images in the WYSIWYG, add file->overrides handling.
if (empty($image_style) || !isset($valid_image_styles[$image_style])) {
$element = array(
'#theme' => 'image',
'#path' => $wrapper->getOriginalThumbnailPath(),
'#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
);
}
else {
$element = array(
'#theme' => 'image_style',
'#style_name' => $image_style,
'#path' => $wrapper->getLocalThumbnailPath(),
'#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
);
}
return $element;
}
}
/**
* Implements hook_file_formatter_FORMATTER_settings().
*/
function media_archive_file_formatter_image_settings($form, &$form_state, $settings) {
$element = array();
$element['image_style'] = array(
'#title' => t('Image style'),
'#type' => 'select',
'#options' => image_style_options(FALSE),
'#default_value' => $settings['image_style'],
'#empty_option' => t('None (original image)'),
);
$element['width'] = array(
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $settings['width'],
'#states' => array(
'visible' => array(
':input[name="displays[media_archive_image][settings][image_style]"]' => array('value' => ''),
),
),
);
$element['height'] = array(
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $settings['height'],
'#states' => array(
'visible' => array(
':input[name="displays[media_archive_image][settings][image_style]"]' => array('value' => ''),
),
),
);
return $element;
}
/**
* Implements hook_file_default_displays().
*/
function media_archive_file_default_displays() {
$default_displays = array();
// Default settings for displaying as a video.
$default_video_settings = array(
'default' => array(
'width' => 640,
'height' => 388,
'autoplay' => FALSE,
'controls' => TRUE,
'controls_hide' => TRUE,
'loop' => FALSE,
),
'teaser' => array(
'width' => 560,
'height' => 348,
'autoplay' => FALSE,
'controls' => TRUE,
'controls_hide' => TRUE,
'loop' => FALSE,
),
'media_large' => array(
'width' => 480,
'height' => 360,
'autoplay' => FALSE,
'controls' => TRUE,
'controls_hide' => TRUE,
'loop' => FALSE,
),
'media_original' => array(
'width' => 640,
'height' => 480,
'autoplay' => media_archive_variable_get('autoplay'),
'controls' => TRUE,
'controls_hide' => TRUE,
'loop' => FALSE,
),
);
foreach ($default_video_settings as $view_mode => $settings) {
$display_name = 'video__' . $view_mode . '__media_archive_video';
$default_displays[$display_name] = (object) array(
'api_version' => 1,
'name' => $display_name,
'status' => 1,
'weight' => 1,
'settings' => $settings,
);
}
// Default settings for displaying a video preview image.
// We enable preview images even for view modes that also play video
// for use inside a running WYSIWYG editor. We weight them so video
// formatters come first in the cascade to make sure the video formatter
// is used whenever possible.
$default_image_styles = array(
'default' => 'large',
'preview' => 'square_thumbnail',
'teaser' => 'large',
// Legacy view modes, see note above.
'media_preview' => 'square_thumbnail',
'media_large' => 'large',
'media_original' => '',
);
foreach ($default_image_styles as $view_mode => $image_style) {
$display_name = 'video__' . $view_mode . '__media_archive_image';
$default_displays[$display_name] = (object) array(
'api_version' => 1,
'name' => $display_name,
'status' => 1,
'weight' => 2,
'settings' => array('image_style' => $image_style),
);
}
return $default_displays;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment