diff --git a/drush_make.download.inc b/drush_make.download.inc index 2d6c070..18f9f31 100644 --- a/drush_make.download.inc +++ b/drush_make.download.inc @@ -99,7 +99,22 @@ function _drush_make_download_file($download) { $download_path = $tmp_path . '/__download__'; drush_make_mkdir($download_path); - if (!isset($download_mechanism) || $download_mechanism == 'curl') { + // Check if we have already downloaded a file+headers + $can_cache = ($download['request_type'] == 'get'); + if ($can_cache) { + $cache_path = sys_get_temp_dir() .'/__drushcache__/'. sha1($url); + $cache_file = $cache_path .'/file.contents'; + $cache_headers = $cache_path .'/file.headers'; + if (file_exists($cache_file) && file_exists($cache_headers)) { + drush_log(dt('Using cached download of @url', array('@url' => $url))); + $filename = $download_path . '/__destination__'; + copy($cache_file, $filename); + $headers = unserialize(file_get_contents($cache_headers)); + $success = TRUE; + } + } + + if (!$success && (!isset($download_mechanism) || $download_mechanism == 'curl')) { $header_file = $tmp_path . '/__header__'; touch($header_file); drush_shell_exec("ls %s", $download_path); @@ -144,7 +159,7 @@ function _drush_make_download_file($download) { } } - if (!isset($download_mechanism) || $download_mechanism == 'drush_make') { + if (!$success && (!isset($download_mechanism) || $download_mechanism == 'drush_make')) { $retry = 5; $url = $download['url']; $filename = $download_path . '/__destination__'; @@ -183,6 +198,14 @@ function _drush_make_download_file($download) { if (!$success) { return; } + + // Save downloaded files in cache + if ($can_cache && $download_mechanism != 'cached') { + drush_make_mkdir($cache_path); + copy($filename, $cache_file); + file_put_contents($cache_headers, serialize($headers)); + } + // Much more useful in reverse order. $headers = array_reverse($headers); $content_type = '';