Commit 261bf980 authored by Henri-Damien LAURENT's avatar Henri-Damien LAURENT
Browse files

dev#105492: Images from electre are downloaded

parent c01e3ebe
Pipeline #10876 failed with stage
in 48 seconds
<?php
return ['base_url' => 'http://pellicule.afi-sa.net/',
'base_path' => './images'
];
\ No newline at end of file
......@@ -7,6 +7,8 @@ use \Storm\Persistence\SqlStrategy;
use \Pellicule\HttpClientAware;
use \Pellicule\FileSystem;
include_once __DIR__ . '/../conf/config.php';
require __DIR__ . '/../vendor/autoload.php';
$app = \Pellicule\AppFactory::create();
......
<?php
namespace Pellicule;
use \Buzz\Client\Curl;
use \Buzz\Client\FileGetContents;
use \Slim\Psr7\Factory\RequestFactory;
use \Slim\Psr7\Factory\ResponseFactory;
use \Fig\Http\Message\StatusCodeInterface;
class FileSystem {
use HttpClientAware;
......@@ -30,7 +32,7 @@ class FileSystem {
}
public function download($url, $path, $header ='') {
public function download($url, $path, $headers = []) {
if (!$url || !$path)
return;
......@@ -38,26 +40,20 @@ class FileSystem {
if (false === \file_exists($directory))
\mkdir($directory, 0755, true);
// can throw exception on invalid url
$request = (new RequestFactory())->createRequest('GET', $url);
foreach($headers as $key => $value)
$request = $request->withHeader($key, $value);
if (false === ($local_file = fopen(static::$_base_path . $path , 'w')))
return;
$this->newHttpClient()
->sendRequest($request,
['curl' => ['CURLOPT_FILE'=> $local_file,
'CURLOPT_HTTPHEADER' => $this->_headerAsText($header) ]]);
$client = new FileGetContents(new ResponseFactory(), ['allow_redirects' => true]);
fclose($local_file);
}
$response = $client
->sendRequest($request, ['timeout' => 4]);
protected function _headerAsText($header_array){
$header_text = "";
foreach ($header_array as $key => $value){
$header_text .= $key. ": ". $value ."\n";
if (($status = $response->getStatusCode()) != StatusCodeInterface::STATUS_OK) {
echo 'file was not downloaded error:'. $status . ' '. $response->getBody();
return $this;
}
return $header_text;
file_put_contents(static::$_base_path . $path, $response->getBody());
}
}
......@@ -17,6 +17,8 @@ class Media extends ModelAbstract {
$_belongs_to = [ 'record' => ['model' => Record::class ] ],
$_default_attribute_values = [ 'id' => 0,
'fullsize' => '',
'provider_headers' => '',
'created_at' => '',
'updated_at' => ''
],
......
......@@ -45,12 +45,12 @@ class FileSystemTest extends TestCase {
/** @test */
public function fullsizeCoverDirectoryShouldBeCreated() {
$this->assertTrue($this->_root->hasChild('fullsize/cover/0/1'));
$this->assertTrue($this->_root->hasChild('fullsize/cover/2/2/5/9/'));
}
/** @test */
public function fullsizeCoverFileShouldBeCreated() {
$this->assertTrue($this->_root->hasChild('fullsize/cover/0/1/3/5/135.jpg'));
$this->assertTrue($this->_root->hasChild('fullsize/cover/2/2/5/9/2259228194.jpg'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment