Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
88.89% |
8 / 9 |
CRAP | |
98.39% |
61 / 62 |
AttachmentThumbnail | |
0.00% |
0 / 1 |
|
88.89% |
8 / 9 |
35 | |
98.39% |
61 / 62 |
setAttachmentId | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
getAttachmentId | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
setMimetype | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
getMimetype | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
setWidth | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
getWidth | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
setHeight | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
getHeight | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
setFilename | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
getFilename | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
setModified | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
getModified | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
setAttachment | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getAttachment | |
100.00% |
1 / 1 |
3 | |
100.00% |
3 / 3 |
|||
getOrCreate | |
0.00% |
0 / 1 |
7 | |
96.97% |
32 / 33 |
|||
getPath | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getUrl | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getHTMLAttributes | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
delete | |
100.00% |
1 / 1 |
4 | |
100.00% |
7 / 7 |
|||
predictScalingValues | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
|||
schemaDef | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
1 | <?php |
2 | |
3 | // {{{ License |
4 | |
5 | // This file is part of GNU social - https://www.gnu.org/software/social |
6 | // |
7 | // GNU social is free software: you can redistribute it and/or modify |
8 | // it under the terms of the GNU Affero General Public License as published by |
9 | // the Free Software Foundation, either version 3 of the License, or |
10 | // (at your option) any later version. |
11 | // |
12 | // GNU social is distributed in the hope that it will be useful, |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | // GNU Affero General Public License for more details. |
16 | // |
17 | // You should have received a copy of the GNU Affero General Public License |
18 | // along with GNU social. If not, see <http://www.gnu.org/licenses/>. |
19 | |
20 | // }}} |
21 | |
22 | namespace App\Entity; |
23 | |
24 | use App\Core\Cache; |
25 | use App\Core\DB\DB; |
26 | use App\Core\Entity; |
27 | use App\Core\Event; |
28 | use App\Core\GSFile; |
29 | use function App\Core\I18n\_m; |
30 | use App\Core\Log; |
31 | use App\Core\Router\Router; |
32 | use App\Util\Common; |
33 | use App\Util\Exception\ClientException; |
34 | use App\Util\Exception\NotFoundException; |
35 | use App\Util\Exception\NotStoredLocallyException; |
36 | use App\Util\Exception\ServerException; |
37 | use DateTimeInterface; |
38 | use Symfony\Component\Mime\MimeTypes; |
39 | |
40 | /** |
41 | * Entity for Attachment thumbnails |
42 | * |
43 | * @category DB |
44 | * @package GNUsocial |
45 | * |
46 | * @author Zach Copley <zach@status.net> |
47 | * @copyright 2010 StatusNet Inc. |
48 | * @author Mikael Nordfeldth <mmn@hethane.se> |
49 | * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org |
50 | * @author Hugo Sales <hugo@hsal.es> |
51 | * @author Diogo Peralta Cordeiro <mail@diogo.site> |
52 | * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org |
53 | * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later |
54 | */ |
55 | class AttachmentThumbnail extends Entity |
56 | { |
57 | // {{{ Autocode |
58 | // @codeCoverageIgnoreStart |
59 | private int $attachment_id; |
60 | private ?string $mimetype; |
61 | private int $width; |
62 | private int $height; |
63 | private string $filename; |
64 | private \DateTimeInterface $modified; |
65 | |
66 | public function setAttachmentId(int $attachment_id): self |
67 | { |
68 | $this->attachment_id = $attachment_id; |
69 | return $this; |
70 | } |
71 | |
72 | public function getAttachmentId(): int |
73 | { |
74 | return $this->attachment_id; |
75 | } |
76 | |
77 | public function setMimetype(?string $mimetype): self |
78 | { |
79 | $this->mimetype = $mimetype; |
80 | return $this; |
81 | } |
82 | |
83 | public function getMimetype(): ?string |
84 | { |
85 | return $this->mimetype; |
86 | } |
87 | |
88 | public function setWidth(int $width): self |
89 | { |
90 | $this->width = $width; |
91 | return $this; |
92 | } |
93 | |
94 | public function getWidth(): int |
95 | { |
96 | return $this->width; |
97 | } |
98 | |
99 | public function setHeight(int $height): self |
100 | { |
101 | $this->height = $height; |
102 | return $this; |
103 | } |
104 | |
105 | public function getHeight(): int |
106 | { |
107 | return $this->height; |
108 | } |
109 | |
110 | public function setFilename(string $filename): self |
111 | { |
112 | $this->filename = $filename; |
113 | return $this; |
114 | } |
115 | |
116 | public function getFilename(): string |
117 | { |
118 | return $this->filename; |
119 | } |
120 | |
121 | public function setModified(DateTimeInterface $modified): self |
122 | { |
123 | $this->modified = $modified; |
124 | return $this; |
125 | } |
126 | |
127 | public function getModified(): DateTimeInterface |
128 | { |
129 | return $this->modified; |
130 | } |
131 | |
132 | // @codeCoverageIgnoreEnd |
133 | // }}} Autocode |
134 | |
135 | private ?Attachment $attachment = null; |
136 | |
137 | public function setAttachment(?Attachment $attachment) |
138 | { |
139 | $this->attachment = $attachment; |
140 | } |
141 | |
142 | public function getAttachment() |
143 | { |
144 | if (isset($this->attachment) && !is_null($this->attachment)) { |
145 | return $this->attachment; |
146 | } else { |
147 | return $this->attachment = DB::findOneBy('attachment', ['id' => $this->attachment_id]); |
148 | } |
149 | } |
150 | |
151 | /** |
152 | * @param Attachment $attachment |
153 | * @param int $width |
154 | * @param int $height |
155 | * @param bool $crop |
156 | * |
157 | * @throws ClientException |
158 | * @throws NotFoundException |
159 | * @throws ServerException |
160 | * |
161 | * @return mixed |
162 | */ |
163 | public static function getOrCreate(Attachment $attachment, int $width, int $height, bool $crop) |
164 | { |
165 | // We need to keep these in mind for DB indexing |
166 | $predicted_width = null; |
167 | $predicted_height = null; |
168 | try { |
169 | if (is_null($attachment->getWidth()) || is_null($attachment->getHeight())) { |
170 | // @codeCoverageIgnoreStart |
171 | // TODO: check if we can generate from an existing thumbnail |
172 | throw new ClientException(_m('Invalid dimensions requested for thumbnail.')); |
173 | // @codeCoverageIgnoreEnd |
174 | } |
175 | return Cache::get('thumb-' . $attachment->getId() . "-{$width}x{$height}", |
176 | function () use ($crop, $attachment, $width, $height, &$predicted_width, &$predicted_height) { |
177 | [$predicted_width, $predicted_height] = self::predictScalingValues($attachment->getWidth(), $attachment->getHeight(), $width, $height, $crop); |
178 | return DB::findOneBy('attachment_thumbnail', ['attachment_id' => $attachment->getId(), 'width' => $predicted_width, 'height' => $predicted_height]); |
179 | }); |
180 | } catch (NotFoundException $e) { |
181 | if (!file_exists($attachment->getPath())) { |
182 | throw new NotStoredLocallyException(); |
183 | } |
184 | $thumbnail = self::create(['attachment_id' => $attachment->getId()]); |
185 | $mimetype = $attachment->getMimetype(); |
186 | $event_map[$mimetype] = []; |
187 | $major_mime = GSFile::mimetypeMajor($mimetype); |
188 | $event_map[$major_mime] = []; |
189 | Event::handle('FileResizerAvailable', [&$event_map, $mimetype]); |
190 | // Always prefer specific encoders |
191 | $encoders = array_merge($event_map[$mimetype], $event_map[$major_mime]); |
192 | foreach ($encoders as $encoder) { |
193 | $temp = null; // Let the EncoderPlugin create a temporary file for us |
194 | if ($encoder($attachment->getPath(), $temp, $width, $height, $crop, $mimetype)) { |
195 | $thumbnail->setAttachment($attachment); |
196 | $thumbnail->setWidth($predicted_width); |
197 | $thumbnail->setHeight($predicted_height); |
198 | $ext = '.' . MimeTypes::getDefault()->getExtensions($temp->getMimeType())[0]; |
199 | $filename = "{$predicted_width}x{$predicted_height}{$ext}-" . $attachment->getFilehash(); |
200 | $thumbnail->setFilename($filename); |
201 | $thumbnail->setMimetype($mimetype); |
202 | DB::persist($thumbnail); |
203 | DB::flush(); |
204 | $temp->move(Common::config('thumbnail', 'dir'), $filename); |
205 | return $thumbnail; |
206 | } |
207 | } |
208 | throw new ClientException(_m('Can not generate thumbnail for attachment with id={id}', ['id' => $attachment->getId()])); |
209 | } |
210 | } |
211 | |
212 | public function getPath() |
213 | { |
214 | return Common::config('thumbnail', 'dir') . DIRECTORY_SEPARATOR . $this->getFilename(); |
215 | } |
216 | |
217 | public function getUrl() |
218 | { |
219 | return Router::url('attachment_thumbnail', ['id' => $this->getAttachmentId(), 'w' => $this->getWidth(), 'h' => $this->getHeight()]); |
220 | } |
221 | |
222 | /** |
223 | * Get the HTML attributes for this thumbnail |
224 | */ |
225 | public function getHTMLAttributes(array $orig = [], bool $overwrite = true) |
226 | { |
227 | $attrs = [ |
228 | 'height' => $this->getHeight(), |
229 | 'width' => $this->getWidth(), |
230 | 'src' => $this->getUrl(), |
231 | ]; |
232 | return $overwrite ? array_merge($orig, $attrs) : array_merge($attrs, $orig); |
233 | } |
234 | |
235 | /** |
236 | * Delete an attachment thumbnail |
237 | */ |
238 | public function delete(bool $flush = true): void |
239 | { |
240 | $filepath = $this->getPath(); |
241 | if (file_exists($filepath)) { |
242 | if (@unlink($filepath) === false) { |
243 | // @codeCoverageIgnoreStart |
244 | Log::warning("Failed deleting file for attachment thumbnail with id={$this->attachment_id}, width={$this->width}, height={$this->height} at {$filepath}"); |
245 | // @codeCoverageIgnoreEnd |
246 | } |
247 | } |
248 | DB::remove($this); |
249 | if ($flush) { |
250 | DB::flush(); |
251 | } |
252 | } |
253 | |
254 | /** |
255 | * Gets scaling values for images of various types. Cropping can be enabled. |
256 | * |
257 | * Values will scale _up_ to fit max values if cropping is enabled! |
258 | * With cropping disabled, the max value of each axis will be respected. |
259 | * |
260 | * @param $width int Original width |
261 | * @param $height int Original height |
262 | * @param $maxW int Resulting max width |
263 | * @param $maxH int Resulting max height |
264 | * @param $crop bool Crop to the size (not preserving aspect ratio) |
265 | * |
266 | * @return array [predicted width, predicted height] |
267 | */ |
268 | public static function predictScalingValues( |
269 | int $existing_width, |
270 | int $existing_height, |
271 | int $requested_width, |
272 | int $requested_height, |
273 | bool $crop |
274 | ): array { |
275 | if ($crop) { |
276 | $rw = min($existing_width, $requested_width); |
277 | $rh = min($existing_height, $requested_height); |
278 | } else { |
279 | if ($existing_width > $existing_height) { |
280 | $rw = min($existing_width, $requested_width); |
281 | $rh = ceil($existing_height * $rw / $existing_width); |
282 | } else { |
283 | $rh = min($existing_height, $requested_height); |
284 | $rw = ceil($existing_width * $rh / $existing_height); |
285 | } |
286 | } |
287 | |
288 | return [(int) $rw, (int) $rh]; |
289 | } |
290 | |
291 | public static function schemaDef(): array |
292 | { |
293 | return [ |
294 | 'name' => 'attachment_thumbnail', |
295 | 'fields' => [ |
296 | 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'thumbnail for what attachment'], |
297 | 'mimetype' => ['type' => 'varchar', 'length' => 129, 'description' => 'resource mime type 64+1+64, images hardly will show up with long mimetypes, this is probably safe considering rfc6838#section-4.2'], |
298 | 'width' => ['type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'], |
299 | 'height' => ['type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'], |
300 | 'filename' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'thumbnail filename'], |
301 | 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], |
302 | ], |
303 | 'primary key' => ['attachment_id', 'width', 'height'], |
304 | 'indexes' => [ |
305 | 'attachment_thumbnail_attachment_id_idx' => ['attachment_id'], |
306 | ], |
307 | ]; |
308 | } |
309 | } |