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

dev#105493:Gestion du fullsize avec un nouveau media

parent 7aca376d
Pipeline #10843 failed with stage
in 40 seconds
......@@ -5,6 +5,7 @@ use \Storm\Persistence\Configuration;
use \Storm\Model\Loader;
use \Storm\Persistence\SqlStrategy;
use \Pellicule\HttpClientAware;
use \Pellicule\FileSystem;
require __DIR__ . '/../vendor/autoload.php';
......@@ -20,6 +21,10 @@ $app->add(function($request, $handler)
$settings = require __DIR__ . '/../conf/http_client.php';
HttpClientAware::setHttpClientOptions($settings);
$settings = require __DIR__ . '/../conf/persistence.php';
\Pellicule\FileSystem::setBaseUrl($settings['base_url']);
\Pellicule\FileSystem::setBasePath($settings['base_path']);
return $handler->handle($request);
});
......
......@@ -7,7 +7,7 @@ use \Slim\Psr7\Factory\RequestFactory;
class FileSystem {
use HttpClientAware;
protected static $_base_path,
protected static $_base_path = '/tmp/',
$_base_url;
......@@ -30,7 +30,7 @@ class FileSystem {
}
public function download($url, $path) {
public function download($url, $path, $header ='') {
if (!$url || !$path)
return;
......@@ -46,8 +46,18 @@ class FileSystem {
$this->newHttpClient()
->sendRequest($request,
['curl' => ['CURLOPT_FILE', $local_file]]);
['curl' => ['CURLOPT_FILE'=> $local_file,
'CURLOPT_HTTPHEADER' => $this->_headerAsText($header) ]]);
fclose($local_file);
}
protected function _headerAsText($header_array){
$header_text = "";
foreach ($header_array as $key => $value){
$header_text .= $key. ": ". $value ."\n";
}
return $header_text;
}
}
......@@ -19,7 +19,8 @@ class Media extends ModelAbstract {
$_default_attribute_values = [ 'id' => 0,
'created_at' => '',
'updated_at' => ''
];
],
$_provider_headers;
/** @category testing */
......@@ -47,7 +48,6 @@ class Media extends ModelAbstract {
public function beforeSave() {
$this->setFullsize( \Pellicule\FileSystem::baseUrl() . $this->getFilePath());
$datenow = (new \DateTime())->format('Y-m-d H:i:s');
if (!$this->getCreatedAt())
$this->setCreatedAt($datenow);
......@@ -57,7 +57,22 @@ class Media extends ModelAbstract {
public function afterSave() {
if (!$this->fileExists())
$this->getFileSystem()->download($this->getUrl(), $this->getFilePath());
$this->getFileSystem()->download($this->getUrl(),
$this->getFullsize(),
$this->getProviderHeaders());
}
public function setFullsizeFromIdentifier($identifier){
return $this->setFullsize($this->_getFilePathFromIdentifier($identifier));
}
public function _getFilePathFromIdentifier($identifier) {
return '/' . implode('/',
array_merge([$this->getSize(),
$this->getType()],
$this->splitId($identifier)))
. '/'. $identifier .".";
}
......@@ -86,8 +101,9 @@ class Media extends ModelAbstract {
}
public function splitId() {
return array_slice(str_split(sprintf('%04d', $this->getId())),
public function splitId($identifier = "") {
return array_slice(str_split(sprintf('%04d',
($identifier ? $identifier : $this->getId()))),
0, 4);
}
......
......@@ -66,9 +66,10 @@ class Electre extends Provider {
$token = $json_response->access_token;
$this->setHeaders(['Authorization' => 'Bearer '. $token]);
try{
$response = $this->_getAllLinks($token, $this->_getIsbnOrEan($this->_search_args));
$response = $this->_getAllLinks($this->_getIsbnOrEan($this->_search_args));
} catch(\Exception $exception) {
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_GATEWAY_TIMEOUT,
'no_answer_from_gateway');
......@@ -98,12 +99,15 @@ class Electre extends Provider {
{
return (new \Pellicule\Models\Media())
->setProvider($this->providerName())
->setProviderHeaders($this->getHeaders())
->setUrl($link->href)
->setType( (($link->rel == 'coverCopy')
? 'back_cover'
: $link->rel));
: $link->rel))
->setFullsizeFromIdentifier($this->_getIsbnOrEan($this->_search_args));
});
$record = (new Record())
->updateAttributes($this->_search_args)
->setMedia($medialist);
......@@ -133,10 +137,10 @@ class Electre extends Provider {
}
public function _getAllLinks($token, $ean) {
public function _getAllLinks($ean) {
$browser = static::newHttpClient();
$response = $browser->get(static::API_END_POINT . '/v1.0/eans/' . $ean,
['Authorization'=>'Bearer '. $token]);
$this->getHeaders());
return $response;
}
}
......@@ -72,7 +72,8 @@ class Orb extends Provider {
$medialist[] = (new \Pellicule\Models\Media())
->setProvider($this->providerName())
->setType($imagetype_translate[$image_type])
->setUrl($images[$image_type]['original']['src']);
->setUrl($images[$image_type]['original']['src'])
->setFullsizeFromIdentifier($this->_getIsbnOrEan($this->_search_args));
}
$record = (new Record())
......
......@@ -11,7 +11,8 @@ use Fig\Http\Message\StatusCodeInterface;
abstract class Provider {
use HttpClientAware;
protected $_search_args = [];
protected $_search_args = [],
$_headers = [];
public static function newProvider($request, $args) {
return static::_realNewProvider($request, $args)
......@@ -82,6 +83,13 @@ abstract class Provider {
return $this;
}
public function setHeaders($value){
$this->_headers = $value;
}
public function getHeaders(){
return $this->_headers;
}
abstract public function fetchRecord();
}
\ No newline at end of file
......@@ -66,15 +66,13 @@ class MediaLocalRecordTest extends TestCase {
$expected = [ 'media'=>
[
[ 'url' => 'http://image.org/city.jpg',
'fullsize' => FileSystem::baseUrl() . Media::find(135)->getFilePath(),
'provider' => 'Me',
'provider' => 'Me',
'created_at' => '2020-01-13 08:00:00',
'updated_at' => (new \DateTime())->format('Y-m-d H:i:s'),
'type' => 'cover'],
[ 'url' => 'http://image.org/cityback.jpg',
'type' => 'back_cover',
'provider' => 'Me',
'fullsize' => FileSystem::baseUrl() . Media::find(147)->getFilePath(),
'created_at' => '2020-01-21 08:00:00',
'updated_at' => (new \DateTime())->format('Y-m-d H:i:s')
]
......@@ -148,14 +146,14 @@ abstract class MediaWithElectreTestCase extends TestCase {
[ 'media' => [
['url'=> 'http://image.org/city.jpg',
'provider' => 'Electre',
'fullsize' => '',
'fullsize' => '/fullsize/cover/2/2/5/9/2259228194.',
'type' => 'cover',
'created_at' => (new \DateTime())->format('Y-m-d H:i:s'),
'updated_at' => (new \DateTime())->format('Y-m-d H:i:s')
],
['url' => 'http://image.org/cityback.jpg',
'provider' => 'Electre',
'fullsize' => '',
'fullsize' => '/fullsize/back_cover/2/2/5/9/2259228194.',
'type' => 'back_cover',
'created_at' => (new \DateTime())->format('Y-m-d H:i:s'),
'updated_at' => (new \DateTime())->format('Y-m-d H:i:s')
......@@ -425,7 +423,7 @@ abstract class MediaWithOrbTestCase extends TestCase {
[ 'media' => [
['url'=> 'https://products-images.di-static.com/image/orb-9162f416a0b7b2e64a1f5480db8d4394/2259228234-475x500-1.jpg',
'provider' => 'Orb',
'fullsize' => '',
'fullsize' => '/fullsize/cover/2/2/5/9/2259228234.',
'type' => 'cover',
'created_at' => (new \DateTime())->format('Y-m-d H:i:s'),
'updated_at' => (new \DateTime())->format('Y-m-d H:i:s')
......
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