diff options
Diffstat (limited to 'lib/private/Preview')
25 files changed, 1247 insertions, 0 deletions
diff --git a/lib/private/Preview/BMP.php b/lib/private/Preview/BMP.php new file mode 100644 index 00000000000..da13cd9e5b8 --- /dev/null +++ b/lib/private/Preview/BMP.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Olivier Paroz <github@oparoz.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +class BMP extends Image { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/image\/bmp/'; + } +} diff --git a/lib/private/Preview/Bitmap.php b/lib/private/Preview/Bitmap.php new file mode 100644 index 00000000000..34bc2f93fc7 --- /dev/null +++ b/lib/private/Preview/Bitmap.php @@ -0,0 +1,114 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Olivier Paroz <github@oparoz.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +use Imagick; + +/** + * Creates a PNG preview using ImageMagick via the PECL extension + * + * @package OC\Preview + */ +abstract class Bitmap extends Provider { + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + + $tmpPath = $fileview->toTmpFile($path); + if (!$tmpPath) { + return false; + } + + // Creates \Imagick object from bitmap or vector file + try { + $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); + } catch (\Exception $e) { + \OCP\Util::writeLog('core', 'ImageMagick says: ' . $e->getmessage(), \OCP\Util::ERROR); + return false; + } + + unlink($tmpPath); + + //new bitmap image object + $image = new \OC_Image(); + $image->loadFromData($bp); + //check if image object is valid + return $image->valid() ? $image : false; + } + + /** + * Returns a preview of maxX times maxY dimensions in PNG format + * + * * The default resolution is already 72dpi, no need to change it for a bitmap output + * * It's possible to have proper colour conversion using profileimage(). + * ICC profiles are here: http://www.color.org/srgbprofiles.xalter + * * It's possible to Gamma-correct an image via gammaImage() + * + * @param string $tmpPath the location of the file to convert + * @param int $maxX + * @param int $maxY + * + * @return \Imagick + */ + private function getResizedPreview($tmpPath, $maxX, $maxY) { + $bp = new Imagick(); + + // Layer 0 contains either the bitmap or a flat representation of all vector layers + $bp->readImage($tmpPath . '[0]'); + + $bp = $this->resize($bp, $maxX, $maxY); + + $bp->setImageFormat('png'); + + return $bp; + } + + /** + * Returns a resized \Imagick object + * + * If you want to know more on the various methods available to resize an + * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im + * + * @param \Imagick $bp + * @param int $maxX + * @param int $maxY + * + * @return \Imagick + */ + private function resize($bp, $maxX, $maxY) { + list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry()); + + // We only need to resize a preview which doesn't fit in the maximum dimensions + if ($previewWidth > $maxX || $previewHeight > $maxY) { + // TODO: LANCZOS is the default filter, CATROM could bring similar results faster + $bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true); + } + + return $bp; + } + +} diff --git a/lib/private/Preview/Font.php b/lib/private/Preview/Font.php new file mode 100644 index 00000000000..caac2923789 --- /dev/null +++ b/lib/private/Preview/Font.php @@ -0,0 +1,32 @@ +<?php +/** + * @author Olivier Paroz <github@oparoz.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +// .otf, .ttf and .pfb +class Font extends Bitmap { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/(?:font-sfnt|x-font$)/'; + } +}
\ No newline at end of file diff --git a/lib/private/Preview/GIF.php b/lib/private/Preview/GIF.php new file mode 100644 index 00000000000..0716a6f4406 --- /dev/null +++ b/lib/private/Preview/GIF.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Olivier Paroz <github@oparoz.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +class GIF extends Image { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/image\/gif/'; + } +} diff --git a/lib/private/Preview/Illustrator.php b/lib/private/Preview/Illustrator.php new file mode 100644 index 00000000000..ef8448d7b53 --- /dev/null +++ b/lib/private/Preview/Illustrator.php @@ -0,0 +1,33 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +//.ai +class Illustrator extends Bitmap { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/illustrator/'; + } +} diff --git a/lib/private/Preview/Image.php b/lib/private/Preview/Image.php new file mode 100644 index 00000000000..3ea99d6963a --- /dev/null +++ b/lib/private/Preview/Image.php @@ -0,0 +1,68 @@ +<?php +/** + * @author Georg Ehrke <georg@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Olivier Paroz <github@oparoz.com> + * @author Robin Appelman <icewind@owncloud.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * @author Thomas Tanghus <thomas@tanghus.net> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +abstract class Image extends Provider { + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + //get fileinfo + $fileInfo = $fileview->getFileInfo($path); + if (!$fileInfo) { + return false; + } + + $maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50); + $size = $fileInfo->getSize(); + + if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) { + return false; + } + + $image = new \OC_Image(); + + $useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal(); + if ($useTempFile) { + $fileName = $fileview->toTmpFile($path); + } else { + $fileName = $fileview->getLocalFile($path); + } + $image->loadFromFile($fileName); + $image->fixOrientation(); + if ($useTempFile) { + unlink($fileName); + } + if ($image->valid()) { + $image->scaleDownToFit($maxX, $maxY); + + return $image; + } + return false; + } + +} diff --git a/lib/private/Preview/JPEG.php b/lib/private/Preview/JPEG.php new file mode 100644 index 00000000000..2ee5dd24419 --- /dev/null +++ b/lib/private/Preview/JPEG.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Olivier Paroz <github@oparoz.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +class JPEG extends Image { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/image\/jpeg/'; + } +} diff --git a/lib/private/Preview/MP3.php b/lib/private/Preview/MP3.php new file mode 100644 index 00000000000..c7b70457afe --- /dev/null +++ b/lib/private/Preview/MP3.php @@ -0,0 +1,81 @@ +<?php +/** + * @author Georg Ehrke <georg@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Olivier Paroz <github@oparoz.com> + * @author Thomas Tanghus <thomas@tanghus.net> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +use ID3Parser\ID3Parser; + +class MP3 extends Provider { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/audio\/mpeg/'; + } + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + $getID3 = new ID3Parser(); + + $tmpPath = $fileview->toTmpFile($path); + $tags = $getID3->analyze($tmpPath); + unlink($tmpPath); + $picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null; + if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) { + $picture = $tags['id3v2']['PIC'][0]['data']; + } + + if(!is_null($picture)) { + $image = new \OC_Image(); + $image->loadFromData($picture); + + if ($image->valid()) { + $image->scaleDownToFit($maxX, $maxY); + + return $image; + } + } + + return $this->getNoCoverThumbnail(); + } + + /** + * Generates a default image when the file has no cover + * + * @return bool|\OCP\IImage false if the default image is missing or invalid + */ + private function getNoCoverThumbnail() { + $icon = \OC::$SERVERROOT . '/core/img/filetypes/audio.png'; + + if(!file_exists($icon)) { + return false; + } + + $image = new \OC_Image(); + $image->loadFromFile($icon); + return $image->valid() ? $image : false; + } + +} diff --git a/lib/private/Preview/MSOffice2003.php b/lib/private/Preview/MSOffice2003.php new file mode 100644 index 00000000000..20dbe13543a --- /dev/null +++ b/lib/private/Preview/MSOffice2003.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +//.docm, .dotm, .xls(m), .xlt(m), .xla(m), .ppt(m), .pot(m), .pps(m), .ppa(m) +class MSOffice2003 extends Office { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/vnd.ms-.*/'; + } +} diff --git a/lib/private/Preview/MSOffice2007.php b/lib/private/Preview/MSOffice2007.php new file mode 100644 index 00000000000..ef6758843f1 --- /dev/null +++ b/lib/private/Preview/MSOffice2007.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +//.docx, .dotx, .xlsx, .xltx, .pptx, .potx, .ppsx +class MSOffice2007 extends Office { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/vnd.openxmlformats-officedocument.*/'; + } +} diff --git a/lib/private/Preview/MSOfficeDoc.php b/lib/private/Preview/MSOfficeDoc.php new file mode 100644 index 00000000000..05d839d508f --- /dev/null +++ b/lib/private/Preview/MSOfficeDoc.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +//.doc, .dot +class MSOfficeDoc extends Office { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/msword/'; + } +} diff --git a/lib/private/Preview/MarkDown.php b/lib/private/Preview/MarkDown.php new file mode 100644 index 00000000000..394af6576c7 --- /dev/null +++ b/lib/private/Preview/MarkDown.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +class MarkDown extends TXT { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/text\/(x-)?markdown/'; + } + +} diff --git a/lib/private/Preview/Movie.php b/lib/private/Preview/Movie.php new file mode 100644 index 00000000000..43a8d674fc9 --- /dev/null +++ b/lib/private/Preview/Movie.php @@ -0,0 +1,113 @@ +<?php +/** + * @author Georg Ehrke <georg@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Olivier Paroz <github@oparoz.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +class Movie extends Provider { + public static $avconvBinary; + public static $ffmpegBinary; + + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/video\/.*/'; + } + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + // TODO: use proc_open() and stream the source file ? + + $fileInfo = $fileview->getFileInfo($path); + $useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted()); + + if ($useFileDirectly) { + $absPath = $fileview->getLocalFile($path); + } else { + $absPath = \OC::$server->getTempManager()->getTemporaryFile(); + + $handle = $fileview->fopen($path, 'rb'); + + // we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB. + // in some cases 1MB was no enough to generate thumbnail + $firstmb = stream_get_contents($handle, 5242880); + file_put_contents($absPath, $firstmb); + } + + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); + if ($result === false) { + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 1); + if ($result === false) { + $result = $this->generateThumbNail($maxX, $maxY, $absPath, 0); + } + } + + if (!$useFileDirectly) { + unlink($absPath); + } + + return $result; + } + + /** + * @param int $maxX + * @param int $maxY + * @param string $absPath + * @param int $second + * @return bool|\OCP\IImage + */ + private function generateThumbNail($maxX, $maxY, $absPath, $second) { + $tmpPath = \OC::$server->getTempManager()->getTemporaryFile(); + + if (self::$avconvBinary) { + $cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) . + ' -i ' . escapeshellarg($absPath) . + ' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) . + ' > /dev/null 2>&1'; + } else { + $cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) . + ' -i ' . escapeshellarg($absPath) . + ' -f mjpeg -vframes 1' . + ' ' . escapeshellarg($tmpPath) . + ' > /dev/null 2>&1'; + } + + exec($cmd, $output, $returnCode); + + if ($returnCode === 0) { + $image = new \OC_Image(); + $image->loadFromFile($tmpPath); + unlink($tmpPath); + if ($image->valid()) { + $image->scaleDownToFit($maxX, $maxY); + + return $image; + } + } + unlink($tmpPath); + return false; + } +} diff --git a/lib/private/Preview/Office.php b/lib/private/Preview/Office.php new file mode 100644 index 00000000000..6496e091b1d --- /dev/null +++ b/lib/private/Preview/Office.php @@ -0,0 +1,104 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Olivier Paroz <github@oparoz.com> + * @author Robin McCorkell <robin@mccorkell.me.uk> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +abstract class Office extends Provider { + private $cmd; + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + $this->initCmd(); + if (is_null($this->cmd)) { + return false; + } + + $absPath = $fileview->toTmpFile($path); + + $tmpDir = \OC::$server->getTempManager()->getTempBaseDir(); + + $defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to pdf --outdir '; + $clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters); + + $exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath); + + shell_exec($exec); + + //create imagick object from pdf + $pdfPreview = null; + try { + list($dirname, , , $filename) = array_values(pathinfo($absPath)); + $pdfPreview = $dirname . '/' . $filename . '.pdf'; + + $pdf = new \imagick($pdfPreview . '[0]'); + $pdf->setImageFormat('jpg'); + } catch (\Exception $e) { + unlink($absPath); + unlink($pdfPreview); + \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR); + return false; + } + + $image = new \OC_Image(); + $image->loadFromData($pdf); + + unlink($absPath); + unlink($pdfPreview); + + if ($image->valid()) { + $image->scaleDownToFit($maxX, $maxY); + + return $image; + } + return false; + + } + + private function initCmd() { + $cmd = ''; + + $libreOfficePath = \OC::$server->getConfig()->getSystemValue('preview_libreoffice_path', null); + if (is_string($libreOfficePath)) { + $cmd = $libreOfficePath; + } + + $whichLibreOffice = shell_exec('command -v libreoffice'); + if ($cmd === '' && !empty($whichLibreOffice)) { + $cmd = 'libreoffice'; + } + + $whichOpenOffice = shell_exec('command -v openoffice'); + if ($cmd === '' && !empty($whichOpenOffice)) { + $cmd = 'openoffice'; + } + + if ($cmd === '') { + $cmd = null; + } + + $this->cmd = $cmd; + } +} diff --git a/lib/private/Preview/OpenDocument.php b/lib/private/Preview/OpenDocument.php new file mode 100644 index 00000000000..0da1e88cafa --- /dev/null +++ b/lib/private/Preview/OpenDocument.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +//.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt +class OpenDocument extends Office { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/vnd.oasis.opendocument.*/'; + } +} diff --git a/lib/private/Preview/PDF.php b/lib/private/Preview/PDF.php new file mode 100644 index 00000000000..6ddf33cdea2 --- /dev/null +++ b/lib/private/Preview/PDF.php @@ -0,0 +1,33 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +//.pdf +class PDF extends Bitmap { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/pdf/'; + } +} diff --git a/lib/private/Preview/PNG.php b/lib/private/Preview/PNG.php new file mode 100644 index 00000000000..5dd9ae484a5 --- /dev/null +++ b/lib/private/Preview/PNG.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Olivier Paroz <github@oparoz.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +class PNG extends Image { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/image\/png/'; + } +} diff --git a/lib/private/Preview/Photoshop.php b/lib/private/Preview/Photoshop.php new file mode 100644 index 00000000000..df91247f072 --- /dev/null +++ b/lib/private/Preview/Photoshop.php @@ -0,0 +1,33 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +//.psd +class Photoshop extends Bitmap { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/x-photoshop/'; + } +} diff --git a/lib/private/Preview/Postscript.php b/lib/private/Preview/Postscript.php new file mode 100644 index 00000000000..edfd43968c2 --- /dev/null +++ b/lib/private/Preview/Postscript.php @@ -0,0 +1,33 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +//.eps +class Postscript extends Bitmap { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/postscript/'; + } +} diff --git a/lib/private/Preview/Provider.php b/lib/private/Preview/Provider.php new file mode 100644 index 00000000000..738d13d7fc8 --- /dev/null +++ b/lib/private/Preview/Provider.php @@ -0,0 +1,67 @@ +<?php +/** + * @author Georg Ehrke <georg@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Jörn Friedrich Dreyer <jfd@butonic.de> + * @author Olivier Paroz <github@oparoz.com> + * @author Robin Appelman <icewind@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +use OCP\Preview\IProvider; + +abstract class Provider implements IProvider { + private $options; + + /** + * Constructor + * + * @param array $options + */ + public function __construct(array $options = []) { + $this->options = $options; + } + + /** + * @return string Regex with the mimetypes that are supported by this provider + */ + abstract public function getMimeType(); + + /** + * Check if a preview can be generated for $path + * + * @param \OCP\Files\FileInfo $file + * @return bool + */ + public function isAvailable(\OCP\Files\FileInfo $file) { + return true; + } + + /** + * Generates thumbnail which fits in $maxX and $maxY and keeps the aspect ratio, for file at path $path + * + * @param string $path Path of file + * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image + * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image + * @param bool $scalingup Disable/Enable upscaling of previews + * @param \OC\Files\View $fileview fileview object of user folder + * @return bool|\OCP\IImage false if no preview was generated + */ + abstract public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview); +} diff --git a/lib/private/Preview/SVG.php b/lib/private/Preview/SVG.php new file mode 100644 index 00000000000..6618c1fbf82 --- /dev/null +++ b/lib/private/Preview/SVG.php @@ -0,0 +1,70 @@ +<?php +/** + * @author Georg Ehrke <georg@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Olivier Paroz <github@oparoz.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +class SVG extends Provider { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/image\/svg\+xml/'; + } + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + try { + $svg = new \Imagick(); + $svg->setBackgroundColor(new \ImagickPixel('transparent')); + + $content = stream_get_contents($fileview->fopen($path, 'r')); + if (substr($content, 0, 5) !== '<?xml') { + $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content; + } + + // Do not parse SVG files with references + if (stripos($content, 'xlink:href') !== false) { + return false; + } + + $svg->readImageBlob($content); + $svg->setImageFormat('png32'); + } catch (\Exception $e) { + \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR); + return false; + } + + //new image object + $image = new \OC_Image(); + $image->loadFromData($svg); + //check if image object is valid + if ($image->valid()) { + $image->scaleDownToFit($maxX, $maxY); + + return $image; + } + return false; + } +} diff --git a/lib/private/Preview/StarOffice.php b/lib/private/Preview/StarOffice.php new file mode 100644 index 00000000000..6ea4efa5144 --- /dev/null +++ b/lib/private/Preview/StarOffice.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +//.sxw, .stw, .sxc, .stc, .sxd, .std, .sxi, .sti, .sxg, .sxm +class StarOffice extends Office { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/application\/vnd.sun.xml.*/'; + } +} diff --git a/lib/private/Preview/TIFF.php b/lib/private/Preview/TIFF.php new file mode 100644 index 00000000000..006ced6aec0 --- /dev/null +++ b/lib/private/Preview/TIFF.php @@ -0,0 +1,33 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +//.tiff +class TIFF extends Bitmap { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/image\/tiff/'; + } +} diff --git a/lib/private/Preview/TXT.php b/lib/private/Preview/TXT.php new file mode 100644 index 00000000000..a27517c9f39 --- /dev/null +++ b/lib/private/Preview/TXT.php @@ -0,0 +1,92 @@ +<?php +/** + * @author Georg Ehrke <georg@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Nmz <nemesiz@nmz.lt> + * @author Robin Appelman <icewind@owncloud.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OC\Preview; + +class TXT extends Provider { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/text\/plain/'; + } + + /** + * {@inheritDoc} + */ + public function isAvailable(\OCP\Files\FileInfo $file) { + return $file->getSize() > 0; + } + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + $content = $fileview->fopen($path, 'r'); + $content = stream_get_contents($content,3000); + + //don't create previews of empty text files + if(trim($content) === '') { + return false; + } + + $lines = preg_split("/\r\n|\n|\r/", $content); + + $fontSize = ($maxX) ? (int) ((5 / 32) * $maxX) : 5; //5px + $lineSize = ceil($fontSize * 1.25); + + $image = imagecreate($maxX, $maxY); + imagecolorallocate($image, 255, 255, 255); + $textColor = imagecolorallocate($image, 0, 0, 0); + + $fontFile = __DIR__; + $fontFile .= '/../../../core'; + $fontFile .= '/fonts/OpenSans-Regular.ttf'; + + $canUseTTF = function_exists('imagettftext'); + + foreach($lines as $index => $line) { + $index = $index + 1; + + $x = (int) 1; + $y = (int) ($index * $lineSize); + + if ($canUseTTF === true) { + imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line); + } else { + $y -= $fontSize; + imagestring($image, 1, $x, $y, $line, $textColor); + } + + if(($index * $lineSize) >= $maxY) { + break; + } + } + + $image = new \OC_Image($image); + + return $image->valid() ? $image : false; + } +} diff --git a/lib/private/Preview/XBitmap.php b/lib/private/Preview/XBitmap.php new file mode 100644 index 00000000000..604a51a6a83 --- /dev/null +++ b/lib/private/Preview/XBitmap.php @@ -0,0 +1,31 @@ +<?php +/** + * @author Olivier Paroz <github@oparoz.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +class XBitmap extends Image { + /** + * {@inheritDoc} + */ + public function getMimeType() { + return '/image\/x-xbitmap/'; + } +} |