123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- /**
- * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace OC\Preview;
- use OCP\Files\File;
- use OCP\Files\IAppData;
- use OCP\Files\NotFoundException;
- use OCP\Files\NotPermittedException;
- use OCP\Files\SimpleFS\ISimpleFile;
- use OCP\Files\SimpleFS\ISimpleFolder;
- use OCP\IConfig;
- use OCP\IImage;
- use OCP\IPreview;
- use OCP\Preview\IProvider;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Symfony\Component\EventDispatcher\GenericEvent;
- class Generator {
- /** @var IPreview */
- private $previewManager;
- /** @var IConfig */
- private $config;
- /** @var IAppData */
- private $appData;
- /** @var GeneratorHelper */
- private $helper;
- /** @var EventDispatcherInterface */
- private $eventDispatcher;
- /**
- * @param IConfig $config
- * @param IPreview $previewManager
- * @param IAppData $appData
- * @param GeneratorHelper $helper
- * @param EventDispatcherInterface $eventDispatcher
- */
- public function __construct(
- IConfig $config,
- IPreview $previewManager,
- IAppData $appData,
- GeneratorHelper $helper,
- EventDispatcherInterface $eventDispatcher
- ) {
- $this->config = $config;
- $this->previewManager = $previewManager;
- $this->appData = $appData;
- $this->helper = $helper;
- $this->eventDispatcher = $eventDispatcher;
- }
- /**
- * Returns a preview of a file
- *
- * The cache is searched first and if nothing usable was found then a preview is
- * generated by one of the providers
- *
- * @param File $file
- * @param int $width
- * @param int $height
- * @param bool $crop
- * @param string $mode
- * @param string $mimeType
- * @return ISimpleFile
- * @throws NotFoundException
- * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
- */
- public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
- //Make sure that we can read the file
- if (!$file->isReadable()) {
- throw new NotFoundException('Cannot read file');
- }
- $this->eventDispatcher->dispatch(
- IPreview::EVENT,
- new GenericEvent($file,[
- 'width' => $width,
- 'height' => $height,
- 'crop' => $crop,
- 'mode' => $mode
- ])
- );
- if ($mimeType === null) {
- $mimeType = $file->getMimeType();
- }
- if (!$this->previewManager->isMimeSupported($mimeType)) {
- throw new NotFoundException();
- }
- $previewFolder = $this->getPreviewFolder($file);
- // Get the max preview and infer the max preview sizes from that
- $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
- if ($maxPreview->getSize() === 0) {
- $maxPreview->delete();
- throw new NotFoundException('Max preview size 0, invalid!');
- }
- list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);
- // If both width and heigth are -1 we just want the max preview
- if ($width === -1 && $height === -1) {
- $width = $maxWidth;
- $height = $maxHeight;
- }
- // Calculate the preview size
- list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
- // No need to generate a preview that is just the max preview
- if ($width === $maxWidth && $height === $maxHeight) {
- return $maxPreview;
- }
- // Try to get a cached preview. Else generate (and store) one
- try {
- try {
- $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType());
- } catch (NotFoundException $e) {
- $preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
- }
- } catch (\InvalidArgumentException $e) {
- throw new NotFoundException();
- }
- if ($preview->getSize() === 0) {
- $preview->delete();
- throw new NotFoundException('Cached preview size 0, invalid!');
- }
- return $preview;
- }
- /**
- * @param ISimpleFolder $previewFolder
- * @param File $file
- * @param string $mimeType
- * @return ISimpleFile
- * @throws NotFoundException
- */
- private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) {
- $nodes = $previewFolder->getDirectoryListing();
- foreach ($nodes as $node) {
- if (strpos($node->getName(), 'max')) {
- return $node;
- }
- }
- $previewProviders = $this->previewManager->getProviders();
- foreach ($previewProviders as $supportedMimeType => $providers) {
- if (!preg_match($supportedMimeType, $mimeType)) {
- continue;
- }
- foreach ($providers as $provider) {
- $provider = $this->helper->getProvider($provider);
- if (!($provider instanceof IProvider)) {
- continue;
- }
- if (!$provider->isAvailable($file)) {
- continue;
- }
- $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
- $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
- $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
- if (!($preview instanceof IImage)) {
- continue;
- }
- // Try to get the extention.
- try {
- $ext = $this->getExtention($preview->dataMimeType());
- } catch (\InvalidArgumentException $e) {
- // Just continue to the next iteration if this preview doesn't have a valid mimetype
- continue;
- }
- $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
- try {
- $file = $previewFolder->newFile($path);
- $file->putContent($preview->data());
- } catch (NotPermittedException $e) {
- throw new NotFoundException();
- }
- return $file;
- }
- }
- throw new NotFoundException();
- }
- /**
- * @param ISimpleFile $file
- * @return int[]
- */
- private function getPreviewSize(ISimpleFile $file) {
- $size = explode('-', $file->getName());
- return [(int)$size[0], (int)$size[1]];
- }
- /**
- * @param int $width
- * @param int $height
- * @param bool $crop
- * @param string $mimeType
- * @return string
- */
- private function generatePath($width, $height, $crop, $mimeType) {
- $path = (string)$width . '-' . (string)$height;
- if ($crop) {
- $path .= '-crop';
- }
- $ext = $this->getExtention($mimeType);
- $path .= '.' . $ext;
- return $path;
- }
- /**
- * @param int $width
- * @param int $height
- * @param bool $crop
- * @param string $mode
- * @param int $maxWidth
- * @param int $maxHeight
- * @return int[]
- */
- private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
- /*
- * If we are not cropping we have to make sure the requested image
- * respects the aspect ratio of the original.
- */
- if (!$crop) {
- $ratio = $maxHeight / $maxWidth;
- if ($width === -1) {
- $width = $height / $ratio;
- }
- if ($height === -1) {
- $height = $width * $ratio;
- }
- $ratioH = $height / $maxHeight;
- $ratioW = $width / $maxWidth;
- /*
- * Fill means that the $height and $width are the max
- * Cover means min.
- */
- if ($mode === IPreview::MODE_FILL) {
- if ($ratioH > $ratioW) {
- $height = $width * $ratio;
- } else {
- $width = $height / $ratio;
- }
- } else if ($mode === IPreview::MODE_COVER) {
- if ($ratioH > $ratioW) {
- $width = $height / $ratio;
- } else {
- $height = $width * $ratio;
- }
- }
- }
- if ($height !== $maxHeight && $width !== $maxWidth) {
- /*
- * Scale to the nearest power of four
- */
- $pow4height = 4 ** ceil(log($height) / log(4));
- $pow4width = 4 ** ceil(log($width) / log(4));
- // Minimum size is 64
- $pow4height = max($pow4height, 64);
- $pow4width = max($pow4width, 64);
- $ratioH = $height / $pow4height;
- $ratioW = $width / $pow4width;
- if ($ratioH < $ratioW) {
- $width = $pow4width;
- $height /= $ratioW;
- } else {
- $height = $pow4height;
- $width /= $ratioH;
- }
- }
- /*
- * Make sure the requested height and width fall within the max
- * of the preview.
- */
- if ($height > $maxHeight) {
- $ratio = $height / $maxHeight;
- $height = $maxHeight;
- $width /= $ratio;
- }
- if ($width > $maxWidth) {
- $ratio = $width / $maxWidth;
- $width = $maxWidth;
- $height /= $ratio;
- }
- return [(int)round($width), (int)round($height)];
- }
- /**
- * @param ISimpleFolder $previewFolder
- * @param ISimpleFile $maxPreview
- * @param int $width
- * @param int $height
- * @param bool $crop
- * @param int $maxWidth
- * @param int $maxHeight
- * @return ISimpleFile
- * @throws NotFoundException
- * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
- */
- private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
- $preview = $this->helper->getImage($maxPreview);
- if (!$preview->valid()) {
- throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
- }
- if ($crop) {
- if ($height !== $preview->height() && $width !== $preview->width()) {
- //Resize
- $widthR = $preview->width() / $width;
- $heightR = $preview->height() / $height;
- if ($widthR > $heightR) {
- $scaleH = $height;
- $scaleW = $maxWidth / $heightR;
- } else {
- $scaleH = $maxHeight / $widthR;
- $scaleW = $width;
- }
- $preview->preciseResize((int)round($scaleW), (int)round($scaleH));
- }
- $cropX = (int)floor(abs($width - $preview->width()) * 0.5);
- $cropY = 0;
- $preview->crop($cropX, $cropY, $width, $height);
- } else {
- $preview->resize(max($width, $height));
- }
- $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
- try {
- $file = $previewFolder->newFile($path);
- $file->putContent($preview->data());
- } catch (NotPermittedException $e) {
- throw new NotFoundException();
- }
- return $file;
- }
- /**
- * @param ISimpleFolder $previewFolder
- * @param int $width
- * @param int $height
- * @param bool $crop
- * @param string $mimeType
- * @return ISimpleFile
- *
- * @throws NotFoundException
- */
- private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
- $path = $this->generatePath($width, $height, $crop, $mimeType);
- return $previewFolder->getFile($path);
- }
- /**
- * Get the specific preview folder for this file
- *
- * @param File $file
- * @return ISimpleFolder
- */
- private function getPreviewFolder(File $file) {
- try {
- $folder = $this->appData->getFolder($file->getId());
- } catch (NotFoundException $e) {
- $folder = $this->appData->newFolder($file->getId());
- }
- return $folder;
- }
- /**
- * @param string $mimeType
- * @return null|string
- * @throws \InvalidArgumentException
- */
- private function getExtention($mimeType) {
- switch ($mimeType) {
- case 'image/png':
- return 'png';
- case 'image/jpeg':
- return 'jpg';
- case 'image/gif':
- return 'gif';
- default:
- throw new \InvalidArgumentException('Not a valid mimetype');
- }
- }
- }
|