Problem/Motivation

The function getLocalThumbnailPath() in MediaYouTubeStreamWrapper.inc copies thumbnails to a single directory. This is a scaling problem on high traffic sites with many video thumbnails, as the directory can become too large. This can hinder site performance with folders with large amounts of files (~2,500). Breaking the folder into small sub-folders fixes the issue.

function getLocalThumbnailPath() {
    $parts = $this->get_parameters();
    $local_path = file_default_scheme() . '://media-youtube/' . check_plain($parts['v']) . '.jpg';

    if (!file_exists($local_path)) {
      // getOriginalThumbnailPath throws an exception if there are any errors
      // when retrieving the original thumbnail from YouTube.
      try {
        $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_unmanaged_save_data($response->data, $local_path, TRUE);
        }
        else {
          @copy($this->getOriginalThumbnailPath(), $local_path);
        }
      }
      catch (Exception $e) {
        // In the event of an endpoint error, use the mime type icon provided
        // by the Media module.
        $file = file_uri_to_object($this->uri);
        $icon_dir = variable_get('media_icon_base_directory', 'public://media-icons') . '/' . variable_get('media_icon_set', 'default');
        $local_path = file_icon_path($file, $icon_dir);
      }
    }

    return $local_path;
  }
}

Proposed resolution

Change the $local_path to include an additional folder structure to breakup the directory into smaller sub-folders. Perhaps adding date information (Y/m/d) or video id ($parts['v']). This will prevent a single folder from getting too large.

Also the function media_youtube_cache_clear($fids) will need changed to support recursive deletion by changing line 40 to file_unmanaged_delete_recursive($local_path).

Comments

travelertt created an issue. See original summary.

travelertt’s picture

Issue summary: View changes
travelertt’s picture

Status: Active » Needs review
StatusFileSize
new1.17 KB

This patch puts each file inside a folder with the same Youtube "v" id. And adds changes the clear cache function to work with recursive folders.

travelertt’s picture

Updated patch to correctly remove the thumbnail file and the containing folder.