1
0
Fork 0

Clean up archive downloader, fixes #1630

pull/1635/head
Jordi Boggiano 2013-02-27 19:12:54 +01:00
parent 1c468e7c02
commit e3f06582e4
1 changed files with 22 additions and 16 deletions

View File

@ -32,7 +32,7 @@ abstract class ArchiveDownloader extends FileDownloader
$fileName = $this->getFileName($package, $path);
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);
@ -46,26 +46,20 @@ abstract class ArchiveDownloader extends FileDownloader
throw $e;
}
if ($this->io->isVerbose()) {
$this->io->write(' Cleaning up');
}
unlink($fileName);
// If we have only a one dir inside it suppose to be a package itself
$contentDir = glob($temporaryDir . '/*');
if (1 === count($contentDir)) {
$contentDir = $contentDir[0];
// get file list
$contentDir = $this->listFiles($temporaryDir);
// 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)) {
$this->filesystem->rename($contentDir, $path . '/' . basename($contentDir));
} else {
foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
if ('' !== trim(basename($file), '.')) {
// move files back out of the temp dir
foreach ($contentDir as $file) {
$this->filesystem->rename($file, $path . '/' . basename($file));
}
}
}
$this->filesystem->removeDirectory($temporaryDir);
} catch (\Exception $e) {
@ -129,4 +123,16 @@ abstract class ArchiveDownloader extends FileDownloader
* @throws \UnexpectedValueException If can not extract downloaded file to 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) !== '..';
}));
}
}