diff --git a/lib/preview/libreoffice-cl.php b/lib/preview/libreoffice-cl.php index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1408e69e8cfdffa953d5846e1827b0ecd9c3c1f1 100644 --- a/lib/preview/libreoffice-cl.php +++ b/lib/preview/libreoffice-cl.php @@ -0,0 +1,129 @@ +<?php +/** + * Copyright (c) 2013 Georg Ehrke georg@ownCloud.com + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +namespace OC\Preview; + +//we need imagick to convert +if (extension_loaded('imagick')) { + + class Office extends Provider { + + private $cmd; + + public function getMimeType() { + return null; + } + + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + $this->initCmd(); + if(is_null($this->cmd)) { + return false; + } + + $abspath = $fileview->toTmpFile($path); + + chdir(get_temp_dir()); + + $exec = 'libreoffice --headless -convert-to pdf ' . escapeshellarg($abspath); + exec($exec) + + //create imagick object from pdf + try{ + $pdf = new \imagick($abspath . '.pdf' . '[0]'); + $pdf->setImageFormat('jpg'); + }catch(\Exception $e){ + \OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR); + return false; + } + + $image = new \OC_Image($pdf); + + unlink($abspath); + unlink($tmppath); + if (!$image->valid()) return false; + + return $image; + } + + private function initCmd() { + $cmd = ''; + + if(is_string(\OC_Config::getValue('preview_libreoffice_path', null))) { + $cmd = \OC_Config::getValue('preview_libreoffice_path', null); + } + + if($cmd === '' && shell_exec('libreoffice --headless --version')) { + $cmd = 'libreoffice'; + } + + if($cmd === '' && shell_exec('openoffice --headless --version')) { + $cmd = 'openoffice'; + } + + if($cmd === '') { + $cmd = null; + } + + $this->cmd = $cmd; + } + } +} + +//.doc, .dot +class MSOfficeDoc extends Office { + + public function getMimeType() { + return '/application\/msword/'; + } + +} + +\OC\Preview::registerProvider('OC\Preview\MSOfficeDoc'); + +//.docm, .dotm, .xls(m), .xlt(m), .xla(m), .ppt(m), .pot(m), .pps(m), .ppa(m) +class MSOffice2003 extends Office { + + public function getMimeType() { + return '/application\/vnd.ms-.*/'; + } + +} + +\OC\Preview::registerProvider('OC\Preview\MSOffice2003'); + +//.docx, .dotx, .xlsx, .xltx, .pptx, .potx, .ppsx +class MSOffice2007 extends Office { + + public function getMimeType() { + return '/application\/vnd.openxmlformats-officedocument.*/'; + } + +} + +\OC\Preview::registerProvider('OC\Preview\MSOffice2007'); + +//.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt +class OpenDocument extends Office { + + public function getMimeType() { + return '/application\/vnd.oasis.opendocument.*/'; + } + +} + +\OC\Preview::registerProvider('OC\Preview\OpenDocument'); + +//.sxw, .stw, .sxc, .stc, .sxd, .std, .sxi, .sti, .sxg, .sxm +class StarOffice extends Office { + + public function getMimeType() { + return '/application\/vnd.sun.xml.*/'; + } + +} + +\OC\Preview::registerProvider('OC\Preview\StarOffice'); \ No newline at end of file diff --git a/lib/preview/office.php b/lib/preview/office.php index c66f5584f0715bea220622783cdf61948d4ff05a..cc1addf39961fa93b33b4e346bf429464fc07f22 100644 --- a/lib/preview/office.php +++ b/lib/preview/office.php @@ -5,9 +5,11 @@ * later. * See the COPYING-README file. */ -if(shell_exec('libreoffice') || shell_exec('openoffice')) { +//let's see if there is libreoffice or openoffice on this machine +if(shell_exec('libreoffice --headless --version') || shell_exec('openoffice --headless --version') || is_string(\OC_Config::getValue('preview_libreoffice_path', null))) { require_once('libreoffice-cl.php'); }else{ + //in case there isn't, use our fallback require_once('msoffice.php'); require_once('opendocument.php'); } \ No newline at end of file