diff --git a/library/Class/WebService/Abstract.php b/library/Class/WebService/Abstract.php index bec5e5f1f3eebee9f73d4cb076285e24fa8b4be9..51977f04beee2f105aa8bcb2afb15ebda6adff57 100644 --- a/library/Class/WebService/Abstract.php +++ b/library/Class/WebService/Abstract.php @@ -53,4 +53,23 @@ class Class_WebService_Abstract { $params = array_filter([$url,$options]); return call_user_func_array([$this->getHttpClient(),'open_url'],$params); // avoid mocking of 2nd parameters } + + + public function httpGetResponse($url, $options= []) { + $params = array_filter([$url,$options]); + return call_user_func_array([$this->getHttpClient(), 'getResponse'], $params); // avoid mocking of 2nd parameters + } + + + public function httpTypeFrom($response, $filter=[]) { + if (!$content_type = $response->getHeader('Content-Type')) + return; + + $parts = explode('/', current(explode(';', $content_type))); + if (!$filter) + return $parts[1]; + + if (in_array(current($parts), $filter)) + return $parts[1]; + } } \ No newline at end of file diff --git a/library/Class/WebService/BibNumerique/Vignette.php b/library/Class/WebService/BibNumerique/Vignette.php index f663be676c9a3f80023632e825eea8ce56aa4021..0857da8d1578fd52330841de4fa39e49b53efc41 100644 --- a/library/Class/WebService/BibNumerique/Vignette.php +++ b/library/Class/WebService/BibNumerique/Vignette.php @@ -80,12 +80,11 @@ class Class_WebService_BibNumerique_Vignette extends Class_WebService_Abstract { return; } - if (!$poster = $this->httpGet($url)) { - $this->getLogger()->log($this->_('Impossible de télécharger l\'image "%s" sur le serveur', $url)); - return; - } + $response = $this->httpGetResponse($url); + if (!$response->isSuccessful() || (!$poster = $response->getBody())) + return $this->getLogger()->log($this->_('Impossible de télécharger l\'image "%s" sur le serveur', $url)); - if(!$filename = $this->_initFilename($url, $poster, $album)) + if(!$filename = $this->_initFilename($url, $poster, $album, $response)) return; $album->setUploadMover('fichier', $this->_upload_mover); @@ -102,10 +101,17 @@ class Class_WebService_BibNumerique_Vignette extends Class_WebService_Abstract { } - protected function _initFilename($url, $poster, $album) { - $parts = explode('/', $url); - $filename = array_pop($parts); - $filename = $this->_formatFilename($filename, $album); + protected function _initFilename($url, $poster, $album, $response) { + $filename = ($type = $this->httpTypeFrom($response, ['image'])) + ? md5($url) . '.' . $type + : null; + + if (!$filename) { + $parts = explode('/', $url); + $filename = array_pop($parts); + $filename = $this->_formatFilename($filename, $album); + } + $temp_name = PATH_TEMP . $filename; if (false === $this->getFileWriter()->putContents($temp_name, $poster)) { diff --git a/library/Class/WebService/SimpleWebClient.php b/library/Class/WebService/SimpleWebClient.php index 1e0774569b1a9f182a6e0739d582f61f86a870de..5ec8ffc117230e3f8539a530c83e639aef7172f5 100644 --- a/library/Class/WebService/SimpleWebClient.php +++ b/library/Class/WebService/SimpleWebClient.php @@ -32,6 +32,11 @@ class Class_WebService_SimpleWebClient { public function open_url($url,$options = []) { + return $this->getResponse($url, $options)->getBody(); + } + + + public function getResponse($url, $options=[]) { $httpClient = $this->getHttpClient(); if (isset($options['headers'])) @@ -41,7 +46,7 @@ class Class_WebService_SimpleWebClient { $httpClient->setMethod(Zend_Http_Client::GET); $httpClient->setUri($url); - return $httpClient->request()->getBody(); + return $httpClient->request(); }