Skip to content

Commit

Permalink
Merge pull request #8 from SwitchSystems/fix-download-and-unzipping
Browse files Browse the repository at this point in the history
fixing issues with download action after potential changes to geolite ma...
  • Loading branch information
snapshotpl committed Nov 4, 2014
2 parents df7ff41 + d664ec1 commit adcf9ac
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
'geoip' => 'ZfSnapGeoip\Service\Geoip',
'geoip_record' => 'ZfSnapGeoip\Entity\Record',
'geoip_hydrator' => 'Zend\Stdlib\Hydrator\ClassMethods',
'ZfSnapGeoip\HttpClient\Adapter' => 'Zend\Http\Client\Adapter\Curl',
),
'factories' => array(
'ZfSnapGeoip\DatabaseConfig' => 'ZfSnapGeoip\DatabaseConfigFactory',
'ZfSnapGeoip\HttpClient' => 'ZfSnapGeoip\HttpClientFactory',
),
'shared' => array(
'geoip_record' => false,
Expand Down
38 changes: 29 additions & 9 deletions src/ZfSnapGeoip/Controller/ConsoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace ZfSnapGeoip\Controller;

use Zend\Http\Client;
use Zend\Http\Request;
use Zend\Http\Response;
use ZfSnapGeoip\DatabaseConfig;
use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Console\ColorInterface as Color;
Expand Down Expand Up @@ -35,14 +38,28 @@ class ConsoleController extends AbstractActionController
*/
protected $config;

/**
* @var Zend\Http\Client
*/
protected $httpClient;

/**
* @param Console $console
* @param DatabaseConfig $config
*/
public function __construct(Console $console, DatabaseConfig $config)
public function __construct(Console $console, DatabaseConfig $config, Client $httpClient)
{
$this->console = $console;
$this->config = $config;
$this->setHttpClient($httpClient);
}

/**
* @param \Zend\Http\Client $httpClient
*/
public function setHttpClient(Client $httpClient)
{
$this->httpClient = $httpClient;
}

/**
Expand Down Expand Up @@ -72,24 +89,26 @@ public function downloadAction()
return;
}

$gzFilePath = $this->config->getPackedDatabasePath();
$source = $this->config->getSource();

$this->writeLine(sprintf('Downloading %s...', $source), Color::YELLOW);

$this->httpClient->setUri($source);
$this->httpClient->setMethod(Request::METHOD_GET);
$response = $this->httpClient->send();

if (!copy($source, $gzFilePath)) {
if ($response->getStatusCode() !== Response::STATUS_CODE_200) {
$this->writeLine('Error during file download occured', Color::RED);
return;
}

$this->writeLine('Download completed', Color::GREEN);
$this->writeLine('Unzip the downloading data...', Color::YELLOW);

$events->trigger(__FUNCTION__ . '.pre', $this, array(
'path' => $gzFilePath,
'path' => $datFilePath,
));

system(sprintf('gunzip -f %s', $gzFilePath));
$this->writeLine('Download completed', Color::GREEN);
$this->writeLine('Unzip the downloading data...', Color::YELLOW);
file_put_contents($datFilePath, gzdecode($response->getBody()));

$events->trigger(__FUNCTION__ . '.post', $this, array(
'path' => $datFilePath,
Expand All @@ -112,4 +131,5 @@ private function writeLine($text, $color = null, $bgColor = null)
$this->console->writeLine($text, $color, $bgColor);
}
}
}
}

3 changes: 2 additions & 1 deletion src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$serviceLocator = $serviceLocator->getServiceLocator();
$console = $serviceLocator->get('Console');
$config = $serviceLocator->get('ZfSnapGeoip\DatabaseConfig');
$httpClient = $serviceLocator->get('ZfSnapGeoip\HttpClient');

return new ConsoleController($console, $config);
return new ConsoleController($console, $config, $httpClient);
}
}
5 changes: 0 additions & 5 deletions src/ZfSnapGeoip/DatabaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,4 @@ public function getDatabasePath()
{
return $this->getDestination() . $this->getFilename();
}

public function getPackedDatabasePath()
{
return $this->getDestination() . $this->getSourceBasename();
}
}
18 changes: 18 additions & 0 deletions src/ZfSnapGeoip/HttpClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace ZfSnapGeoip;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Http\Client;
use Zend\Http\Client\Adapter\Curl;

class HttpClientFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$client = new Client();
$client->setAdapter($serviceLocator->get('ZfSnapGeoip\HttpClient\Adapter'));

return $client;
}
}

0 comments on commit adcf9ac

Please sign in to comment.