5 * Contains \Drupal\hacked\hackedProjectWebFilesDownloader.
8 namespace Drupal\hacked;
10 use Drupal\Component\Utility\Unicode;
14 * Downloads a project using a standard Drupal method.
16 class hackedProjectWebFilesDownloader extends hackedProjectWebDownloader {
17 function download_link() {
18 if (!empty($this->project->project_info['releases'][$this->project->existing_version])) {
19 $this_release = $this->project->project_info['releases'][$this->project->existing_version];
20 return $this_release['download_link'];
25 $dir = $this->get_destination();
26 if (!($release_url = $this->download_link())) {
30 // If our directory already exists, we can just return the path to this cached version
31 if (file_exists($dir) && count(hacked_file_scan_directory($dir, '/.*/', [
42 // Build the destination folder tree if it doesn't already exists.
43 if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
44 $message = $this->t('Failed to create temp directory: %dir', ['%dir' => $dir]);
45 \Drupal::logger('hacked')->error($message->render());
49 if (!($local_file = $this->file_get($release_url))) {
50 $message = $this->t('Could not download the project: @name from URL: @url', [
51 '@name' => $this->project->title(),
52 '@url' => $release_url
54 \Drupal::logger('hacked')->error($message->render());
58 $this->archive_extract($local_file, $dir);
60 catch (Exception $e) {
61 $message = $this->t('Could not extract the project: @name. Error was: !error', [
62 '@name' => $this->project->title(),
63 '!error' => $e->getMessage()
65 \Drupal::logger('hacked')->error($message->render());
73 * Copies a file from $url to the temporary directory for updates.
75 * If the file has already been downloaded, returns the the local path.
78 * The URL of the file on the server.
83 function file_get($url) {
84 $parsed_url = parse_url($url);
85 $remote_schemes = ['http', 'https', 'ftp', 'ftps', 'smb', 'nfs'];
86 if (!in_array($parsed_url['scheme'], $remote_schemes)) {
87 // This is a local file, just return the path.
88 return \Drupal::service('file_system')->realpath($url);
91 // Check the cache and download the file if needed.
92 $cache_directory = 'temporary://hacked-cache';
93 $local = $cache_directory . '/' . basename($parsed_url['path']);
95 if (!file_exists($cache_directory)) {
96 mkdir($cache_directory);
99 return system_retrieve_file($url, $local, FALSE, FILE_EXISTS_REPLACE);
103 * Unpack a downloaded archive file.
105 * @param string $file
106 * The filename of the archive you wish to extract.
107 * @param string $directory
108 * The directory you wish to extract the archive into.
110 * The Archiver object used to extract the archive.
111 * @throws Exception on failure.
113 function archive_extract($file, $directory) {
114 $archiver = archiver_get_archiver($file);
116 throw new Exception(t('Cannot extract %file, not a valid archive.', ['%file' => $file]));
119 // Remove the directory if it exists, otherwise it might contain a mixture of
120 // old files mixed with the new files (e.g. in cases where files were removed
121 // from a later release).
122 $files = $archiver->listContents();
123 // Unfortunately, we can only use the directory name for this. :(
124 $project = Unicode::substr($files[0], 0, -1);
125 $extract_location = $directory . '/' . $project;
126 if (file_exists($extract_location)) {
127 file_unmanaged_delete_recursive($extract_location);
130 $archiver->extract($directory);