You are here

function _print_drush_download_file in Printer, email and PDF versions 7.2

Download a file using wget or curl.

Adapted from a function in drush/includes/drush.inc to support 302 redirects.

Parameters

string $download_url: The path to the file to download.

Return value

string The filename that was downloaded, or NULL if the file could not be downloaded.

2 calls to _print_drush_download_file()
_print_drush_download_lib in includes/print.drush.inc
Download and extract the lib.
_print_drush_github_latest_url in includes/print.drush.inc
Get filename of latest from github.

File

includes/print.drush.inc, line 100
Common drush functions for the print submodules.

Code

function _print_drush_download_file($download_url) {
  if (!drush_get_context('DRUSH_SIMULATE')) {
    $wget_ret = drush_shell_exec("wget -nv --content-disposition %s", $download_url);
    if ($wget_ret) {

      // Get the filename of the saved file from the output.
      $wget_out = explode('"', array_shift(drush_shell_exec_output()));
      $filename = $wget_out[1];
    }
    else {
      $tempnam = uniqid('print_drush_');
      $curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url);
      if ($curl_ret) {

        // File was downloaded with the temporary name.
        // Find the effective name.
        $filename = explode('/', array_shift(drush_shell_exec_output()));
        $filename = array_pop($filename);

        // Rename file from tempname to effective name.
        if (!drush_op('rename', $tempnam, './' . $filename)) {
          $filename = $tempnam;
        }
      }
      else {
        $filename = FALSE;
      }
    }
  }
  else {
    $filename = basename($download_url);
  }
  return $filename;
}