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
|
|
|
* @property-read \App\Controller\Component\FlashComponent $Flash |
24
|
|
|
* @property-read \App\Controller\Component\QueryComponent $Query |
25
|
|
|
*/ |
26
|
|
|
class ThumbsComponent extends Component |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
|
|
protected $_defaultConfig = [ |
32
|
|
|
'queryParams' => ['preset' => 'default'], |
33
|
|
|
'objectTypes' => ['images', 'videos'], |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Components |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $components = ['Flash', 'Query']; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retrieve thumbnails URL of related objects in `meta.url` if present. |
45
|
|
|
* |
46
|
|
|
* @param array|null $response Related objects response. |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function urls(?array &$response): void |
50
|
|
|
{ |
51
|
|
|
if (empty($response) || empty($response['data'])) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// extract ids of objects |
56
|
|
|
$types = $this->getConfig('objectTypes', []); |
57
|
|
|
$ids = (array)Hash::extract($response, sprintf('data.{n}[type=/%s/].id', join('|', $types))); |
58
|
|
|
if (empty($ids)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$thumbs = $this->getThumbs($ids); |
63
|
|
|
if ($thumbs === null) { |
64
|
|
|
// An error happened: let's try again by generating one thumbnail at a time. |
65
|
|
|
$thumbs = []; |
66
|
|
|
foreach ($ids as $id) { |
67
|
|
|
$thumbs += (array)$this->getThumbs([$id]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($response['data'] as &$object) { |
72
|
|
|
$thumbnail = Hash::get($object, 'attributes.provider_thumbnail'); |
73
|
|
|
// if provider_thumbnail is found there's no need to extract it from thumbsResponse |
74
|
|
|
if ($thumbnail) { |
75
|
|
|
$object['meta']['thumb_url'] = $thumbnail; |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// extract url of the matching objectid's thumb |
80
|
|
|
$thumbnail = Hash::get($thumbs, sprintf('%d.url', $object['id'])); |
81
|
|
|
if ($thumbnail !== null) { |
82
|
|
|
$object['meta']['thumb_url'] = $thumbnail; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Extract possible errors in creation of thumbnail(s) |
87
|
|
|
$errors = (array)Hash::extract($thumbs, '{*}[acceptable=false].message'); |
88
|
|
|
|
89
|
|
|
if (!empty($errors)) { |
90
|
|
|
$message = __('There were errors creating the thumbnail(s)'); |
91
|
|
|
if (count($errors) === 1) { |
92
|
|
|
$message = array_shift($errors); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->Flash->error($message, ['params' => $errors]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get thumbs by IDs |
101
|
|
|
* |
102
|
|
|
* @param array $ids The IDs |
103
|
|
|
* @return array|null |
104
|
|
|
*/ |
105
|
|
|
protected function getThumbs(array $ids): ?array |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
|
|
$params = $this->getController()->getRequest()->getQueryParams(); |
109
|
|
|
$query = $this->Query->prepare($params); |
110
|
|
|
$url = sprintf('/media/thumbs?%s', http_build_query([ |
111
|
|
|
'ids' => implode(',', $ids), |
112
|
|
|
] + $this->getConfig('queryParams', []))); |
113
|
|
|
$apiClient = ApiClientProvider::getApiClient(); |
114
|
|
|
$res = (array)$apiClient->get($url, $query); |
115
|
|
|
|
116
|
|
|
return Hash::combine($res, 'meta.thumbnails.{*}.id', 'meta.thumbnails.{*}'); |
117
|
|
|
} catch (BEditaClientException $e) { |
118
|
|
|
$this->getController()->log($e, 'error'); |
119
|
|
|
|
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|