Generator.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. // If imaginary is enabled, and we request a small thumbnail,
  131. // let's not generate the max preview for performance reasons
  132. if (count($specifications) === 1
  133. && ($specifications[0]['width'] <= 256 || $specifications[0]['height'] <= 256)
  134. && preg_match(Imaginary::supportedMimeTypes(), $mimeType)
  135. && $this->config->getSystemValueString('preview_imaginary_url', 'invalid') !== 'invalid') {
  136. $crop = $specifications[0]['crop'] ?? false;
  137. $preview = $this->getSmallImagePreview($previewFolder, $previewFiles, $file, $mimeType, $previewVersion, $crop);
  138. if ($preview->getSize() === 0) {
  139. $preview->delete();
  140. throw new NotFoundException('Cached preview size 0, invalid!');
  141. }
  142. return $preview;
  143. }
  144. // Get the max preview and infer the max preview sizes from that
  145. $maxPreview = $this->getMaxPreview($previewFolder, $previewFiles, $file, $mimeType, $previewVersion);
  146. $maxPreviewImage = null; // only load the image when we need it
  147. if ($maxPreview->getSize() === 0) {
  148. $maxPreview->delete();
  149. throw new NotFoundException('Max preview size 0, invalid!');
  150. }
  151. [$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion);
  152. if ($maxWidth <= 0 || $maxHeight <= 0) {
  153. throw new NotFoundException('The maximum preview sizes are zero or less pixels');
  154. }
  155. $preview = null;
  156. foreach ($specifications as $specification) {
  157. $width = $specification['width'] ?? -1;
  158. $height = $specification['height'] ?? -1;
  159. $crop = $specification['crop'] ?? false;
  160. $mode = $specification['mode'] ?? IPreview::MODE_FILL;
  161. // If both width and height are -1 we just want the max preview
  162. if ($width === -1 && $height === -1) {
  163. $width = $maxWidth;
  164. $height = $maxHeight;
  165. }
  166. // Calculate the preview size
  167. [$width, $height] = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
  168. // No need to generate a preview that is just the max preview
  169. if ($width === $maxWidth && $height === $maxHeight) {
  170. // ensure correct return value if this was the last one
  171. $preview = $maxPreview;
  172. continue;
  173. }
  174. // Try to get a cached preview. Else generate (and store) one
  175. try {
  176. try {
  177. $preview = $this->getCachedPreview($previewFiles, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
  178. } catch (NotFoundException $e) {
  179. if (!$this->previewManager->isMimeSupported($mimeType)) {
  180. throw new NotFoundException();
  181. }
  182. if ($maxPreviewImage === null) {
  183. $maxPreviewImage = $this->helper->getImage($maxPreview);
  184. }
  185. $preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
  186. // New file, augment our array
  187. $previewFiles[] = $preview;
  188. }
  189. } catch (\InvalidArgumentException $e) {
  190. throw new NotFoundException("", 0, $e);
  191. }
  192. if ($preview->getSize() === 0) {
  193. $preview->delete();
  194. throw new NotFoundException('Cached preview size 0, invalid!');
  195. }
  196. }
  197. assert($preview !== null);
  198. // Free memory being used by the embedded image resource. Without this the image is kept in memory indefinitely.
  199. // Garbage Collection does NOT free this memory. We have to do it ourselves.
  200. if ($maxPreviewImage instanceof \OCP\Image) {
  201. $maxPreviewImage->destroy();
  202. }
  203. return $preview;
  204. }
  205. /**
  206. * Generate a small image straight away without generating a max preview first
  207. * Preview generated is 256x256
  208. *
  209. * @param ISimpleFile[] $previewFiles
  210. *
  211. * @throws NotFoundException
  212. */
  213. private function getSmallImagePreview(ISimpleFolder $previewFolder, array $previewFiles, File $file, string $mimeType, string $prefix, bool $crop): ISimpleFile {
  214. $width = 256;
  215. $height = 256;
  216. try {
  217. return $this->getCachedPreview($previewFiles, $width, $height, $crop, $mimeType, $prefix);
  218. } catch (NotFoundException $e) {
  219. return $this->generateProviderPreview($previewFolder, $file, $width, $height, $crop, false, $mimeType, $prefix);
  220. }
  221. }
  222. /**
  223. * Acquire a semaphore of the specified id and concurrency, blocking if necessary.
  224. * Return an identifier of the semaphore on success, which can be used to release it via
  225. * {@see Generator::unguardWithSemaphore()}.
  226. *
  227. * @param int $semId
  228. * @param int $concurrency
  229. * @return false|resource the semaphore on success or false on failure
  230. */
  231. public static function guardWithSemaphore(int $semId, int $concurrency) {
  232. if (!extension_loaded('sysvsem')) {
  233. return false;
  234. }
  235. $sem = sem_get($semId, $concurrency);
  236. if ($sem === false) {
  237. return false;
  238. }
  239. if (!sem_acquire($sem)) {
  240. return false;
  241. }
  242. return $sem;
  243. }
  244. /**
  245. * Releases the semaphore acquired from {@see Generator::guardWithSemaphore()}.
  246. *
  247. * @param resource|bool $semId the semaphore identifier returned by guardWithSemaphore
  248. * @return bool
  249. */
  250. public static function unguardWithSemaphore($semId): bool {
  251. if (!is_resource($semId) || !extension_loaded('sysvsem')) {
  252. return false;
  253. }
  254. return sem_release($semId);
  255. }
  256. /**
  257. * Get the number of concurrent threads supported by the host.
  258. *
  259. * @return int number of concurrent threads, or 0 if it cannot be determined
  260. */
  261. public static function getHardwareConcurrency(): int {
  262. static $width;
  263. if (!isset($width)) {
  264. if (is_file("/proc/cpuinfo")) {
  265. $width = substr_count(file_get_contents("/proc/cpuinfo"), "processor");
  266. } else {
  267. $width = 0;
  268. }
  269. }
  270. return $width;
  271. }
  272. /**
  273. * Get number of concurrent preview generations from system config
  274. *
  275. * Two config entries, `preview_concurrency_new` and `preview_concurrency_all`,
  276. * are available. If not set, the default values are determined with the hardware concurrency
  277. * of the host. In case the hardware concurrency cannot be determined, or the user sets an
  278. * invalid value, fallback values are:
  279. * For new images whose previews do not exist and need to be generated, 4;
  280. * For all preview generation requests, 8.
  281. * Value of `preview_concurrency_all` should be greater than or equal to that of
  282. * `preview_concurrency_new`, otherwise, the latter is returned.
  283. *
  284. * @param string $type either `preview_concurrency_new` or `preview_concurrency_all`
  285. * @return int number of concurrent preview generations, or -1 if $type is invalid
  286. */
  287. public function getNumConcurrentPreviews(string $type): int {
  288. static $cached = array();
  289. if (array_key_exists($type, $cached)) {
  290. return $cached[$type];
  291. }
  292. $hardwareConcurrency = self::getHardwareConcurrency();
  293. switch ($type) {
  294. case "preview_concurrency_all":
  295. $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency * 2 : 8;
  296. $concurrency_all = $this->config->getSystemValueInt($type, $fallback);
  297. $concurrency_new = $this->getNumConcurrentPreviews("preview_concurrency_new");
  298. $cached[$type] = max($concurrency_all, $concurrency_new);
  299. break;
  300. case "preview_concurrency_new":
  301. $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency : 4;
  302. $cached[$type] = $this->config->getSystemValueInt($type, $fallback);
  303. break;
  304. default:
  305. return -1;
  306. }
  307. return $cached[$type];
  308. }
  309. /**
  310. * @param ISimpleFolder $previewFolder
  311. * @param ISimpleFile[] $previewFiles
  312. * @param File $file
  313. * @param string $mimeType
  314. * @param string $prefix
  315. * @return ISimpleFile
  316. * @throws NotFoundException
  317. */
  318. private function getMaxPreview(ISimpleFolder $previewFolder, array $previewFiles, File $file, $mimeType, $prefix) {
  319. // We don't know the max preview size, so we can't use getCachedPreview.
  320. // It might have been generated with a higher resolution than the current value.
  321. foreach ($previewFiles as $node) {
  322. $name = $node->getName();
  323. if (($prefix === '' || str_starts_with($name, $prefix)) && strpos($name, 'max')) {
  324. return $node;
  325. }
  326. }
  327. $maxWidth = $this->config->getSystemValueInt('preview_max_x', 4096);
  328. $maxHeight = $this->config->getSystemValueInt('preview_max_y', 4096);
  329. return $this->generateProviderPreview($previewFolder, $file, $maxWidth, $maxHeight, false, true, $mimeType, $prefix);
  330. }
  331. private function generateProviderPreview(ISimpleFolder $previewFolder, File $file, int $width, int $height, bool $crop, bool $max, string $mimeType, string $prefix) {
  332. $previewProviders = $this->previewManager->getProviders();
  333. foreach ($previewProviders as $supportedMimeType => $providers) {
  334. // Filter out providers that does not support this mime
  335. if (!preg_match($supportedMimeType, $mimeType)) {
  336. continue;
  337. }
  338. foreach ($providers as $providerClosure) {
  339. $provider = $this->helper->getProvider($providerClosure);
  340. if (!($provider instanceof IProviderV2)) {
  341. continue;
  342. }
  343. if (!$provider->isAvailable($file)) {
  344. continue;
  345. }
  346. $previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new');
  347. $sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);
  348. try {
  349. $preview = $this->helper->getThumbnail($provider, $file, $width, $height);
  350. } finally {
  351. self::unguardWithSemaphore($sem);
  352. }
  353. if (!($preview instanceof IImage)) {
  354. continue;
  355. }
  356. $path = $this->generatePath($preview->width(), $preview->height(), $crop, $max, $preview->dataMimeType(), $prefix);
  357. try {
  358. $file = $previewFolder->newFile($path);
  359. if ($preview instanceof IStreamImage) {
  360. $file->putContent($preview->resource());
  361. } else {
  362. $file->putContent($preview->data());
  363. }
  364. } catch (NotPermittedException $e) {
  365. throw new NotFoundException();
  366. }
  367. return $file;
  368. }
  369. }
  370. throw new NotFoundException('No provider successfully handled the preview generation');
  371. }
  372. /**
  373. * @param ISimpleFile $file
  374. * @param string $prefix
  375. * @return int[]
  376. */
  377. private function getPreviewSize(ISimpleFile $file, string $prefix = '') {
  378. $size = explode('-', substr($file->getName(), strlen($prefix)));
  379. return [(int)$size[0], (int)$size[1]];
  380. }
  381. /**
  382. * @param int $width
  383. * @param int $height
  384. * @param bool $crop
  385. * @param bool $max
  386. * @param string $mimeType
  387. * @param string $prefix
  388. * @return string
  389. */
  390. private function generatePath($width, $height, $crop, $max, $mimeType, $prefix) {
  391. $path = $prefix . (string)$width . '-' . (string)$height;
  392. if ($crop) {
  393. $path .= '-crop';
  394. }
  395. if ($max) {
  396. $path .= '-max';
  397. }
  398. $ext = $this->getExtension($mimeType);
  399. $path .= '.' . $ext;
  400. return $path;
  401. }
  402. /**
  403. * @param int $width
  404. * @param int $height
  405. * @param bool $crop
  406. * @param string $mode
  407. * @param int $maxWidth
  408. * @param int $maxHeight
  409. * @return int[]
  410. */
  411. private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
  412. /*
  413. * If we are not cropping we have to make sure the requested image
  414. * respects the aspect ratio of the original.
  415. */
  416. if (!$crop) {
  417. $ratio = $maxHeight / $maxWidth;
  418. if ($width === -1) {
  419. $width = $height / $ratio;
  420. }
  421. if ($height === -1) {
  422. $height = $width * $ratio;
  423. }
  424. $ratioH = $height / $maxHeight;
  425. $ratioW = $width / $maxWidth;
  426. /*
  427. * Fill means that the $height and $width are the max
  428. * Cover means min.
  429. */
  430. if ($mode === IPreview::MODE_FILL) {
  431. if ($ratioH > $ratioW) {
  432. $height = $width * $ratio;
  433. } else {
  434. $width = $height / $ratio;
  435. }
  436. } elseif ($mode === IPreview::MODE_COVER) {
  437. if ($ratioH > $ratioW) {
  438. $width = $height / $ratio;
  439. } else {
  440. $height = $width * $ratio;
  441. }
  442. }
  443. }
  444. if ($height !== $maxHeight && $width !== $maxWidth) {
  445. /*
  446. * Scale to the nearest power of four
  447. */
  448. $pow4height = 4 ** ceil(log($height) / log(4));
  449. $pow4width = 4 ** ceil(log($width) / log(4));
  450. // Minimum size is 64
  451. $pow4height = max($pow4height, 64);
  452. $pow4width = max($pow4width, 64);
  453. $ratioH = $height / $pow4height;
  454. $ratioW = $width / $pow4width;
  455. if ($ratioH < $ratioW) {
  456. $width = $pow4width;
  457. $height /= $ratioW;
  458. } else {
  459. $height = $pow4height;
  460. $width /= $ratioH;
  461. }
  462. }
  463. /*
  464. * Make sure the requested height and width fall within the max
  465. * of the preview.
  466. */
  467. if ($height > $maxHeight) {
  468. $ratio = $height / $maxHeight;
  469. $height = $maxHeight;
  470. $width /= $ratio;
  471. }
  472. if ($width > $maxWidth) {
  473. $ratio = $width / $maxWidth;
  474. $width = $maxWidth;
  475. $height /= $ratio;
  476. }
  477. return [(int)round($width), (int)round($height)];
  478. }
  479. /**
  480. * @param ISimpleFolder $previewFolder
  481. * @param ISimpleFile $maxPreview
  482. * @param int $width
  483. * @param int $height
  484. * @param bool $crop
  485. * @param int $maxWidth
  486. * @param int $maxHeight
  487. * @param string $prefix
  488. * @return ISimpleFile
  489. * @throws NotFoundException
  490. * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
  491. */
  492. private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) {
  493. $preview = $maxPreview;
  494. if (!$preview->valid()) {
  495. throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
  496. }
  497. $previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new');
  498. $sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);
  499. try {
  500. if ($crop) {
  501. if ($height !== $preview->height() && $width !== $preview->width()) {
  502. //Resize
  503. $widthR = $preview->width() / $width;
  504. $heightR = $preview->height() / $height;
  505. if ($widthR > $heightR) {
  506. $scaleH = $height;
  507. $scaleW = $maxWidth / $heightR;
  508. } else {
  509. $scaleH = $maxHeight / $widthR;
  510. $scaleW = $width;
  511. }
  512. $preview = $preview->preciseResizeCopy((int)round($scaleW), (int)round($scaleH));
  513. }
  514. $cropX = (int)floor(abs($width - $preview->width()) * 0.5);
  515. $cropY = (int)floor(abs($height - $preview->height()) * 0.5);
  516. $preview = $preview->cropCopy($cropX, $cropY, $width, $height);
  517. } else {
  518. $preview = $maxPreview->resizeCopy(max($width, $height));
  519. }
  520. } finally {
  521. self::unguardWithSemaphore($sem);
  522. }
  523. $path = $this->generatePath($width, $height, $crop, false, $preview->dataMimeType(), $prefix);
  524. try {
  525. $file = $previewFolder->newFile($path);
  526. $file->putContent($preview->data());
  527. } catch (NotPermittedException $e) {
  528. throw new NotFoundException();
  529. }
  530. return $file;
  531. }
  532. /**
  533. * @param ISimpleFile[] $files Array of FileInfo, as the result of getDirectoryListing()
  534. * @param int $width
  535. * @param int $height
  536. * @param bool $crop
  537. * @param string $mimeType
  538. * @param string $prefix
  539. * @return ISimpleFile
  540. *
  541. * @throws NotFoundException
  542. */
  543. private function getCachedPreview($files, $width, $height, $crop, $mimeType, $prefix) {
  544. $path = $this->generatePath($width, $height, $crop, false, $mimeType, $prefix);
  545. foreach ($files as $file) {
  546. if ($file->getName() === $path) {
  547. return $file;
  548. }
  549. }
  550. throw new NotFoundException();
  551. }
  552. /**
  553. * Get the specific preview folder for this file
  554. *
  555. * @param File $file
  556. * @return ISimpleFolder
  557. *
  558. * @throws InvalidPathException
  559. * @throws NotFoundException
  560. * @throws NotPermittedException
  561. */
  562. private function getPreviewFolder(File $file) {
  563. // Obtain file id outside of try catch block to prevent the creation of an existing folder
  564. $fileId = (string)$file->getId();
  565. try {
  566. $folder = $this->appData->getFolder($fileId);
  567. } catch (NotFoundException $e) {
  568. $folder = $this->appData->newFolder($fileId);
  569. }
  570. return $folder;
  571. }
  572. /**
  573. * @param string $mimeType
  574. * @return null|string
  575. * @throws \InvalidArgumentException
  576. */
  577. private function getExtension($mimeType) {
  578. switch ($mimeType) {
  579. case 'image/png':
  580. return 'png';
  581. case 'image/jpeg':
  582. return 'jpg';
  583. case 'image/gif':
  584. return 'gif';
  585. default:
  586. throw new \InvalidArgumentException('Not a valid mimetype: "' . $mimeType . '"');
  587. }
  588. }
  589. }