public function ChosenLibCommands::drush_download_file in Chosen 8.2
Same name and namespace in other branches
- 3.0.x modules/chosen_lib/src/Commands/ChosenLibCommands.php \Drupal\chosen_lib\Commands\ChosenLibCommands::drush_download_file()
Parameters
string $url: The download url.
mixed $destination: The destination path.
Return value
bool|string The destination file.
Throws
\Exception
1 call to ChosenLibCommands::drush_download_file()
- ChosenLibCommands::plugin in modules/
chosen_lib/ src/ Commands/ ChosenLibCommands.php - Download and install the Chosen plugin.
File
- modules/
chosen_lib/ src/ Commands/ ChosenLibCommands.php, line 111
Class
- ChosenLibCommands
- A Drush commandfile.
Namespace
Drupal\chosen_lib\CommandsCode
public function drush_download_file($url, $destination = FALSE) {
// Generate destination if omitted.
if (!$destination) {
$file = basename(current(explode('?', $url, 2)));
$destination = getcwd() . '/' . basename($file);
}
// Copied from: \Drush\Commands\SyncViaHttpCommands::downloadFile
static $use_wget;
if ($use_wget === NULL) {
$process = Drush::process([
'which',
'wget',
]);
$process
->run();
$use_wget = $process
->isSuccessful();
}
$destination_tmp = drush_tempnam('download_file');
if ($use_wget) {
$args = [
'wget',
'-q',
'--timeout=30',
'-O',
$destination_tmp,
$url,
];
}
else {
$args = [
'curl',
'-s',
'-L',
'--connect-timeout',
'30',
'-o',
$destination_tmp,
$url,
];
}
$process = Drush::process($args);
$process
->mustRun();
if (!drush_file_not_empty($destination_tmp) && ($file = @file_get_contents($url))) {
@file_put_contents($destination_tmp, $file);
}
if (!drush_file_not_empty($destination_tmp)) {
// Download failed.
throw new \Exception(dt("The URL !url could not be downloaded.", [
'!url' => $url,
]));
}
if ($destination) {
$fs = new Filesystem();
$fs
->rename($destination_tmp, $destination, TRUE);
return $destination;
}
return $destination_tmp;
}