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).
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | getLocalThumbnailPath-scaling-2741333-4.patch | 1.2 KB | travelertt |
| #3 | getLocalThumbnailPath-scaling-2741333-3.patch | 1.17 KB | travelertt |
Comments
Comment #2
travelerttComment #3
travelerttThis 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.
Comment #4
travelerttUpdated patch to correctly remove the thumbnail file and the containing folder.