|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2021 ChannelWeb Srl, Chialab Srl |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace App\Controller\Component; |
|
14
|
|
|
|
|
15
|
|
|
use BEdita\SDK\BEditaClientException; |
|
16
|
|
|
use BEdita\WebTools\ApiClientProvider; |
|
17
|
|
|
use Cake\Controller\Component; |
|
18
|
|
|
use Cake\Utility\Hash; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Handles thumbs. |
|
22
|
|
|
*/ |
|
23
|
|
|
class ThumbsComponent extends Component |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Retrieve thumbnails URL of related objects in `meta.url` if present. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array|null $response Related objects response. |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function urls(?array &$response): void |
|
32
|
|
|
{ |
|
33
|
|
|
if (empty($response) || empty($response['data'])) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// extract ids of objects |
|
38
|
|
|
$ids = (array)Hash::extract($response, 'data.{n}[type=/images|videos/].id'); |
|
39
|
|
|
if (empty($ids)) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$thumbs = $this->getThumbs($ids); |
|
44
|
|
|
if ($thumbs === null) { |
|
45
|
|
|
// An error happened: let's try again by generating one thumbnail at a time. |
|
46
|
|
|
$thumbs = []; |
|
47
|
|
|
foreach ($ids as $id) { |
|
48
|
|
|
$thumbs += (array)$this->getThumbs([$id]); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
foreach ($response['data'] as &$object) { |
|
53
|
|
|
$thumbnail = Hash::get($object, 'attributes.provider_thumbnail'); |
|
54
|
|
|
// if provider_thumbnail is found there's no need to extract it from thumbsResponse |
|
55
|
|
|
if ($thumbnail) { |
|
56
|
|
|
$object['meta']['thumb_url'] = $thumbnail; |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// extract url of the matching objectid's thumb |
|
61
|
|
|
$thumbnail = Hash::get($thumbs, $object['id']); |
|
62
|
|
|
if ($thumbnail !== null) { |
|
63
|
|
|
$object['meta']['thumb_url'] = $thumbnail; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get thumbs by IDs |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $ids The IDs |
|
72
|
|
|
* @return array|null |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getThumbs(array $ids): ?array |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
$params = $this->getController()->getRequest()->getQueryParams(); |
|
78
|
|
|
$query = $this->getController()->Query->prepare($params); |
|
79
|
|
|
$url = sprintf('/media/thumbs?%s', http_build_query([ |
|
80
|
|
|
'ids' => implode(',', $ids), |
|
81
|
|
|
'options' => ['w' => 400], |
|
82
|
|
|
])); |
|
83
|
|
|
$apiClient = ApiClientProvider::getApiClient(); |
|
84
|
|
|
$res = $apiClient->get($url, $query); |
|
85
|
|
|
|
|
86
|
|
|
return (array)Hash::combine($res, 'meta.thumbnails.{*}.id', 'meta.thumbnails.{*}.url'); |
|
|
|
|
|
|
87
|
|
|
} catch (BEditaClientException $e) { |
|
88
|
|
|
$this->getController()->log($e, 'error'); |
|
89
|
|
|
|
|
90
|
|
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|