Generator.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Preview;
  24. use OCP\Files\File;
  25. use OCP\Files\IAppData;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Files\NotPermittedException;
  28. use OCP\Files\SimpleFS\ISimpleFile;
  29. use OCP\Files\SimpleFS\ISimpleFolder;
  30. use OCP\IConfig;
  31. use OCP\IImage;
  32. use OCP\IPreview;
  33. use OCP\Preview\IProvider;
  34. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  35. use Symfony\Component\EventDispatcher\GenericEvent;
  36. class Generator {
  37. /** @var IPreview */
  38. private $previewManager;
  39. /** @var IConfig */
  40. private $config;
  41. /** @var IAppData */
  42. private $appData;
  43. /** @var GeneratorHelper */
  44. private $helper;
  45. /** @var EventDispatcherInterface */
  46. private $eventDispatcher;
  47. /**
  48. * @param IConfig $config
  49. * @param IPreview $previewManager
  50. * @param IAppData $appData
  51. * @param GeneratorHelper $helper
  52. * @param EventDispatcherInterface $eventDispatcher
  53. */
  54. public function __construct(
  55. IConfig $config,
  56. IPreview $previewManager,
  57. IAppData $appData,
  58. GeneratorHelper $helper,
  59. EventDispatcherInterface $eventDispatcher
  60. ) {
  61. $this->config = $config;
  62. $this->previewManager = $previewManager;
  63. $this->appData = $appData;
  64. $this->helper = $helper;
  65. $this->eventDispatcher = $eventDispatcher;
  66. }
  67. /**
  68. * Returns a preview of a file
  69. *
  70. * The cache is searched first and if nothing usable was found then a preview is
  71. * generated by one of the providers
  72. *
  73. * @param File $file
  74. * @param int $width
  75. * @param int $height
  76. * @param bool $crop
  77. * @param string $mode
  78. * @param string $mimeType
  79. * @return ISimpleFile
  80. * @throws NotFoundException
  81. */
  82. public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
  83. $this->eventDispatcher->dispatch(
  84. IPreview::EVENT,
  85. new GenericEvent($file,[
  86. 'width' => $width,
  87. 'height' => $height,
  88. 'crop' => $crop,
  89. 'mode' => $mode
  90. ])
  91. );
  92. if ($mimeType === null) {
  93. $mimeType = $file->getMimeType();
  94. }
  95. if (!$this->previewManager->isMimeSupported($mimeType)) {
  96. throw new NotFoundException();
  97. }
  98. $previewFolder = $this->getPreviewFolder($file);
  99. // Get the max preview and infer the max preview sizes from that
  100. $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
  101. list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);
  102. // Calculate the preview size
  103. list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
  104. // No need to generate a preview that is just the max preview
  105. if ($width === $maxWidth && $height === $maxHeight) {
  106. return $maxPreview;
  107. }
  108. // Try to get a cached preview. Else generate (and store) one
  109. try {
  110. $file = $this->getCachedPreview($previewFolder, $width, $height, $crop);
  111. } catch (NotFoundException $e) {
  112. $file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
  113. }
  114. return $file;
  115. }
  116. /**
  117. * @param ISimpleFolder $previewFolder
  118. * @param File $file
  119. * @param string $mimeType
  120. * @return ISimpleFile
  121. * @throws NotFoundException
  122. */
  123. private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) {
  124. $nodes = $previewFolder->getDirectoryListing();
  125. foreach ($nodes as $node) {
  126. if (strpos($node->getName(), 'max')) {
  127. return $node;
  128. }
  129. }
  130. $previewProviders = $this->previewManager->getProviders();
  131. foreach ($previewProviders as $supportedMimeType => $providers) {
  132. if (!preg_match($supportedMimeType, $mimeType)) {
  133. continue;
  134. }
  135. foreach ($providers as $provider) {
  136. $provider = $this->helper->getProvider($provider);
  137. if (!($provider instanceof IProvider)) {
  138. continue;
  139. }
  140. $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048);
  141. $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048);
  142. $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
  143. if (!($preview instanceof IImage)) {
  144. continue;
  145. }
  146. $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png';
  147. try {
  148. $file = $previewFolder->newFile($path);
  149. $file->putContent($preview->data());
  150. } catch (NotPermittedException $e) {
  151. throw new NotFoundException();
  152. }
  153. return $file;
  154. }
  155. }
  156. throw new NotFoundException();
  157. }
  158. /**
  159. * @param ISimpleFile $file
  160. * @return int[]
  161. */
  162. private function getPreviewSize(ISimpleFile $file) {
  163. $size = explode('-', $file->getName());
  164. return [(int)$size[0], (int)$size[1]];
  165. }
  166. /**
  167. * @param int $width
  168. * @param int $height
  169. * @param bool $crop
  170. * @return string
  171. */
  172. private function generatePath($width, $height, $crop) {
  173. $path = (string)$width . '-' . (string)$height;
  174. if ($crop) {
  175. $path .= '-crop';
  176. }
  177. $path .= '.png';
  178. return $path;
  179. }
  180. /**
  181. * @param int $width
  182. * @param int $height
  183. * @param bool $crop
  184. * @param string $mode
  185. * @param int $maxWidth
  186. * @param int $maxHeight
  187. * @return int[]
  188. */
  189. private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
  190. /*
  191. * If we are not cropping we have to make sure the requested image
  192. * respects the aspect ratio of the original.
  193. */
  194. if (!$crop) {
  195. $ratio = $maxHeight / $maxWidth;
  196. if ($width === -1) {
  197. $width = $height / $ratio;
  198. }
  199. if ($height === -1) {
  200. $height = $width * $ratio;
  201. }
  202. $ratioH = $height / $maxHeight;
  203. $ratioW = $width / $maxWidth;
  204. /*
  205. * Fill means that the $height and $width are the max
  206. * Cover means min.
  207. */
  208. if ($mode === IPreview::MODE_FILL) {
  209. if ($ratioH > $ratioW) {
  210. $height = $width * $ratio;
  211. } else {
  212. $width = $height / $ratio;
  213. }
  214. } else if ($mode === IPreview::MODE_COVER) {
  215. if ($ratioH > $ratioW) {
  216. $width = $height / $ratio;
  217. } else {
  218. $height = $width * $ratio;
  219. }
  220. }
  221. }
  222. if ($height !== $maxHeight && $width !== $maxWidth) {
  223. /*
  224. * Scale to the nearest power of two
  225. */
  226. $pow2height = 2 ** ceil(log($height) / log(2));
  227. $pow2width = 2 ** ceil(log($width) / log(2));
  228. $ratioH = $height / $pow2height;
  229. $ratioW = $width / $pow2width;
  230. if ($ratioH < $ratioW) {
  231. $width = $pow2width;
  232. $height /= $ratioW;
  233. } else {
  234. $height = $pow2height;
  235. $width /= $ratioH;
  236. }
  237. }
  238. /*
  239. * Make sure the requested height and width fall within the max
  240. * of the preview.
  241. */
  242. if ($height > $maxHeight) {
  243. $ratio = $height / $maxHeight;
  244. $height = $maxHeight;
  245. $width /= $ratio;
  246. }
  247. if ($width > $maxWidth) {
  248. $ratio = $width / $maxWidth;
  249. $width = $maxWidth;
  250. $height /= $ratio;
  251. }
  252. return [(int)round($width), (int)round($height)];
  253. }
  254. /**
  255. * @param ISimpleFolder $previewFolder
  256. * @param ISimpleFile $maxPreview
  257. * @param int $width
  258. * @param int $height
  259. * @param bool $crop
  260. * @param int $maxWidth
  261. * @param int $maxHeight
  262. * @return ISimpleFile
  263. * @throws NotFoundException
  264. */
  265. private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
  266. $preview = $this->helper->getImage($maxPreview);
  267. if ($crop) {
  268. if ($height !== $preview->height() && $width !== $preview->width()) {
  269. //Resize
  270. $widthR = $preview->width() / $width;
  271. $heightR = $preview->height() / $height;
  272. if ($widthR > $heightR) {
  273. $scaleH = $height;
  274. $scaleW = $maxWidth / $heightR;
  275. } else {
  276. $scaleH = $maxHeight / $widthR;
  277. $scaleW = $width;
  278. }
  279. $preview->preciseResize(round($scaleW), round($scaleH));
  280. }
  281. $cropX = floor(abs($width - $preview->width()) * 0.5);
  282. $cropY = 0;
  283. $preview->crop($cropX, $cropY, $width, $height);
  284. } else {
  285. $preview->resize(max($width, $height));
  286. }
  287. $path = $this->generatePath($width, $height, $crop);
  288. try {
  289. $file = $previewFolder->newFile($path);
  290. $file->putContent($preview->data());
  291. } catch (NotPermittedException $e) {
  292. throw new NotFoundException();
  293. }
  294. return $file;
  295. }
  296. /**
  297. * @param ISimpleFolder $previewFolder
  298. * @param int $width
  299. * @param int $height
  300. * @param bool $crop
  301. * @return ISimpleFile
  302. *
  303. * @throws NotFoundException
  304. */
  305. private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) {
  306. $path = $this->generatePath($width, $height, $crop);
  307. return $previewFolder->getFile($path);
  308. }
  309. /**
  310. * Get the specific preview folder for this file
  311. *
  312. * @param File $file
  313. * @return ISimpleFolder
  314. */
  315. private function getPreviewFolder(File $file) {
  316. try {
  317. $folder = $this->appData->getFolder($file->getId());
  318. } catch (NotFoundException $e) {
  319. $folder = $this->appData->newFolder($file->getId());
  320. }
  321. return $folder;
  322. }
  323. }