Generator.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Elijah Martin-Merrill <elijah@nyp-itsours.com>
  7. * @author J0WI <J0WI@users.noreply.github.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Scott Dutton <scott@exussum.co.uk>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OC\Preview;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\Files\File;
  33. use OCP\Files\IAppData;
  34. use OCP\Files\InvalidPathException;
  35. use OCP\Files\NotFoundException;
  36. use OCP\Files\NotPermittedException;
  37. use OCP\Files\SimpleFS\ISimpleFile;
  38. use OCP\Files\SimpleFS\ISimpleFolder;
  39. use OCP\IConfig;
  40. use OCP\IImage;
  41. use OCP\IPreview;
  42. use OCP\IStreamImage;
  43. use OCP\Preview\BeforePreviewFetchedEvent;
  44. use OCP\Preview\IProviderV2;
  45. use OCP\Preview\IVersionedPreviewFile;
  46. class Generator {
  47. public const SEMAPHORE_ID_ALL = 0x0a11;
  48. public const SEMAPHORE_ID_NEW = 0x07ea;
  49. /** @var IPreview */
  50. private $previewManager;
  51. /** @var IConfig */
  52. private $config;
  53. /** @var IAppData */
  54. private $appData;
  55. /** @var GeneratorHelper */
  56. private $helper;
  57. /** @var IEventDispatcher */
  58. private $eventDispatcher;
  59. public function __construct(
  60. IConfig $config,
  61. IPreview $previewManager,
  62. IAppData $appData,
  63. GeneratorHelper $helper,
  64. IEventDispatcher $eventDispatcher
  65. ) {
  66. $this->config = $config;
  67. $this->previewManager = $previewManager;
  68. $this->appData = $appData;
  69. $this->helper = $helper;
  70. $this->eventDispatcher = $eventDispatcher;
  71. }
  72. /**
  73. * Returns a preview of a file
  74. *
  75. * The cache is searched first and if nothing usable was found then a preview is
  76. * generated by one of the providers
  77. *
  78. * @param File $file
  79. * @param int $width
  80. * @param int $height
  81. * @param bool $crop
  82. * @param string $mode
  83. * @param string|null $mimeType
  84. * @return ISimpleFile
  85. * @throws NotFoundException
  86. * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
  87. */
  88. public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
  89. $specification = [
  90. 'width' => $width,
  91. 'height' => $height,
  92. 'crop' => $crop,
  93. 'mode' => $mode,
  94. ];
  95. $this->eventDispatcher->dispatchTyped(new BeforePreviewFetchedEvent(
  96. $file,
  97. $width,
  98. $height,
  99. $crop,
  100. $mode,
  101. ));
  102. // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
  103. return $this->generatePreviews($file, [$specification], $mimeType);
  104. }
  105. /**
  106. * Generates previews of a file
  107. *
  108. * @param File $file
  109. * @param non-empty-array $specifications
  110. * @param string $mimeType
  111. * @return ISimpleFile the last preview that was generated
  112. * @throws NotFoundException
  113. * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
  114. */
  115. public function generatePreviews(File $file, array $specifications, $mimeType = null) {
  116. //Make sure that we can read the file
  117. if (!$file->isReadable()) {
  118. throw new NotFoundException('Cannot read file');
  119. }
  120. if ($mimeType === null) {
  121. $mimeType = $file->getMimeType();
  122. }
  123. $previewFolder = $this->getPreviewFolder($file);
  124. // List every existing preview first instead of trying to find them one by one
  125. $previewFiles = $previewFolder->getDirectoryListing();
  126. $previewVersion = '';
  127. if ($file instanceof IVersionedPreviewFile) {
  128. $previewVersion = $file->getPreviewVersion() . '-';
  129. }
  130. // Get the max preview and infer the max preview sizes from that
  131. $maxPreview = $this->getMaxPreview($previewFolder, $previewFiles, $file, $mimeType, $previewVersion);
  132. $maxPreviewImage = null; // only load the image when we need it
  133. if ($maxPreview->getSize() === 0) {
  134. $maxPreview->delete();
  135. throw new NotFoundException('Max preview size 0, invalid!');
  136. }
  137. [$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion);
  138. if ($maxWidth <= 0 || $maxHeight <= 0) {
  139. throw new NotFoundException('The maximum preview sizes are zero or less pixels');
  140. }
  141. $preview = null;
  142. foreach ($specifications as $specification) {
  143. $width = $specification['width'] ?? -1;
  144. $height = $specification['height'] ?? -1;
  145. $crop = $specification['crop'] ?? false;
  146. $mode = $specification['mode'] ?? IPreview::MODE_FILL;
  147. // If both width and height are -1 we just want the max preview
  148. if ($width === -1 && $height === -1) {
  149. $width = $maxWidth;
  150. $height = $maxHeight;
  151. }
  152. // Calculate the preview size
  153. [$width, $height] = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
  154. // No need to generate a preview that is just the max preview
  155. if ($width === $maxWidth && $height === $maxHeight) {
  156. // ensure correct return value if this was the last one
  157. $preview = $maxPreview;
  158. continue;
  159. }
  160. // Try to get a cached preview. Else generate (and store) one
  161. try {
  162. try {
  163. $preview = $this->getCachedPreview($previewFiles, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
  164. } catch (NotFoundException $e) {
  165. if (!$this->previewManager->isMimeSupported($mimeType)) {
  166. throw new NotFoundException();
  167. }
  168. if ($maxPreviewImage === null) {
  169. $maxPreviewImage = $this->helper->getImage($maxPreview);
  170. }
  171. $preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
  172. // New file, augment our array
  173. $previewFiles[] = $preview;
  174. }
  175. } catch (\InvalidArgumentException $e) {
  176. throw new NotFoundException("", 0, $e);
  177. }
  178. if ($preview->getSize() === 0) {
  179. $preview->delete();
  180. throw new NotFoundException('Cached preview size 0, invalid!');
  181. }
  182. }
  183. assert($preview !== null);
  184. // Free memory being used by the embedded image resource. Without this the image is kept in memory indefinitely.
  185. // Garbage Collection does NOT free this memory. We have to do it ourselves.
  186. if ($maxPreviewImage instanceof \OCP\Image) {
  187. $maxPreviewImage->destroy();
  188. }
  189. return $preview;
  190. }
  191. /**
  192. * Acquire a semaphore of the specified id and concurrency, blocking if necessary.
  193. * Return an identifier of the semaphore on success, which can be used to release it via
  194. * {@see Generator::unguardWithSemaphore()}.
  195. *
  196. * @param int $semId
  197. * @param int $concurrency
  198. * @return false|\SysvSemaphore the semaphore on success or false on failure
  199. */
  200. public static function guardWithSemaphore(int $semId, int $concurrency) {
  201. if (!extension_loaded('sysvsem')) {
  202. return false;
  203. }
  204. $sem = sem_get($semId, $concurrency);
  205. if ($sem === false) {
  206. return false;
  207. }
  208. if (!sem_acquire($sem)) {
  209. return false;
  210. }
  211. return $sem;
  212. }
  213. /**
  214. * Releases the semaphore acquired from {@see Generator::guardWithSemaphore()}.
  215. *
  216. * @param false|\SysvSemaphore $semId the semaphore identifier returned by guardWithSemaphore
  217. * @return bool
  218. */
  219. public static function unguardWithSemaphore(false|\SysvSemaphore $semId): bool {
  220. if ($semId === false || !($semId instanceof \SysvSemaphore)) {
  221. return false;
  222. }
  223. return sem_release($semId);
  224. }
  225. /**
  226. * Get the number of concurrent threads supported by the host.
  227. *
  228. * @return int number of concurrent threads, or 0 if it cannot be determined
  229. */
  230. public static function getHardwareConcurrency(): int {
  231. static $width;
  232. if (!isset($width)) {
  233. if (function_exists('ini_get')) {
  234. $openBasedir = ini_get('open_basedir');
  235. if (empty($openBasedir) || strpos($openBasedir, '/proc/cpuinfo') !== false) {
  236. $width = is_readable('/proc/cpuinfo') ? substr_count(file_get_contents('/proc/cpuinfo'), 'processor') : 0;
  237. } else {
  238. $width = 0;
  239. }
  240. } else {
  241. $width = 0;
  242. }
  243. }
  244. return $width;
  245. }
  246. /**
  247. * Get number of concurrent preview generations from system config
  248. *
  249. * Two config entries, `preview_concurrency_new` and `preview_concurrency_all`,
  250. * are available. If not set, the default values are determined with the hardware concurrency
  251. * of the host. In case the hardware concurrency cannot be determined, or the user sets an
  252. * invalid value, fallback values are:
  253. * For new images whose previews do not exist and need to be generated, 4;
  254. * For all preview generation requests, 8.
  255. * Value of `preview_concurrency_all` should be greater than or equal to that of
  256. * `preview_concurrency_new`, otherwise, the latter is returned.
  257. *
  258. * @param string $type either `preview_concurrency_new` or `preview_concurrency_all`
  259. * @return int number of concurrent preview generations, or -1 if $type is invalid
  260. */
  261. public function getNumConcurrentPreviews(string $type): int {
  262. static $cached = [];
  263. if (array_key_exists($type, $cached)) {
  264. return $cached[$type];
  265. }
  266. $hardwareConcurrency = self::getHardwareConcurrency();
  267. switch ($type) {
  268. case "preview_concurrency_all":
  269. $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency * 2 : 8;
  270. $concurrency_all = $this->config->getSystemValueInt($type, $fallback);
  271. $concurrency_new = $this->getNumConcurrentPreviews("preview_concurrency_new");
  272. $cached[$type] = max($concurrency_all, $concurrency_new);
  273. break;
  274. case "preview_concurrency_new":
  275. $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency : 4;
  276. $cached[$type] = $this->config->getSystemValueInt($type, $fallback);
  277. break;
  278. default:
  279. return -1;
  280. }
  281. return $cached[$type];
  282. }
  283. /**
  284. * @param ISimpleFolder $previewFolder
  285. * @param ISimpleFile[] $previewFiles
  286. * @param File $file
  287. * @param string $mimeType
  288. * @param string $prefix
  289. * @return ISimpleFile
  290. * @throws NotFoundException
  291. */
  292. private function getMaxPreview(ISimpleFolder $previewFolder, array $previewFiles, File $file, $mimeType, $prefix) {
  293. // We don't know the max preview size, so we can't use getCachedPreview.
  294. // It might have been generated with a higher resolution than the current value.
  295. foreach ($previewFiles as $node) {
  296. $name = $node->getName();
  297. if (($prefix === '' || str_starts_with($name, $prefix)) && strpos($name, 'max')) {
  298. return $node;
  299. }
  300. }
  301. $maxWidth = $this->config->getSystemValueInt('preview_max_x', 4096);
  302. $maxHeight = $this->config->getSystemValueInt('preview_max_y', 4096);
  303. return $this->generateProviderPreview($previewFolder, $file, $maxWidth, $maxHeight, false, true, $mimeType, $prefix);
  304. }
  305. private function generateProviderPreview(ISimpleFolder $previewFolder, File $file, int $width, int $height, bool $crop, bool $max, string $mimeType, string $prefix) {
  306. $previewProviders = $this->previewManager->getProviders();
  307. foreach ($previewProviders as $supportedMimeType => $providers) {
  308. // Filter out providers that does not support this mime
  309. if (!preg_match($supportedMimeType, $mimeType)) {
  310. continue;
  311. }
  312. foreach ($providers as $providerClosure) {
  313. $provider = $this->helper->getProvider($providerClosure);
  314. if (!($provider instanceof IProviderV2)) {
  315. continue;
  316. }
  317. if (!$provider->isAvailable($file)) {
  318. continue;
  319. }
  320. $previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new');
  321. $sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);
  322. try {
  323. $preview = $this->helper->getThumbnail($provider, $file, $width, $height);
  324. } finally {
  325. self::unguardWithSemaphore($sem);
  326. }
  327. if (!($preview instanceof IImage)) {
  328. continue;
  329. }
  330. $path = $this->generatePath($preview->width(), $preview->height(), $crop, $max, $preview->dataMimeType(), $prefix);
  331. try {
  332. $file = $previewFolder->newFile($path);
  333. if ($preview instanceof IStreamImage) {
  334. $file->putContent($preview->resource());
  335. } else {
  336. $file->putContent($preview->data());
  337. }
  338. } catch (NotPermittedException $e) {
  339. throw new NotFoundException();
  340. }
  341. return $file;
  342. }
  343. }
  344. throw new NotFoundException('No provider successfully handled the preview generation');
  345. }
  346. /**
  347. * @param ISimpleFile $file
  348. * @param string $prefix
  349. * @return int[]
  350. */
  351. private function getPreviewSize(ISimpleFile $file, string $prefix = '') {
  352. $size = explode('-', substr($file->getName(), strlen($prefix)));
  353. return [(int)$size[0], (int)$size[1]];
  354. }
  355. /**
  356. * @param int $width
  357. * @param int $height
  358. * @param bool $crop
  359. * @param bool $max
  360. * @param string $mimeType
  361. * @param string $prefix
  362. * @return string
  363. */
  364. private function generatePath($width, $height, $crop, $max, $mimeType, $prefix) {
  365. $path = $prefix . (string)$width . '-' . (string)$height;
  366. if ($crop) {
  367. $path .= '-crop';
  368. }
  369. if ($max) {
  370. $path .= '-max';
  371. }
  372. $ext = $this->getExtension($mimeType);
  373. $path .= '.' . $ext;
  374. return $path;
  375. }
  376. /**
  377. * @param int $width
  378. * @param int $height
  379. * @param bool $crop
  380. * @param string $mode
  381. * @param int $maxWidth
  382. * @param int $maxHeight
  383. * @return int[]
  384. */
  385. private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
  386. /*
  387. * If we are not cropping we have to make sure the requested image
  388. * respects the aspect ratio of the original.
  389. */
  390. if (!$crop) {
  391. $ratio = $maxHeight / $maxWidth;
  392. if ($width === -1) {
  393. $width = $height / $ratio;
  394. }
  395. if ($height === -1) {
  396. $height = $width * $ratio;
  397. }
  398. $ratioH = $height / $maxHeight;
  399. $ratioW = $width / $maxWidth;
  400. /*
  401. * Fill means that the $height and $width are the max
  402. * Cover means min.
  403. */
  404. if ($mode === IPreview::MODE_FILL) {
  405. if ($ratioH > $ratioW) {
  406. $height = $width * $ratio;
  407. } else {
  408. $width = $height / $ratio;
  409. }
  410. } elseif ($mode === IPreview::MODE_COVER) {
  411. if ($ratioH > $ratioW) {
  412. $width = $height / $ratio;
  413. } else {
  414. $height = $width * $ratio;
  415. }
  416. }
  417. }
  418. if ($height !== $maxHeight && $width !== $maxWidth) {
  419. /*
  420. * Scale to the nearest power of four
  421. */
  422. $pow4height = 4 ** ceil(log($height) / log(4));
  423. $pow4width = 4 ** ceil(log($width) / log(4));
  424. // Minimum size is 64
  425. $pow4height = max($pow4height, 64);
  426. $pow4width = max($pow4width, 64);
  427. $ratioH = $height / $pow4height;
  428. $ratioW = $width / $pow4width;
  429. if ($ratioH < $ratioW) {
  430. $width = $pow4width;
  431. $height /= $ratioW;
  432. } else {
  433. $height = $pow4height;
  434. $width /= $ratioH;
  435. }
  436. }
  437. /*
  438. * Make sure the requested height and width fall within the max
  439. * of the preview.
  440. */
  441. if ($height > $maxHeight) {
  442. $ratio = $height / $maxHeight;
  443. $height = $maxHeight;
  444. $width /= $ratio;
  445. }
  446. if ($width > $maxWidth) {
  447. $ratio = $width / $maxWidth;
  448. $width = $maxWidth;
  449. $height /= $ratio;
  450. }
  451. return [(int)round($width), (int)round($height)];
  452. }
  453. /**
  454. * @param ISimpleFolder $previewFolder
  455. * @param ISimpleFile $maxPreview
  456. * @param int $width
  457. * @param int $height
  458. * @param bool $crop
  459. * @param int $maxWidth
  460. * @param int $maxHeight
  461. * @param string $prefix
  462. * @return ISimpleFile
  463. * @throws NotFoundException
  464. * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
  465. */
  466. private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) {
  467. $preview = $maxPreview;
  468. if (!$preview->valid()) {
  469. throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
  470. }
  471. $previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new');
  472. $sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);
  473. try {
  474. if ($crop) {
  475. if ($height !== $preview->height() && $width !== $preview->width()) {
  476. //Resize
  477. $widthR = $preview->width() / $width;
  478. $heightR = $preview->height() / $height;
  479. if ($widthR > $heightR) {
  480. $scaleH = $height;
  481. $scaleW = $maxWidth / $heightR;
  482. } else {
  483. $scaleH = $maxHeight / $widthR;
  484. $scaleW = $width;
  485. }
  486. $preview = $preview->preciseResizeCopy((int)round($scaleW), (int)round($scaleH));
  487. }
  488. $cropX = (int)floor(abs($width - $preview->width()) * 0.5);
  489. $cropY = (int)floor(abs($height - $preview->height()) * 0.5);
  490. $preview = $preview->cropCopy($cropX, $cropY, $width, $height);
  491. } else {
  492. $preview = $maxPreview->resizeCopy(max($width, $height));
  493. }
  494. } finally {
  495. self::unguardWithSemaphore($sem);
  496. }
  497. $path = $this->generatePath($width, $height, $crop, false, $preview->dataMimeType(), $prefix);
  498. try {
  499. $file = $previewFolder->newFile($path);
  500. $file->putContent($preview->data());
  501. } catch (NotPermittedException $e) {
  502. throw new NotFoundException();
  503. }
  504. return $file;
  505. }
  506. /**
  507. * @param ISimpleFile[] $files Array of FileInfo, as the result of getDirectoryListing()
  508. * @param int $width
  509. * @param int $height
  510. * @param bool $crop
  511. * @param string $mimeType
  512. * @param string $prefix
  513. * @return ISimpleFile
  514. *
  515. * @throws NotFoundException
  516. */
  517. private function getCachedPreview($files, $width, $height, $crop, $mimeType, $prefix) {
  518. $path = $this->generatePath($width, $height, $crop, false, $mimeType, $prefix);
  519. foreach ($files as $file) {
  520. if ($file->getName() === $path) {
  521. return $file;
  522. }
  523. }
  524. throw new NotFoundException();
  525. }
  526. /**
  527. * Get the specific preview folder for this file
  528. *
  529. * @param File $file
  530. * @return ISimpleFolder
  531. *
  532. * @throws InvalidPathException
  533. * @throws NotFoundException
  534. * @throws NotPermittedException
  535. */
  536. private function getPreviewFolder(File $file) {
  537. // Obtain file id outside of try catch block to prevent the creation of an existing folder
  538. $fileId = (string)$file->getId();
  539. try {
  540. $folder = $this->appData->getFolder($fileId);
  541. } catch (NotFoundException $e) {
  542. $folder = $this->appData->newFolder($fileId);
  543. }
  544. return $folder;
  545. }
  546. /**
  547. * @param string $mimeType
  548. * @return null|string
  549. * @throws \InvalidArgumentException
  550. */
  551. private function getExtension($mimeType) {
  552. switch ($mimeType) {
  553. case 'image/png':
  554. return 'png';
  555. case 'image/jpeg':
  556. return 'jpg';
  557. case 'image/webp':
  558. return 'webp';
  559. case 'image/gif':
  560. return 'gif';
  561. default:
  562. throw new \InvalidArgumentException('Not a valid mimetype: "' . $mimeType . '"');
  563. }
  564. }
  565. }