Commit f09b164b authored by guschilds's avatar guschilds

Issue #2446343 by michaelfavia, Tommy Cox, guschilds, aguilarm: Use Drupal's...

Issue #2446343 by michaelfavia, Tommy Cox, guschilds, aguilarm: Use Drupal's filesystem wrappers when creating thumbnails.
parent ec54a049
......@@ -209,25 +209,15 @@ function template_preprocess_youtube_thumbnail(&$variables) {
}
// Check to see if a thumbnail exists locally.
$youtube_dir_path = youtube_get_yt_dir();
$destination = $youtube_dir_path . '/' . $video_id . '.jpg';
if (!file_exists($destination)) {
$uri = youtube_build_thumbnail_uri($video_id);
if (!file_exists($uri)) {
// Retrieve the image from YouTube.
if (!youtube_get_remote_image($video_id)) {
// Use the remote source if local copy fails.
$remote_uri = youtube_build_remote_image_path($video_id);
$uri = youtube_build_remote_image_path($video_id);
}
}
// Build the thumbnail's URI. Use the remote URI if there isn't a local image.
if (empty($remote_uri)) {
$youtube_dir = \Drupal::config('youtube.settings')->get('youtube_thumb_dir');
$uri = 'public://' . $youtube_dir . '/' . $video_id . '.jpg';
}
else {
$uri = $remote_uri;
}
// Build the initial image render array.
$variables['image'] = array(
'#theme' => 'image',
......@@ -285,26 +275,29 @@ function youtube_get_dimensions($size = NULL, $width = NULL, $height = NULL) {
}
/**
* Retrieve youtube thumbnail image via YouTube API.
* Retrieves the thumbnail image for a given video from YouTube.
*
* @param string $video_id
* The video id of the YouTube video.
* @param integer $video_id
* The video ID of the particular YouTube video.
* @param bool $force_small
* When TRUE, this function should return the standard size image regardless
* of what the youtube_thumb_hires variable is set to. This is used should
* the high resolution image be found to not exist for a particular video.
* (optional) When TRUE, this function should return the standard size image
* regardless of what the youtube_thumb_hires variable is set to. This is used
* should the high resolution image be found to not exist for a particular
* video.
*
* @return bool
* TRUE if the remote image was saved locally, FALSE if something went wrong.
* @return bool|stdClass
* Either the Drupal $file object of saved image, or FALSE if the save failed.
*/
function youtube_get_remote_image($video_id = NULL, $force_small = FALSE) {
// This value is TRUE when higher resolution thumnbails should be saved.
// This value is TRUE when higher resolution thumbnails should be saved.
// This resolution is not guaranteed to exist and if it doesn't, the smaller
// resolution image will be saved in its place.
$youtube_thumb_hires = \Drupal::config('youtube.settings')->get('youtube_thumb_hires');
// This boolean is TRUE if we're obtaining a hi-res thumbnail.
$get_hires = ($youtube_thumb_hires && !$force_small);
// Build the image url.
if ($youtube_thumb_hires && !$force_small) {
if ($get_hires) {
$src = youtube_build_remote_image_path($video_id, 'maxresdefault');
}
else {
......@@ -318,7 +311,7 @@ function youtube_get_remote_image($video_id = NULL, $force_small = FALSE) {
catch (RequestException $e) {
// The high resolution image didn't exist and the request was a 404. Force
// it to try again, but look for the smaller resolution image.
if ($youtube_thumb_hires && !$force_small) {
if ($get_hires) {
return youtube_get_remote_image($video_id, TRUE);
}
......@@ -326,15 +319,28 @@ function youtube_get_remote_image($video_id = NULL, $force_small = FALSE) {
return FALSE;
}
$youtube_thumb_uri = youtube_build_thumbnail_uri();
if (!file_prepare_directory($youtube_thumb_uri, FILE_CREATE_DIRECTORY) && !mkdir($youtube_thumb_uri, 0775, TRUE)) {
\Drupal::service('logger.factory')->get('youtube')->log(WATCHDOG_ERROR,
'Failed to create YouTube thumbnail directory: %dir',
array('%dir' => $youtube_thumb_uri));
return FALSE;
}
$data = $image_request->getBody(TRUE);
$yt_dir_path = youtube_get_yt_dir();
$dest = $yt_dir_path . '/' . $video_id . '.jpg';
$destination = youtube_build_thumbnail_uri($video_id);
if (!file_prepare_directory($yt_dir_path, FILE_CREATE_DIRECTORY) && !mkdir($yt_dir_path, 0775, TRUE)) {
\Drupal::service('logger.factory')->get('youtube')->log(WATCHDOG_ERROR, 'Failed to create YouTube thumbnail directory: %dir', array('%dir' => $yt_dir_path));
}
// Save the thumbnail and add to Drupal managed files.
$file = file_save_data($data, $destination, FILE_EXISTS_REPLACE);
if (!$file) {
\Drupal::service('logger.factory')->get('youtube')->log(WATCHDOG_ERROR,
'Unable to save youtube thumbnail to filesystem for video %id',
array('%id' => $video_id));
return FALSE;
}
return file_put_contents($dest, $data);
return $file;
}
/**
......@@ -343,28 +349,52 @@ function youtube_get_remote_image($video_id = NULL, $force_small = FALSE) {
* This is a submit callback for the "Refresh" option in the configuration form.
*/
function youtube_thumb_delete_all($form, &$form_state) {
$yt_dir_path = youtube_get_yt_dir();
$youtube_thumb_uri = youtube_build_thumbnail_uri();
if (file_prepare_directory($yt_dir_path) && file_unmanaged_delete_recursive($yt_dir_path)) {
drupal_set_message(t('All YouTube Field thumbnail image files have been
deleted and will be redownloaded upon the next page load. This does not
include derivatives created by image styles.'));
drupal_flush_all_caches();
if (!file_prepare_directory($youtube_thumb_uri)) {
return drupal_set_message(t('No files deleted.'));
}
else {
drupal_set_message(t('There were no thumbnails to delete.'), 'warning');
$files = file_scan_directory($youtube_thumb_uri, '/^.*\.(jpg|png)$/');
foreach ($files as $raw_file) {
$database = \Drupal::database();
$data = $database->select('file_managed','fm')
->fields('fm')
->condition('uri', '%' . $raw_file->uri, 'LIKE')
->execute()
->fetchAssoc();
if (!$managed_file) {
// Unmanaged files exist from when the module didn't use managed files.
file_unmanaged_delete($raw_file->uri);
}
else {
// file_delete() invokes hooks to refresh associated image style files.
file_delete($managed_file['fid']);
}
}
}
/**
* Returns the absolute path of the remote image download directory.
* Builds the URI to a given thumbnail or the module's thumbnail directory.
*
* @param string $video_id
* (optional) The video ID to build the thumbnail URI for.
*
* @return string
* If a $video_id was supplied, the URI to that video's thumbnail. Otherwise
* the URI to the module's thumbnail directory.
*/
function youtube_get_yt_dir() {
$youtube_dir = \Drupal::config('youtube.settings')->get('youtube_thumb_dir');
$youtube_dir = empty($youtube_dir) ? 'youtube' : $youtube_dir;
$yt_dir_uri = file_build_uri($youtube_dir);
$yt_dir = drupal_realpath($yt_dir_uri);
return $yt_dir;
function youtube_build_thumbnail_uri($video_id = NULL) {
$youtube_thumb_dir = \Drupal::config('youtube.settings')->get('youtube_thumb_dir');
$youtube_thumb_dir = empty($youtube_thumb_dir) ? 'youtube' : $youtube_thumb_dir;
$youtube_thumb_uri = file_build_uri($youtube_thumb_dir);
if ($video_id) {
return $youtube_thumb_uri . '/' . $video_id . '.jpg';
}
return $youtube_thumb_uri;
}
/**
......@@ -493,10 +523,6 @@ function youtube_token_info_alter(&$data) {
/**
* Implements hook_tokens().
*
* Provide replacement values for placeholder tokens.
*
* Replaces youtube_video_url and youtube_image_url tokens.
*
* @see youtube_tokens_info_alter()
*/
function youtube_tokens($type, $tokens, array $data = array(), array $options = array()) {
......@@ -549,9 +575,7 @@ function youtube_tokens($type, $tokens, array $data = array(), array $options =
$field = $node->$field_name;
if ($video_id = $field->video_id) {
$thumb_dir = \Drupal::config('youtube.settings')->get('youtube_thumb_dir');
$thumb_dir = empty($thumb_dir) ? 'youtube' : $thumb_dir;
$file_uri = 'public://' . $thumb_dir . '/' . $video_id . '.jpg';
$file_uri = youtube_build_thumbnail_uri($video_id);
if (file_exists($file_uri) || youtube_get_remote_image($video_id)) {
$replacements[$original] = file_create_url($file_uri);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment