Preview.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Frank Karlitschek <frank@karlitschek.de>
  7. * @author Georg Ehrke <georg@owncloud.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Olivier Paroz <github@oparoz.com>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Tobias Kaminsky <tobias@kaminsky.me>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC;
  34. use OC\Preview\Provider;
  35. use OCP\Files\FileInfo;
  36. use OCP\Files\NotFoundException;
  37. class Preview {
  38. //the thumbnail folder
  39. const THUMBNAILS_FOLDER = 'thumbnails';
  40. const MODE_FILL = 'fill';
  41. const MODE_COVER = 'cover';
  42. //config
  43. private $maxScaleFactor;
  44. /** @var int maximum width allowed for a preview */
  45. private $configMaxWidth;
  46. /** @var int maximum height allowed for a preview */
  47. private $configMaxHeight;
  48. //fileview object
  49. private $fileView = null;
  50. private $userView = null;
  51. //vars
  52. private $file;
  53. private $maxX;
  54. private $maxY;
  55. private $scalingUp;
  56. private $mimeType;
  57. private $keepAspect = false;
  58. private $mode = self::MODE_FILL;
  59. //used to calculate the size of the preview to generate
  60. /** @var int $maxPreviewWidth max width a preview can have */
  61. private $maxPreviewWidth;
  62. /** @var int $maxPreviewHeight max height a preview can have */
  63. private $maxPreviewHeight;
  64. /** @var int $previewWidth calculated width of the preview we're looking for */
  65. private $previewWidth;
  66. /** @var int $previewHeight calculated height of the preview we're looking for */
  67. private $previewHeight;
  68. //filemapper used for deleting previews
  69. // index is path, value is fileinfo
  70. static public $deleteFileMapper = array();
  71. static public $deleteChildrenMapper = array();
  72. /**
  73. * preview images object
  74. *
  75. * @var \OCP\IImage
  76. */
  77. private $preview;
  78. /**
  79. * @var \OCP\Files\FileInfo
  80. */
  81. protected $info;
  82. /**
  83. * check if thumbnail or bigger version of thumbnail of file is cached
  84. *
  85. * @param string $user userid - if no user is given, OC_User::getUser will be used
  86. * @param string $root path of root
  87. * @param string $file The path to the file where you want a thumbnail from
  88. * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the
  89. * shape of the image
  90. * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the
  91. * shape of the image
  92. * @param bool $scalingUp Disable/Enable upscaling of previews
  93. *
  94. * @throws \Exception
  95. * @return mixed (bool / string)
  96. * false if thumbnail does not exist
  97. * path to thumbnail if thumbnail exists
  98. */
  99. public function __construct(
  100. $user = '',
  101. $root = '/',
  102. $file = '', $maxX = 1,
  103. $maxY = 1,
  104. $scalingUp = true
  105. ) {
  106. //init fileviews
  107. if ($user === '') {
  108. $user = \OC_User::getUser();
  109. }
  110. $this->fileView = new \OC\Files\View('/' . $user . '/' . $root);
  111. $this->userView = new \OC\Files\View('/' . $user);
  112. //set config
  113. $sysConfig = \OC::$server->getConfig();
  114. $this->configMaxWidth = $sysConfig->getSystemValue('preview_max_x', 2048);
  115. $this->configMaxHeight = $sysConfig->getSystemValue('preview_max_y', 2048);
  116. $this->maxScaleFactor = $sysConfig->getSystemValue('preview_max_scale_factor', 2);
  117. //save parameters
  118. $this->setFile($file);
  119. $this->setMaxX((int)$maxX);
  120. $this->setMaxY((int)$maxY);
  121. $this->setScalingUp($scalingUp);
  122. $this->preview = null;
  123. //check if there are preview backends
  124. if (!\OC::$server->getPreviewManager()
  125. ->hasProviders()
  126. && \OC::$server->getConfig()
  127. ->getSystemValue('enable_previews', true)
  128. ) {
  129. \OCP\Util::writeLog('core', 'No preview providers exist', \OCP\Util::ERROR);
  130. throw new \Exception('No preview providers');
  131. }
  132. }
  133. /**
  134. * returns the path of the file you want a thumbnail from
  135. *
  136. * @return string
  137. */
  138. public function getFile() {
  139. return $this->file;
  140. }
  141. /**
  142. * returns the max width of the preview
  143. *
  144. * @return integer
  145. */
  146. public function getMaxX() {
  147. return $this->maxX;
  148. }
  149. /**
  150. * returns the max height of the preview
  151. *
  152. * @return integer
  153. */
  154. public function getMaxY() {
  155. return $this->maxY;
  156. }
  157. /**
  158. * returns whether or not scalingup is enabled
  159. *
  160. * @return bool
  161. */
  162. public function getScalingUp() {
  163. return $this->scalingUp;
  164. }
  165. /**
  166. * returns the name of the thumbnailfolder
  167. *
  168. * @return string
  169. */
  170. public function getThumbnailsFolder() {
  171. return self::THUMBNAILS_FOLDER;
  172. }
  173. /**
  174. * returns the max scale factor
  175. *
  176. * @return string
  177. */
  178. public function getMaxScaleFactor() {
  179. return $this->maxScaleFactor;
  180. }
  181. /**
  182. * returns the max width set in ownCloud's config
  183. *
  184. * @return integer
  185. */
  186. public function getConfigMaxX() {
  187. return $this->configMaxWidth;
  188. }
  189. /**
  190. * returns the max height set in ownCloud's config
  191. *
  192. * @return integer
  193. */
  194. public function getConfigMaxY() {
  195. return $this->configMaxHeight;
  196. }
  197. /**
  198. * Returns the FileInfo object associated with the file to preview
  199. *
  200. * @return false|Files\FileInfo|\OCP\Files\FileInfo
  201. */
  202. protected function getFileInfo() {
  203. $absPath = $this->fileView->getAbsolutePath($this->file);
  204. $absPath = Files\Filesystem::normalizePath($absPath);
  205. if (array_key_exists($absPath, self::$deleteFileMapper)) {
  206. $this->info = self::$deleteFileMapper[$absPath];
  207. } else if (!$this->info) {
  208. $this->info = $this->fileView->getFileInfo($this->file);
  209. }
  210. return $this->info;
  211. }
  212. /**
  213. * @return array|null
  214. */
  215. private function getChildren() {
  216. $absPath = $this->fileView->getAbsolutePath($this->file);
  217. $absPath = Files\Filesystem::normalizePath($absPath);
  218. if (array_key_exists($absPath, self::$deleteChildrenMapper)) {
  219. return self::$deleteChildrenMapper[$absPath];
  220. }
  221. return null;
  222. }
  223. /**
  224. * Sets the path of the file you want a preview of
  225. *
  226. * @param string $file
  227. * @param \OCP\Files\FileInfo|null $info
  228. *
  229. * @return \OC\Preview
  230. */
  231. public function setFile($file, $info = null) {
  232. $this->file = $file;
  233. $this->info = $info;
  234. if ($file !== '') {
  235. $this->getFileInfo();
  236. if ($this->info instanceof \OCP\Files\FileInfo) {
  237. $this->mimeType = $this->info->getMimetype();
  238. }
  239. }
  240. return $this;
  241. }
  242. /**
  243. * Forces the use of a specific media type
  244. *
  245. * @param string $mimeType
  246. */
  247. public function setMimetype($mimeType) {
  248. $this->mimeType = $mimeType;
  249. }
  250. /**
  251. * Sets the max width of the preview. It's capped by the maximum allowed size set in the
  252. * configuration
  253. *
  254. * @param int $maxX
  255. *
  256. * @throws \Exception
  257. * @return \OC\Preview
  258. */
  259. public function setMaxX($maxX = 1) {
  260. if ($maxX <= 0) {
  261. throw new \Exception('Cannot set width of 0 or smaller!');
  262. }
  263. $configMaxX = $this->getConfigMaxX();
  264. $maxX = $this->limitMaxDim($maxX, $configMaxX, 'maxX');
  265. $this->maxX = $maxX;
  266. return $this;
  267. }
  268. /**
  269. * Sets the max height of the preview. It's capped by the maximum allowed size set in the
  270. * configuration
  271. *
  272. * @param int $maxY
  273. *
  274. * @throws \Exception
  275. * @return \OC\Preview
  276. */
  277. public function setMaxY($maxY = 1) {
  278. if ($maxY <= 0) {
  279. throw new \Exception('Cannot set height of 0 or smaller!');
  280. }
  281. $configMaxY = $this->getConfigMaxY();
  282. $maxY = $this->limitMaxDim($maxY, $configMaxY, 'maxY');
  283. $this->maxY = $maxY;
  284. return $this;
  285. }
  286. /**
  287. * Sets whether we're allowed to scale up when generating a preview. It's capped by the maximum
  288. * allowed scale factor set in the configuration
  289. *
  290. * @param bool $scalingUp
  291. *
  292. * @return \OC\Preview
  293. */
  294. public function setScalingup($scalingUp) {
  295. if ($this->getMaxScaleFactor() === 1) {
  296. $scalingUp = false;
  297. }
  298. $this->scalingUp = $scalingUp;
  299. return $this;
  300. }
  301. /**
  302. * Set whether to cover or fill the specified dimensions
  303. *
  304. * @param string $mode
  305. *
  306. * @return \OC\Preview
  307. */
  308. public function setMode($mode) {
  309. $this->mode = $mode;
  310. return $this;
  311. }
  312. /**
  313. * Sets whether we need to generate a preview which keeps the aspect ratio of the original file
  314. *
  315. * @param bool $keepAspect
  316. *
  317. * @return \OC\Preview
  318. */
  319. public function setKeepAspect($keepAspect) {
  320. $this->keepAspect = $keepAspect;
  321. return $this;
  322. }
  323. /**
  324. * Makes sure we were given a file to preview and that it exists in the filesystem
  325. *
  326. * @return bool
  327. */
  328. public function isFileValid() {
  329. $file = $this->getFile();
  330. if ($file === '') {
  331. \OCP\Util::writeLog('core', 'No filename passed', \OCP\Util::DEBUG);
  332. return false;
  333. }
  334. if (!$this->getFileInfo() instanceof FileInfo) {
  335. \OCP\Util::writeLog('core', 'File:"' . $file . '" not found', \OCP\Util::DEBUG);
  336. return false;
  337. }
  338. return true;
  339. }
  340. /**
  341. * Deletes the preview of a file with specific width and height
  342. *
  343. * This should never delete the max preview, use deleteAllPreviews() instead
  344. *
  345. * @return bool
  346. */
  347. public function deletePreview() {
  348. $fileInfo = $this->getFileInfo();
  349. if ($fileInfo !== null && $fileInfo !== false) {
  350. $fileId = $fileInfo->getId();
  351. $previewPath = $this->buildCachePath($fileId);
  352. if (!strpos($previewPath, 'max')) {
  353. return $this->userView->unlink($previewPath);
  354. }
  355. }
  356. return false;
  357. }
  358. /**
  359. * Deletes all previews of a file
  360. */
  361. public function deleteAllPreviews() {
  362. $thumbnailMount = $this->userView->getMount($this->getThumbnailsFolder());
  363. $propagator = $thumbnailMount->getStorage()->getPropagator();
  364. $propagator->beginBatch();
  365. $toDelete = $this->getChildren();
  366. $toDelete[] = $this->getFileInfo();
  367. foreach ($toDelete as $delete) {
  368. if ($delete instanceof FileInfo) {
  369. /** @var \OCP\Files\FileInfo $delete */
  370. $fileId = $delete->getId();
  371. // getId() might return null, e.g. when the file is a
  372. // .ocTransferId*.part file from chunked file upload.
  373. if (!empty($fileId)) {
  374. $previewPath = $this->getPreviewPath($fileId);
  375. $this->userView->rmdir($previewPath);
  376. }
  377. }
  378. }
  379. $propagator->commitBatch();
  380. }
  381. /**
  382. * Checks if a preview matching the asked dimensions or a bigger version is already cached
  383. *
  384. * * We first retrieve the size of the max preview since this is what we be used to create
  385. * all our preview. If it doesn't exist we return false, so that it can be generated
  386. * * Using the dimensions of the max preview, we calculate what the size of the new
  387. * thumbnail should be
  388. * * And finally, we look for a suitable candidate in the cache
  389. *
  390. * @param int $fileId fileId of the original file we need a preview of
  391. *
  392. * @return string|false path to the cached preview if it exists or false
  393. */
  394. public function isCached($fileId) {
  395. if (is_null($fileId)) {
  396. return false;
  397. }
  398. /**
  399. * Phase 1: Looking for the max preview
  400. */
  401. $previewPath = $this->getPreviewPath($fileId);
  402. // We currently can't look for a single file due to bugs related to #16478
  403. $allThumbnails = $this->userView->getDirectoryContent($previewPath);
  404. list($maxPreviewWidth, $maxPreviewHeight) = $this->getMaxPreviewSize($allThumbnails);
  405. // Only use the cache if we have a max preview
  406. if (!is_null($maxPreviewWidth) && !is_null($maxPreviewHeight)) {
  407. /**
  408. * Phase 2: Calculating the size of the preview we need to send back
  409. */
  410. $this->maxPreviewWidth = $maxPreviewWidth;
  411. $this->maxPreviewHeight = $maxPreviewHeight;
  412. list($previewWidth, $previewHeight) = $this->simulatePreviewDimensions();
  413. if (empty($previewWidth) || empty($previewHeight)) {
  414. return false;
  415. }
  416. $this->previewWidth = $previewWidth;
  417. $this->previewHeight = $previewHeight;
  418. /**
  419. * Phase 3: We look for a preview of the exact size
  420. */
  421. // This gives us a calculated path to a preview of asked dimensions
  422. // thumbnailFolder/fileId/<maxX>-<maxY>(-max|-with-aspect).png
  423. $preview = $this->buildCachePath($fileId, $previewWidth, $previewHeight);
  424. // This checks if we have a preview of those exact dimensions in the cache
  425. if ($this->thumbnailSizeExists($allThumbnails, basename($preview))) {
  426. return $preview;
  427. }
  428. /**
  429. * Phase 4: We look for a larger preview, matching the aspect ratio
  430. */
  431. if (($this->getMaxX() >= $maxPreviewWidth)
  432. && ($this->getMaxY() >= $maxPreviewHeight)
  433. ) {
  434. // The preview we-re looking for is the exact size or larger than the max preview,
  435. // so return that
  436. return $this->buildCachePath($fileId, $maxPreviewWidth, $maxPreviewHeight);
  437. } else {
  438. // The last resort is to look for something bigger than what we've calculated,
  439. // but still smaller than the max preview
  440. return $this->isCachedBigger($fileId, $allThumbnails);
  441. }
  442. }
  443. return false;
  444. }
  445. /**
  446. * Returns the dimensions of the max preview
  447. *
  448. * @param FileInfo[] $allThumbnails the list of all our cached thumbnails
  449. *
  450. * @return int[]
  451. */
  452. private function getMaxPreviewSize($allThumbnails) {
  453. $maxPreviewX = null;
  454. $maxPreviewY = null;
  455. foreach ($allThumbnails as $thumbnail) {
  456. $name = $thumbnail['name'];
  457. if (strpos($name, 'max')) {
  458. list($maxPreviewX, $maxPreviewY) = $this->getDimensionsFromFilename($name);
  459. break;
  460. }
  461. }
  462. return [$maxPreviewX, $maxPreviewY];
  463. }
  464. /**
  465. * Check if a specific thumbnail size is cached
  466. *
  467. * @param FileInfo[] $allThumbnails the list of all our cached thumbnails
  468. * @param string $name
  469. * @return bool
  470. */
  471. private function thumbnailSizeExists(array $allThumbnails, $name) {
  472. foreach ($allThumbnails as $thumbnail) {
  473. if ($name === $thumbnail->getName()) {
  474. return true;
  475. }
  476. }
  477. return false;
  478. }
  479. /**
  480. * Determines the size of the preview we should be looking for in the cache
  481. *
  482. * @return integer[]
  483. */
  484. private function simulatePreviewDimensions() {
  485. $askedWidth = $this->getMaxX();
  486. $askedHeight = $this->getMaxY();
  487. if ($this->keepAspect) {
  488. list($newPreviewWidth, $newPreviewHeight) =
  489. $this->applyAspectRatio($askedWidth, $askedHeight);
  490. } else {
  491. list($newPreviewWidth, $newPreviewHeight) = $this->fixSize($askedWidth, $askedHeight);
  492. }
  493. return [(int)$newPreviewWidth, (int)$newPreviewHeight];
  494. }
  495. /**
  496. * Resizes the boundaries to match the aspect ratio
  497. *
  498. * @param int $askedWidth
  499. * @param int $askedHeight
  500. *
  501. * @param int $originalWidth
  502. * @param int $originalHeight
  503. * @return integer[]
  504. */
  505. private function applyAspectRatio($askedWidth, $askedHeight, $originalWidth = 0, $originalHeight = 0) {
  506. if (!$originalWidth) {
  507. $originalWidth = $this->maxPreviewWidth;
  508. }
  509. if (!$originalHeight) {
  510. $originalHeight = $this->maxPreviewHeight;
  511. }
  512. $originalRatio = $originalWidth / $originalHeight;
  513. // Defines the box in which the preview has to fit
  514. $scaleFactor = $this->scalingUp ? $this->maxScaleFactor : 1;
  515. $askedWidth = min($askedWidth, $originalWidth * $scaleFactor);
  516. $askedHeight = min($askedHeight, $originalHeight * $scaleFactor);
  517. if ($askedWidth / $originalRatio < $askedHeight) {
  518. // width restricted
  519. $askedHeight = round($askedWidth / $originalRatio);
  520. } else {
  521. $askedWidth = round($askedHeight * $originalRatio);
  522. }
  523. return [(int)$askedWidth, (int)$askedHeight];
  524. }
  525. /**
  526. * Resizes the boundaries to cover the area
  527. *
  528. * @param int $askedWidth
  529. * @param int $askedHeight
  530. * @param int $previewWidth
  531. * @param int $previewHeight
  532. * @return integer[]
  533. */
  534. private function applyCover($askedWidth, $askedHeight, $previewWidth, $previewHeight) {
  535. $originalRatio = $previewWidth / $previewHeight;
  536. // Defines the box in which the preview has to fit
  537. $scaleFactor = $this->scalingUp ? $this->maxScaleFactor : 1;
  538. $askedWidth = min($askedWidth, $previewWidth * $scaleFactor);
  539. $askedHeight = min($askedHeight, $previewHeight * $scaleFactor);
  540. if ($askedWidth / $originalRatio > $askedHeight) {
  541. // height restricted
  542. $askedHeight = round($askedWidth / $originalRatio);
  543. } else {
  544. $askedWidth = round($askedHeight * $originalRatio);
  545. }
  546. return [(int)$askedWidth, (int)$askedHeight];
  547. }
  548. /**
  549. * Makes sure an upscaled preview doesn't end up larger than the max dimensions defined in the
  550. * config
  551. *
  552. * @param int $askedWidth
  553. * @param int $askedHeight
  554. *
  555. * @return integer[]
  556. */
  557. private function fixSize($askedWidth, $askedHeight) {
  558. if ($this->scalingUp) {
  559. $askedWidth = min($this->configMaxWidth, $askedWidth);
  560. $askedHeight = min($this->configMaxHeight, $askedHeight);
  561. }
  562. return [(int)$askedWidth, (int)$askedHeight];
  563. }
  564. /**
  565. * Checks if a bigger version of a file preview is cached and if not
  566. * return the preview of max allowed dimensions
  567. *
  568. * @param int $fileId fileId of the original image
  569. * @param FileInfo[] $allThumbnails the list of all our cached thumbnails
  570. *
  571. * @return string path to bigger thumbnail
  572. */
  573. private function isCachedBigger($fileId, $allThumbnails) {
  574. // This is used to eliminate any thumbnail narrower than what we need
  575. $maxX = $this->getMaxX();
  576. //array for usable cached thumbnails
  577. $possibleThumbnails = $this->getPossibleThumbnails($allThumbnails);
  578. foreach ($possibleThumbnails as $width => $path) {
  579. if ($width < $maxX) {
  580. continue;
  581. } else {
  582. return $path;
  583. }
  584. }
  585. // At this stage, we didn't find a preview, so we return the max preview
  586. return $this->buildCachePath($fileId, $this->maxPreviewWidth, $this->maxPreviewHeight);
  587. }
  588. /**
  589. * Get possible bigger thumbnails of the given image with the proper aspect ratio
  590. *
  591. * @param FileInfo[] $allThumbnails the list of all our cached thumbnails
  592. *
  593. * @return string[] an array of paths to bigger thumbnails
  594. */
  595. private function getPossibleThumbnails($allThumbnails) {
  596. if ($this->keepAspect) {
  597. $wantedAspectRatio = (float)($this->maxPreviewWidth / $this->maxPreviewHeight);
  598. } else {
  599. $wantedAspectRatio = (float)($this->getMaxX() / $this->getMaxY());
  600. }
  601. //array for usable cached thumbnails
  602. $possibleThumbnails = array();
  603. foreach ($allThumbnails as $thumbnail) {
  604. $name = rtrim($thumbnail['name'], '.png');
  605. list($x, $y, $aspectRatio) = $this->getDimensionsFromFilename($name);
  606. if (abs($aspectRatio - $wantedAspectRatio) >= 0.000001
  607. || $this->unscalable($x, $y)
  608. ) {
  609. continue;
  610. }
  611. $possibleThumbnails[$x] = $thumbnail['path'];
  612. }
  613. ksort($possibleThumbnails);
  614. return $possibleThumbnails;
  615. }
  616. /**
  617. * Looks at the preview filename from the cache and extracts the size of the preview
  618. *
  619. * @param string $name
  620. *
  621. * @return array<int,int,float>
  622. */
  623. private function getDimensionsFromFilename($name) {
  624. $size = explode('-', $name);
  625. $x = (int)$size[0];
  626. $y = (int)$size[1];
  627. $aspectRatio = (float)($x / $y);
  628. return array($x, $y, $aspectRatio);
  629. }
  630. /**
  631. * @param int $x
  632. * @param int $y
  633. *
  634. * @return bool
  635. */
  636. private function unscalable($x, $y) {
  637. $maxX = $this->getMaxX();
  638. $maxY = $this->getMaxY();
  639. $scalingUp = $this->getScalingUp();
  640. $maxScaleFactor = $this->getMaxScaleFactor();
  641. if ($x < $maxX || $y < $maxY) {
  642. if ($scalingUp) {
  643. $scaleFactor = $maxX / $x;
  644. if ($scaleFactor > $maxScaleFactor) {
  645. return true;
  646. }
  647. } else {
  648. return true;
  649. }
  650. }
  651. return false;
  652. }
  653. /**
  654. * Returns a preview of a file
  655. *
  656. * The cache is searched first and if nothing usable was found then a preview is
  657. * generated by one of the providers
  658. *
  659. * @return \OCP\IImage
  660. */
  661. public function getPreview() {
  662. if (!is_null($this->preview) && $this->preview->valid()) {
  663. return $this->preview;
  664. }
  665. $this->preview = null;
  666. $fileInfo = $this->getFileInfo();
  667. if ($fileInfo === null || $fileInfo === false || !$fileInfo->isReadable()) {
  668. return new \OC_Image();
  669. }
  670. $fileId = $fileInfo->getId();
  671. $cached = $this->isCached($fileId);
  672. if ($cached) {
  673. $this->getCachedPreview($fileId, $cached);
  674. }
  675. if (is_null($this->preview)) {
  676. $this->generatePreview($fileId);
  677. }
  678. // We still don't have a preview, so we send back an empty object
  679. if (is_null($this->preview)) {
  680. $this->preview = new \OC_Image();
  681. }
  682. return $this->preview;
  683. }
  684. /**
  685. * Sends the preview, including the headers to client which requested it
  686. *
  687. * @param null|string $mimeTypeForHeaders the media type to use when sending back the reply
  688. *
  689. * @throws NotFoundException
  690. */
  691. public function showPreview($mimeTypeForHeaders = null) {
  692. // Check if file is valid
  693. if ($this->isFileValid() === false) {
  694. throw new NotFoundException('File not found.');
  695. }
  696. if (is_null($this->preview)) {
  697. $this->getPreview();
  698. }
  699. if ($this->preview instanceof \OCP\IImage) {
  700. if ($this->preview->valid()) {
  701. \OCP\Response::enableCaching(3600 * 24); // 24 hours
  702. } else {
  703. $this->getMimeIcon();
  704. }
  705. $this->preview->show($mimeTypeForHeaders);
  706. }
  707. }
  708. /**
  709. * Retrieves the preview from the cache and resizes it if necessary
  710. *
  711. * @param int $fileId fileId of the original image
  712. * @param string $cached the path to the cached preview
  713. */
  714. private function getCachedPreview($fileId, $cached) {
  715. $stream = $this->userView->fopen($cached, 'r');
  716. $this->preview = null;
  717. if ($stream) {
  718. $image = new \OC_Image();
  719. $image->loadFromFileHandle($stream);
  720. $this->preview = $image->valid() ? $image : null;
  721. if (!is_null($this->preview)) {
  722. // Size of the preview we calculated
  723. $maxX = $this->previewWidth;
  724. $maxY = $this->previewHeight;
  725. // Size of the preview we retrieved from the cache
  726. $previewX = (int)$this->preview->width();
  727. $previewY = (int)$this->preview->height();
  728. // We don't have an exact match
  729. if ($previewX !== $maxX || $previewY !== $maxY) {
  730. $this->resizeAndStore($fileId);
  731. }
  732. }
  733. fclose($stream);
  734. }
  735. }
  736. /**
  737. * Resizes, crops, fixes orientation and stores in the cache
  738. *
  739. * @param int $fileId fileId of the original image
  740. */
  741. private function resizeAndStore($fileId) {
  742. $image = $this->preview;
  743. if (!($image instanceof \OCP\IImage)) {
  744. \OCP\Util::writeLog(
  745. 'core', '$this->preview is not an instance of \OCP\IImage', \OCP\Util::DEBUG
  746. );
  747. return;
  748. }
  749. $previewWidth = (int)$image->width();
  750. $previewHeight = (int)$image->height();
  751. $askedWidth = $this->getMaxX();
  752. $askedHeight = $this->getMaxY();
  753. if ($this->mode === self::MODE_COVER) {
  754. list($askedWidth, $askedHeight) =
  755. $this->applyCover($askedWidth, $askedHeight, $previewWidth, $previewHeight);
  756. }
  757. /**
  758. * Phase 1: If required, adjust boundaries to keep aspect ratio
  759. */
  760. if ($this->keepAspect) {
  761. list($askedWidth, $askedHeight) =
  762. $this->applyAspectRatio($askedWidth, $askedHeight, $previewWidth, $previewHeight);
  763. }
  764. /**
  765. * Phase 2: Resizes preview to try and match requirements.
  766. * Takes the scaling ratio into consideration
  767. */
  768. list($newPreviewWidth, $newPreviewHeight) = $this->scale(
  769. $image, $askedWidth, $askedHeight, $previewWidth, $previewHeight
  770. );
  771. // The preview has been resized and should now have the asked dimensions
  772. if ($newPreviewWidth === $askedWidth && $newPreviewHeight === $askedHeight) {
  773. $this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight);
  774. return;
  775. }
  776. /**
  777. * Phase 3: We're still not there yet, so we're clipping and filling
  778. * to match the asked dimensions
  779. */
  780. // It turns out the scaled preview is now too big, so we crop the image
  781. if ($newPreviewWidth >= $askedWidth && $newPreviewHeight >= $askedHeight) {
  782. $this->crop($image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight);
  783. $this->storePreview($fileId, $askedWidth, $askedHeight);
  784. return;
  785. }
  786. // At least one dimension of the scaled preview is too small,
  787. // so we fill the space with a transparent background
  788. if (($newPreviewWidth < $askedWidth || $newPreviewHeight < $askedHeight)) {
  789. $this->cropAndFill(
  790. $image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight
  791. );
  792. $this->storePreview($fileId, $askedWidth, $askedHeight);
  793. return;
  794. }
  795. // The preview is smaller, but we can't touch it
  796. $this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight);
  797. }
  798. /**
  799. * Calculates the new dimensions of the preview
  800. *
  801. * The new dimensions can be larger or smaller than the ones of the preview we have to resize
  802. *
  803. * @param \OCP\IImage $image
  804. * @param int $askedWidth
  805. * @param int $askedHeight
  806. * @param int $previewWidth
  807. * @param int $previewHeight
  808. *
  809. * @return int[]
  810. */
  811. private function scale($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) {
  812. $scalingUp = $this->getScalingUp();
  813. $maxScaleFactor = $this->getMaxScaleFactor();
  814. $factorX = $askedWidth / $previewWidth;
  815. $factorY = $askedHeight / $previewHeight;
  816. if ($factorX >= $factorY) {
  817. $factor = $factorX;
  818. } else {
  819. $factor = $factorY;
  820. }
  821. if ($scalingUp === false) {
  822. if ($factor > 1) {
  823. $factor = 1;
  824. }
  825. }
  826. // We cap when upscaling
  827. if (!is_null($maxScaleFactor)) {
  828. if ($factor > $maxScaleFactor) {
  829. \OCP\Util::writeLog(
  830. 'core', 'scale factor reduced from ' . $factor . ' to ' . $maxScaleFactor,
  831. \OCP\Util::DEBUG
  832. );
  833. $factor = $maxScaleFactor;
  834. }
  835. }
  836. $newPreviewWidth = round($previewWidth * $factor);
  837. $newPreviewHeight = round($previewHeight * $factor);
  838. $image->preciseResize($newPreviewWidth, $newPreviewHeight);
  839. $this->preview = $image;
  840. return [$newPreviewWidth, $newPreviewHeight];
  841. }
  842. /**
  843. * Crops a preview which is larger than the dimensions we've received
  844. *
  845. * @param \OCP\IImage $image
  846. * @param int $askedWidth
  847. * @param int $askedHeight
  848. * @param int $previewWidth
  849. * @param int $previewHeight
  850. */
  851. private function crop($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight = null) {
  852. $cropX = floor(abs($askedWidth - $previewWidth) * 0.5);
  853. //don't crop previews on the Y axis, this sucks if it's a document.
  854. //$cropY = floor(abs($y - $newPreviewHeight) * 0.5);
  855. $cropY = 0;
  856. $image->crop($cropX, $cropY, $askedWidth, $askedHeight);
  857. $this->preview = $image;
  858. }
  859. /**
  860. * Crops an image if it's larger than the dimensions we've received and fills the empty space
  861. * with a transparent background
  862. *
  863. * @param \OCP\IImage $image
  864. * @param int $askedWidth
  865. * @param int $askedHeight
  866. * @param int $previewWidth
  867. * @param int $previewHeight
  868. */
  869. private function cropAndFill($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) {
  870. if ($previewWidth > $askedWidth) {
  871. $cropX = floor(($previewWidth - $askedWidth) * 0.5);
  872. $image->crop($cropX, 0, $askedWidth, $previewHeight);
  873. $previewWidth = $askedWidth;
  874. }
  875. if ($previewHeight > $askedHeight) {
  876. $cropY = floor(($previewHeight - $askedHeight) * 0.5);
  877. $image->crop(0, $cropY, $previewWidth, $askedHeight);
  878. $previewHeight = $askedHeight;
  879. }
  880. // Creates a transparent background
  881. $backgroundLayer = imagecreatetruecolor($askedWidth, $askedHeight);
  882. imagealphablending($backgroundLayer, false);
  883. $transparency = imagecolorallocatealpha($backgroundLayer, 0, 0, 0, 127);
  884. imagefill($backgroundLayer, 0, 0, $transparency);
  885. imagesavealpha($backgroundLayer, true);
  886. $image = $image->resource();
  887. $mergeX = floor(abs($askedWidth - $previewWidth) * 0.5);
  888. $mergeY = floor(abs($askedHeight - $previewHeight) * 0.5);
  889. // Pastes the preview on top of the background
  890. imagecopy(
  891. $backgroundLayer, $image, $mergeX, $mergeY, 0, 0, $previewWidth,
  892. $previewHeight
  893. );
  894. $image = new \OC_Image($backgroundLayer);
  895. $this->preview = $image;
  896. }
  897. /**
  898. * Saves a preview in the cache to speed up future calls
  899. *
  900. * Do not nullify the preview as it might send the whole process in a loop
  901. *
  902. * @param int $fileId fileId of the original image
  903. * @param int $previewWidth
  904. * @param int $previewHeight
  905. */
  906. private function storePreview($fileId, $previewWidth, $previewHeight) {
  907. if (empty($previewWidth) || empty($previewHeight)) {
  908. \OCP\Util::writeLog(
  909. 'core', 'Cannot save preview of dimension ' . $previewWidth . 'x' . $previewHeight,
  910. \OCP\Util::DEBUG
  911. );
  912. } else {
  913. $cachePath = $this->buildCachePath($fileId, $previewWidth, $previewHeight);
  914. $this->userView->file_put_contents($cachePath, $this->preview->data());
  915. }
  916. }
  917. /**
  918. * Returns the path to a preview based on its dimensions and aspect
  919. *
  920. * @param int $fileId
  921. * @param int|null $maxX
  922. * @param int|null $maxY
  923. *
  924. * @return string
  925. */
  926. private function buildCachePath($fileId, $maxX = null, $maxY = null) {
  927. if (is_null($maxX)) {
  928. $maxX = $this->getMaxX();
  929. }
  930. if (is_null($maxY)) {
  931. $maxY = $this->getMaxY();
  932. }
  933. $previewPath = $this->getPreviewPath($fileId);
  934. $previewPath = $previewPath . strval($maxX) . '-' . strval($maxY);
  935. $isMaxPreview =
  936. ($maxX === $this->maxPreviewWidth && $maxY === $this->maxPreviewHeight) ? true : false;
  937. if ($isMaxPreview) {
  938. $previewPath .= '-max';
  939. }
  940. if ($this->keepAspect && !$isMaxPreview) {
  941. $previewPath .= '-with-aspect';
  942. }
  943. if ($this->mode === self::MODE_COVER) {
  944. $previewPath .= '-cover';
  945. }
  946. $previewPath .= '.png';
  947. return $previewPath;
  948. }
  949. /**
  950. * Returns the path to the folder where the previews are stored, identified by the fileId
  951. *
  952. * @param int $fileId
  953. *
  954. * @return string
  955. */
  956. private function getPreviewPath($fileId) {
  957. return $this->getThumbnailsFolder() . '/' . $fileId . '/';
  958. }
  959. /**
  960. * Asks the provider to send a preview of the file which respects the maximum dimensions
  961. * defined in the configuration and after saving it in the cache, it is then resized to the
  962. * asked dimensions
  963. *
  964. * This is only called once in order to generate a large PNG of dimensions defined in the
  965. * configuration file. We'll be able to quickly resize it later on.
  966. * We never upscale the original conversion as this will be done later by the resizing
  967. * operation
  968. *
  969. * @param int $fileId fileId of the original image
  970. */
  971. private function generatePreview($fileId) {
  972. $file = $this->getFile();
  973. $preview = null;
  974. $previewProviders = \OC::$server->getPreviewManager()
  975. ->getProviders();
  976. foreach ($previewProviders as $supportedMimeType => $providers) {
  977. if (!preg_match($supportedMimeType, $this->mimeType)) {
  978. continue;
  979. }
  980. foreach ($providers as $closure) {
  981. $provider = $closure();
  982. if (!($provider instanceof \OCP\Preview\IProvider)) {
  983. continue;
  984. }
  985. \OCP\Util::writeLog(
  986. 'core', 'Generating preview for "' . $file . '" with "' . get_class($provider)
  987. . '"', \OCP\Util::DEBUG
  988. );
  989. /** @var $provider Provider */
  990. $preview = $provider->getThumbnail(
  991. $file, $this->configMaxWidth, $this->configMaxHeight, $scalingUp = false,
  992. $this->fileView
  993. );
  994. if (!($preview instanceof \OCP\IImage)) {
  995. continue;
  996. }
  997. $this->preview = $preview;
  998. $previewPath = $this->getPreviewPath($fileId);
  999. if ($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) {
  1000. $this->userView->mkdir($this->getThumbnailsFolder() . '/');
  1001. }
  1002. if ($this->userView->is_dir($previewPath) === false) {
  1003. $this->userView->mkdir($previewPath);
  1004. }
  1005. // This stores our large preview so that it can be used in subsequent resizing requests
  1006. $this->storeMaxPreview($previewPath);
  1007. break 2;
  1008. }
  1009. }
  1010. // The providers have been kind enough to give us a preview
  1011. if ($preview) {
  1012. $this->resizeAndStore($fileId);
  1013. }
  1014. }
  1015. /**
  1016. * Defines the media icon, for the media type of the original file, as the preview
  1017. */
  1018. private function getMimeIcon() {
  1019. $image = new \OC_Image();
  1020. $mimeIconWebPath = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($this->mimeType);
  1021. if (empty(\OC::$WEBROOT)) {
  1022. $mimeIconServerPath = \OC::$SERVERROOT . $mimeIconWebPath;
  1023. } else {
  1024. $mimeIconServerPath = str_replace(\OC::$WEBROOT, \OC::$SERVERROOT, $mimeIconWebPath);
  1025. }
  1026. $image->loadFromFile($mimeIconServerPath);
  1027. $this->preview = $image;
  1028. }
  1029. /**
  1030. * Stores the max preview in the cache
  1031. *
  1032. * @param string $previewPath path to the preview
  1033. */
  1034. private function storeMaxPreview($previewPath) {
  1035. $maxPreviewExists = false;
  1036. $preview = $this->preview;
  1037. $allThumbnails = $this->userView->getDirectoryContent($previewPath);
  1038. // This is so that the cache doesn't need emptying when upgrading
  1039. // Can be replaced by an upgrade script...
  1040. foreach ($allThumbnails as $thumbnail) {
  1041. $name = rtrim($thumbnail['name'], '.png');
  1042. if (strpos($name, 'max')) {
  1043. $maxPreviewExists = true;
  1044. break;
  1045. }
  1046. }
  1047. // We haven't found the max preview, so we create it
  1048. if (!$maxPreviewExists) {
  1049. $previewWidth = $preview->width();
  1050. $previewHeight = $preview->height();
  1051. $previewPath = $previewPath . strval($previewWidth) . '-' . strval($previewHeight);
  1052. $previewPath .= '-max.png';
  1053. $this->userView->file_put_contents($previewPath, $preview->data());
  1054. $this->maxPreviewWidth = $previewWidth;
  1055. $this->maxPreviewHeight = $previewHeight;
  1056. }
  1057. }
  1058. /**
  1059. * Limits a dimension to the maximum dimension provided as argument
  1060. *
  1061. * @param int $dim
  1062. * @param int $maxDim
  1063. * @param string $dimName
  1064. *
  1065. * @return integer
  1066. */
  1067. private function limitMaxDim($dim, $maxDim, $dimName) {
  1068. if (!is_null($maxDim)) {
  1069. if ($dim > $maxDim) {
  1070. \OCP\Util::writeLog(
  1071. 'core', $dimName . ' reduced from ' . $dim . ' to ' . $maxDim, \OCP\Util::DEBUG
  1072. );
  1073. $dim = $maxDim;
  1074. }
  1075. }
  1076. return $dim;
  1077. }
  1078. /**
  1079. * @param array $args
  1080. */
  1081. public static function post_write($args) {
  1082. self::post_delete($args, 'files/');
  1083. }
  1084. /**
  1085. * @param array $args
  1086. */
  1087. public static function prepare_delete_files($args) {
  1088. self::prepare_delete($args, 'files/');
  1089. }
  1090. /**
  1091. * @param array $args
  1092. * @param string $prefix
  1093. */
  1094. public static function prepare_delete(array $args, $prefix = '') {
  1095. $path = $args['path'];
  1096. if (substr($path, 0, 1) === '/') {
  1097. $path = substr($path, 1);
  1098. }
  1099. $view = new \OC\Files\View('/' . \OC_User::getUser() . '/' . $prefix);
  1100. $absPath = Files\Filesystem::normalizePath($view->getAbsolutePath($path));
  1101. $fileInfo = $view->getFileInfo($path);
  1102. if ($fileInfo === false) {
  1103. return;
  1104. }
  1105. self::addPathToDeleteFileMapper($absPath, $fileInfo);
  1106. if ($view->is_dir($path)) {
  1107. $children = self::getAllChildren($view, $path);
  1108. self::$deleteChildrenMapper[$absPath] = $children;
  1109. }
  1110. }
  1111. /**
  1112. * @param string $absolutePath
  1113. * @param \OCP\Files\FileInfo $info
  1114. */
  1115. private static function addPathToDeleteFileMapper($absolutePath, $info) {
  1116. self::$deleteFileMapper[$absolutePath] = $info;
  1117. }
  1118. /**
  1119. * @param \OC\Files\View $view
  1120. * @param string $path
  1121. *
  1122. * @return array
  1123. */
  1124. private static function getAllChildren($view, $path) {
  1125. $children = $view->getDirectoryContent($path);
  1126. $childrensFiles = array();
  1127. $fakeRootLength = strlen($view->getRoot());
  1128. for ($i = 0; $i < count($children); $i++) {
  1129. $child = $children[$i];
  1130. $childsPath = substr($child->getPath(), $fakeRootLength);
  1131. if ($view->is_dir($childsPath)) {
  1132. $children = array_merge(
  1133. $children,
  1134. $view->getDirectoryContent($childsPath)
  1135. );
  1136. } else {
  1137. $childrensFiles[] = $child;
  1138. }
  1139. }
  1140. return $childrensFiles;
  1141. }
  1142. /**
  1143. * @param array $args
  1144. */
  1145. public static function post_delete_files($args) {
  1146. self::post_delete($args, 'files/');
  1147. }
  1148. /**
  1149. * @param array $args
  1150. */
  1151. public static function post_delete_versions($args) {
  1152. self::post_delete($args, 'files/');
  1153. }
  1154. /**
  1155. * @param array $args
  1156. * @param string $prefix
  1157. */
  1158. public static function post_delete($args, $prefix = '') {
  1159. $path = Files\Filesystem::normalizePath($args['path']);
  1160. $preview = new Preview(\OC_User::getUser(), $prefix, $path);
  1161. $preview->deleteAllPreviews();
  1162. }
  1163. }