Clean up archive downloader, fixes #1630

main
Jordi Boggiano 11 years ago
parent 1c468e7c02
commit e3f06582e4

@ -32,7 +32,7 @@ abstract class ArchiveDownloader extends FileDownloader
$fileName = $this->getFileName($package, $path); $fileName = $this->getFileName($package, $path);
if ($this->io->isVerbose()) { if ($this->io->isVerbose()) {
$this->io->write(' Unpacking archive'); $this->io->write(' Extracting archive');
} }
$temporaryDir = sys_get_temp_dir().'/cmp'.substr(md5(time().mt_rand()), 0, 5); $temporaryDir = sys_get_temp_dir().'/cmp'.substr(md5(time().mt_rand()), 0, 5);
@ -46,25 +46,19 @@ abstract class ArchiveDownloader extends FileDownloader
throw $e; throw $e;
} }
if ($this->io->isVerbose()) {
$this->io->write(' Cleaning up');
}
unlink($fileName); unlink($fileName);
// If we have only a one dir inside it suppose to be a package itself // get file list
$contentDir = glob($temporaryDir . '/*'); $contentDir = $this->listFiles($temporaryDir);
if (1 === count($contentDir)) {
$contentDir = $contentDir[0]; // only one dir in the archive, extract its contents out of it
if (1 === count($contentDir) && !is_file($contentDir[0])) {
$contentDir = $this->listFiles($contentDir[0]);
} }
if (is_string($contentDir) && is_file($contentDir)) { // move files back out of the temp dir
$this->filesystem->rename($contentDir, $path . '/' . basename($contentDir)); foreach ($contentDir as $file) {
} else { $this->filesystem->rename($file, $path . '/' . basename($file));
foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
if ('' !== trim(basename($file), '.')) {
$this->filesystem->rename($file, $path . '/' . basename($file));
}
}
} }
$this->filesystem->removeDirectory($temporaryDir); $this->filesystem->removeDirectory($temporaryDir);
@ -129,4 +123,16 @@ abstract class ArchiveDownloader extends FileDownloader
* @throws \UnexpectedValueException If can not extract downloaded file to path * @throws \UnexpectedValueException If can not extract downloaded file to path
*/ */
abstract protected function extract($file, $path); abstract protected function extract($file, $path);
/**
* Returns the list of files in a directory including dotfiles
*/
private function listFiles($dir)
{
$files = array_merge(glob($dir . '/.*'), glob($dir . '/*'));
return array_values(array_filter($files, function ($el) {
return basename($el) !== '.' && basename($el) !== '..';
}));
}
} }

Loading…
Cancel
Save