Skip to content

Commit

Permalink
Merge remote-tracking branch 'hason/filedownloader'
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 5, 2012
2 parents 5f40ace + 303dae7 commit 9bcea6f
Show file tree
Hide file tree
Showing 12 changed files with 638 additions and 89 deletions.
99 changes: 99 additions & 0 deletions src/Composer/Downloader/ArchiveDownloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Composer\Downloader;

use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use Composer\Util\RemoteFilesystem;

/**
* Base downloader for archives
*
* @author Kirill chEbba Chebunin <iam@chebba.org>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author François Pluchino <francois.pluchino@opendisplay.com>
*/
abstract class ArchiveDownloader extends FileDownloader
{
/**
* {@inheritDoc}
*/
public function download(PackageInterface $package, $path)
{
parent::download($package, $path);

$fileName = $this->getFileName($package, $path);
$this->io->write(' Unpacking archive');
$this->extract($fileName, $path);

$this->io->write(' Cleaning up');
unlink($fileName);

// If we have only a one dir inside it suppose to be a package itself
$contentDir = glob($path . '/*');
if (1 === count($contentDir)) {
$contentDir = $contentDir[0];

// Rename the content directory to avoid error when moving up
// a child folder with the same name
$temporaryName = md5(time().rand());
rename($contentDir, $temporaryName);
$contentDir = $temporaryName;

foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
if (trim(basename($file), '.')) {
rename($file, $path . '/' . basename($file));
}
}
rmdir($contentDir);
}

$this->io->write('');
}

/**
* {@inheritdoc}
*/
protected function getFileName(PackageInterface $package, $path)
{
return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo($package->getDistUrl(), PATHINFO_EXTENSION), '.');
}

/**
* {@inheritdoc}
*/
protected function processUrl($url)
{
if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
// bypass https for github if openssl is disabled
if (preg_match('{^https?://(github.com/[^/]+/[^/]+/(zip|tar)ball/[^/]+)$}i', $url, $match)) {
$url = 'http://nodeload.'.$match[1];
} else {
throw new \RuntimeException('You must enable the openssl extension to download files via https');
}
}

return $url;
}

/**
* Extract file to directory
*
* @param string $file Extracted file
* @param string $path Directory
*
* @throws \UnexpectedValueException If can not extract downloaded file to path
*/
abstract protected function extract($file, $path);
}
79 changes: 34 additions & 45 deletions src/Composer/Downloader/FileDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
use Composer\Util\RemoteFilesystem;

/**
* Base downloader for file packages
* Base downloader for files
*
* @author Kirill chEbba Chebunin <iam@chebba.org>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author François Pluchino <francois.pluchino@opendisplay.com>
*/
abstract class FileDownloader implements DownloaderInterface
class FileDownloader implements DownloaderInterface
{
protected $io;

Expand Down Expand Up @@ -52,7 +52,9 @@ public function getInstallationSource()
public function download(PackageInterface $package, $path)
{
$url = $package->getDistUrl();
$checksum = $package->getDistSha1Checksum();
if (!$url) {
throw new \InvalidArgumentException('The given package is missing url information');
}

if (!is_dir($path)) {
if (file_exists($path)) {
Expand All @@ -63,18 +65,11 @@ public function download(PackageInterface $package, $path)
}
}

$fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.');
$fileName = $this->getFileName($package, $path);

$this->io->write(" - Package <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");

if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
// bypass https for github if openssl is disabled
if (preg_match('{^https?://(github.com/[^/]+/[^/]+/(zip|tar)ball/[^/]+)$}i', $url, $match)) {
$url = 'http://nodeload.'.$match[1];
} else {
throw new \RuntimeException('You must enable the openssl extension to download files via https');
}
}
$url = $this->processUrl($url);

$rfs = new RemoteFilesystem($this->io);
$rfs->copy($package->getSourceUrl(), $url, $fileName);
Expand All @@ -85,33 +80,9 @@ public function download(PackageInterface $package, $path)
.' directory is writable and you have internet connectivity');
}

$checksum = $package->getDistSha1Checksum();
if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
throw new \UnexpectedValueException('The checksum verification of the archive failed (downloaded from '.$url.')');
}

$this->io->write(' Unpacking archive');
$this->extract($fileName, $path);

$this->io->write(' Cleaning up');
unlink($fileName);

// If we have only a one dir inside it suppose to be a package itself
$contentDir = glob($path . '/*');
if (1 === count($contentDir)) {
$contentDir = $contentDir[0];

// Rename the content directory to avoid error when moving up
// a child folder with the same name
$temporaryName = md5(time().rand());
rename($contentDir, $temporaryName);
$contentDir = $temporaryName;

foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
if (trim(basename($file), '.')) {
rename($file, $path . '/' . basename($file));
}
}
rmdir($contentDir);
throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')');
}

$this->io->write('');
Expand All @@ -122,8 +93,7 @@ public function download(PackageInterface $package, $path)
*/
public function update(PackageInterface $initial, PackageInterface $target, $path)
{
$fs = new Filesystem();
$fs->removeDirectory($path);
$this->remove($initial, $path);
$this->download($target, $path);
}

Expand All @@ -137,12 +107,31 @@ public function remove(PackageInterface $package, $path)
}

/**
* Extract file to directory
* Gets file name for specific package
*
* @param PackageInterface $package package instance
* @param string $path download path
* @return string file name
*/
protected function getFileName(PackageInterface $package, $path)
{
return $path.'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME);
}

/**
* Process the download url
*
* @param string $file Extracted file
* @param string $path Directory
* @param string $url download url
* @return string url
*
* @throws \UnexpectedValueException If can not extract downloaded file to path
* @throws \RuntimeException If any problem with the url
*/
protected abstract function extract($file, $path);
protected function processUrl($url)
{
if (!extension_loaded('openssl') && 0 === strpos($url, 'https:')) {
throw new \RuntimeException('You must enable the openssl extension to download files via https');
}

return $url;
}
}
2 changes: 1 addition & 1 deletion src/Composer/Downloader/PharDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Kirill chEbba Chebunin <iam@chebba.org>
*/
class PharDownloader extends FileDownloader
class PharDownloader extends ArchiveDownloader
{
/**
* {@inheritDoc}
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Downloader/TarDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Kirill chEbba Chebunin <iam@chebba.org>
*/
class TarDownloader extends FileDownloader
class TarDownloader extends ArchiveDownloader
{
/**
* {@inheritDoc}
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Downloader/ZipDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ZipDownloader extends FileDownloader
class ZipDownloader extends ArchiveDownloader
{
protected $process;

Expand Down
11 changes: 6 additions & 5 deletions src/Composer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ protected function addPackagistRepository(RepositoryManager $rm)
protected function createDownloadManager(IOInterface $io)
{
$dm = new Downloader\DownloadManager();
$dm->setDownloader('git', new Downloader\GitDownloader($io));
$dm->setDownloader('svn', new Downloader\SvnDownloader($io));
$dm->setDownloader('git', new Downloader\GitDownloader($io));
$dm->setDownloader('svn', new Downloader\SvnDownloader($io));
$dm->setDownloader('hg', new Downloader\HgDownloader($io));
$dm->setDownloader('pear', new Downloader\PearDownloader($io));
$dm->setDownloader('zip', new Downloader\ZipDownloader($io));
$dm->setDownloader('tar', new Downloader\TarDownloader($io));
$dm->setDownloader('phar', new Downloader\PharDownloader($io));
$dm->setDownloader('zip', new Downloader\ZipDownloader($io));
$dm->setDownloader('tar', new Downloader\TarDownloader($io));
$dm->setDownloader('phar', new Downloader\PharDownloader($io));
$dm->setDownloader('file', new Downloader\FileDownloader($io));

return $dm;
}
Expand Down
Loading

0 comments on commit 9bcea6f

Please sign in to comment.