1
0

Generator.php 18 KB

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