Commit fca9d325 authored by Joas Schilling's avatar Joas Schilling
Browse files

Move registration of core preview providers to preview

So the class files only have class code and don't execute code
parent 05a069c1
...@@ -21,7 +21,7 @@ require_once 'preview/movie.php'; ...@@ -21,7 +21,7 @@ require_once 'preview/movie.php';
require_once 'preview/mp3.php'; require_once 'preview/mp3.php';
require_once 'preview/svg.php'; require_once 'preview/svg.php';
require_once 'preview/txt.php'; require_once 'preview/txt.php';
require_once 'preview/office.php'; require_once 'preview/office-cl.php';
require_once 'preview/bitmap.php'; require_once 'preview/bitmap.php';
class Preview { class Preview {
...@@ -744,10 +744,11 @@ class Preview { ...@@ -744,10 +744,11 @@ class Preview {
return; return;
} }
if (count(self::$providers) > 0) { if (!empty(self::$providers)) {
return; return;
} }
self::registerCoreProviders();
foreach (self::$registeredProviders as $provider) { foreach (self::$registeredProviders as $provider) {
$class = $provider['class']; $class = $provider['class'];
$options = $provider['options']; $options = $provider['options'];
...@@ -759,7 +760,74 @@ class Preview { ...@@ -759,7 +760,74 @@ class Preview {
$keys = array_map('strlen', array_keys(self::$providers)); $keys = array_map('strlen', array_keys(self::$providers));
array_multisort($keys, SORT_DESC, self::$providers); array_multisort($keys, SORT_DESC, self::$providers);
}
protected static function registerCoreProviders() {
self::registerProvider('OC\Preview\TXT');
self::registerProvider('OC\Preview\MarkDown');
self::registerProvider('OC\Preview\Image');
self::registerProvider('OC\Preview\MP3');
// SVG, Office and Bitmap require imagick
if (extension_loaded('imagick')) {
$checkImagick = new \Imagick();
$imagickProviders = array(
'SVG' => 'OC\Preview\SVG',
'TIFF' => 'OC\Preview\TIFF',
'PDF' => 'OC\Preview\PDF',
'AI' => 'OC\Preview\Illustrator',
'PSD' => 'OC\Preview\Photoshop',
// Requires adding 'eps' => array('application/postscript', null), to lib/private/mimetypes.list.php
'EPS' => 'OC\Preview\Postscript',
);
foreach ($imagickProviders as $queryFormat => $provider) {
if (count($checkImagick->queryFormats($queryFormat)) === 1) {
self::registerProvider($provider);
}
}
if (count($checkImagick->queryFormats('PDF')) === 1) {
// Office previews are currently not supported on Windows
if (!\OC_Util::runningOnWindows() && \OC_Helper::is_function_enabled('shell_exec')) {
$officeFound = is_string(\OC::$server->getConfig()->getSystemValue('preview_libreoffice_path', null));
if (!$officeFound) {
//let's see if there is libreoffice or openoffice on this machine
$whichLibreOffice = shell_exec('command -v libreoffice');
$officeFound = !empty($whichLibreOffice);
if (!$officeFound) {
$whichOpenOffice = shell_exec('command -v openoffice');
$officeFound = !empty($whichOpenOffice);
}
}
if ($officeFound) {
self::registerProvider('OC\Preview\MSOfficeDoc');
self::registerProvider('OC\Preview\MSOffice2003');
self::registerProvider('OC\Preview\MSOffice2007');
self::registerProvider('OC\Preview\OpenDocument');
self::registerProvider('OC\Preview\StarOffice');
}
}
}
}
// Video requires avconv or ffmpeg and is therefor
// currently not supported on Windows.
if (!\OC_Util::runningOnWindows()) {
$avconvBinary = \OC_Helper::findBinaryPath('avconv');
$ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
if ($avconvBinary || $ffmpegBinary) {
// FIXME // a bit hacky but didn't want to use subclasses
\OC\Preview\Movie::$avconvBinary = $avconvBinary;
\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
self::registerProvider('OC\Preview\Movie');
}
}
} }
/** /**
......
...@@ -9,10 +9,6 @@ namespace OC\Preview; ...@@ -9,10 +9,6 @@ namespace OC\Preview;
use Imagick; use Imagick;
if (extension_loaded('imagick')) {
$checkImagick = new Imagick();
class Bitmap extends Provider { class Bitmap extends Provider {
public function getMimeType() { public function getMimeType() {
...@@ -23,11 +19,11 @@ if (extension_loaded('imagick')) { ...@@ -23,11 +19,11 @@ if (extension_loaded('imagick')) {
$tmpPath = $fileview->toTmpFile($path); $tmpPath = $fileview->toTmpFile($path);
//create imagick object from bitmap or vector file //create imagick object from bitmap or vector file
try{ try {
// Layer 0 contains either the bitmap or // Layer 0 contains either the bitmap or
// a flat representation of all vector layers // a flat representation of all vector layers
$bp = new Imagick($tmpPath . '[0]'); $bp = new Imagick($tmpPath . '[0]');
$bp->setImageFormat('png'); $bp->setImageFormat('png');
} catch (\Exception $e) { } catch (\Exception $e) {
\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR); \OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
...@@ -35,17 +31,14 @@ if (extension_loaded('imagick')) { ...@@ -35,17 +31,14 @@ if (extension_loaded('imagick')) {
} }
unlink($tmpPath); unlink($tmpPath);
//new bitmap image object //new bitmap image object
$image = new \OC_Image($bp); $image = new \OC_Image($bp);
//check if image object is valid //check if image object is valid
return $image->valid() ? $image : false; return $image->valid() ? $image : false;
} }
} }
if(count($checkImagick->queryFormats('PDF')) === 1) {
//.pdf //.pdf
class PDF extends Bitmap { class PDF extends Bitmap {
...@@ -54,12 +47,7 @@ if (extension_loaded('imagick')) { ...@@ -54,12 +47,7 @@ if (extension_loaded('imagick')) {
} }
} }
\OC\Preview::registerProvider('OC\Preview\PDF');
}
if(count($checkImagick->queryFormats('TIFF')) === 1) {
//.tiff //.tiff
class TIFF extends Bitmap { class TIFF extends Bitmap {
...@@ -68,12 +56,7 @@ if (extension_loaded('imagick')) { ...@@ -68,12 +56,7 @@ if (extension_loaded('imagick')) {
} }
} }
\OC\Preview::registerProvider('OC\Preview\TIFF');
}
if(count($checkImagick->queryFormats('AI')) === 1) {
//.ai //.ai
class Illustrator extends Bitmap { class Illustrator extends Bitmap {
...@@ -82,12 +65,6 @@ if (extension_loaded('imagick')) { ...@@ -82,12 +65,6 @@ if (extension_loaded('imagick')) {
} }
} }
\OC\Preview::registerProvider('OC\Preview\Illustrator');
}
// Requires adding 'eps' => array('application/postscript', null), to lib/private/mimetypes.list.php
if(count($checkImagick->queryFormats('EPS')) === 1) {
//.eps //.eps
class Postscript extends Bitmap { class Postscript extends Bitmap {
...@@ -98,11 +75,6 @@ if (extension_loaded('imagick')) { ...@@ -98,11 +75,6 @@ if (extension_loaded('imagick')) {
} }
\OC\Preview::registerProvider('OC\Preview\Postscript');
}
if(count($checkImagick->queryFormats('PSD')) === 1) {
//.psd //.psd
class Photoshop extends Bitmap { class Photoshop extends Bitmap {
...@@ -111,7 +83,3 @@ if (extension_loaded('imagick')) { ...@@ -111,7 +83,3 @@ if (extension_loaded('imagick')) {
} }
} }
\OC\Preview::registerProvider('OC\Preview\Photoshop');
}
}
...@@ -35,5 +35,3 @@ class Image extends Provider { ...@@ -35,5 +35,3 @@ class Image extends Provider {
} }
} }
\OC\Preview::registerProvider('OC\Preview\Image');
...@@ -8,13 +8,6 @@ ...@@ -8,13 +8,6 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
// movie preview is currently not supported on Windows
if (!\OC_Util::runningOnWindows()) {
$avconvBinary = \OC_Helper::findBinaryPath('avconv');
$ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
if ($avconvBinary || $ffmpegBinary) {
class Movie extends Provider { class Movie extends Provider {
public static $avconvBinary; public static $avconvBinary;
public static $ffmpegBinary; public static $ffmpegBinary;
...@@ -95,12 +88,3 @@ if (!\OC_Util::runningOnWindows()) { ...@@ -95,12 +88,3 @@ if (!\OC_Util::runningOnWindows()) {
return false; return false;
} }
} }
// a bit hacky but didn't want to use subclasses
Movie::$avconvBinary = $avconvBinary;
Movie::$ffmpegBinary = $ffmpegBinary;
\OC\Preview::registerProvider('OC\Preview\Movie');
}
}
...@@ -44,5 +44,3 @@ class MP3 extends Provider { ...@@ -44,5 +44,3 @@ class MP3 extends Provider {
} }
} }
\OC\Preview::registerProvider('OC\Preview\MP3');
...@@ -7,9 +7,6 @@ ...@@ -7,9 +7,6 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
// office preview is currently not supported on Windows
if (!\OC_Util::runningOnWindows()) {
//we need imagick to convert //we need imagick to convert
class Office extends Provider { class Office extends Provider {
...@@ -90,8 +87,6 @@ if (!\OC_Util::runningOnWindows()) { ...@@ -90,8 +87,6 @@ if (!\OC_Util::runningOnWindows()) {
} }
\OC\Preview::registerProvider('OC\Preview\MSOfficeDoc');
//.docm, .dotm, .xls(m), .xlt(m), .xla(m), .ppt(m), .pot(m), .pps(m), .ppa(m) //.docm, .dotm, .xls(m), .xlt(m), .xla(m), .ppt(m), .pot(m), .pps(m), .ppa(m)
class MSOffice2003 extends Office { class MSOffice2003 extends Office {
...@@ -101,8 +96,6 @@ if (!\OC_Util::runningOnWindows()) { ...@@ -101,8 +96,6 @@ if (!\OC_Util::runningOnWindows()) {
} }
\OC\Preview::registerProvider('OC\Preview\MSOffice2003');
//.docx, .dotx, .xlsx, .xltx, .pptx, .potx, .ppsx //.docx, .dotx, .xlsx, .xltx, .pptx, .potx, .ppsx
class MSOffice2007 extends Office { class MSOffice2007 extends Office {
...@@ -112,8 +105,6 @@ if (!\OC_Util::runningOnWindows()) { ...@@ -112,8 +105,6 @@ if (!\OC_Util::runningOnWindows()) {
} }
\OC\Preview::registerProvider('OC\Preview\MSOffice2007');
//.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt //.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt
class OpenDocument extends Office { class OpenDocument extends Office {
...@@ -123,8 +114,6 @@ if (!\OC_Util::runningOnWindows()) { ...@@ -123,8 +114,6 @@ if (!\OC_Util::runningOnWindows()) {
} }
\OC\Preview::registerProvider('OC\Preview\OpenDocument');
//.sxw, .stw, .sxc, .stc, .sxd, .std, .sxi, .sti, .sxg, .sxm //.sxw, .stw, .sxc, .stc, .sxd, .std, .sxi, .sti, .sxg, .sxm
class StarOffice extends Office { class StarOffice extends Office {
...@@ -133,6 +122,3 @@ if (!\OC_Util::runningOnWindows()) { ...@@ -133,6 +122,3 @@ if (!\OC_Util::runningOnWindows()) {
} }
} }
\OC\Preview::registerProvider('OC\Preview\StarOffice');
}
<?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.
*/
//both, libreoffice backend and php fallback, need imagick
if (extension_loaded('imagick')) {
$checkImagick = new Imagick();
if(count($checkImagick->queryFormats('PDF')) === 1) {
$isShellExecEnabled = \OC_Helper::is_function_enabled('shell_exec');
// LibreOffice preview is currently not supported on Windows
if (!\OC_Util::runningOnWindows()) {
$whichLibreOffice = ($isShellExecEnabled ? shell_exec('command -v libreoffice') : '');
$isLibreOfficeAvailable = !empty($whichLibreOffice);
$whichOpenOffice = ($isShellExecEnabled ? shell_exec('command -v openoffice') : '');
$isOpenOfficeAvailable = !empty($whichOpenOffice);
//let's see if there is libreoffice or openoffice on this machine
if($isShellExecEnabled && ($isLibreOfficeAvailable || $isOpenOfficeAvailable || is_string(\OC_Config::getValue('preview_libreoffice_path', null)))) {
require_once('office-cl.php');
}
}
}
}
...@@ -5,18 +5,21 @@ abstract class Provider { ...@@ -5,18 +5,21 @@ abstract class Provider {
private $options; private $options;
public function __construct($options) { public function __construct($options) {
$this->options=$options; $this->options = $options;
} }
/**
* @return string Regex with the mimetypes that are supported by this provider
*/
abstract public function getMimeType(); abstract public function getMimeType();
/** /**
* Check if a preview can be generated for $path * Check if a preview can be generated for $path
* *
* @param string $path * @param \OC\Files\FileInfo $file
* @return bool * @return bool
*/ */
public function isAvailable($path) { public function isAvailable($file) {
return true; return true;
} }
...@@ -32,5 +35,4 @@ abstract class Provider { ...@@ -32,5 +35,4 @@ abstract class Provider {
* OC_Image object of the preview * OC_Image object of the preview
*/ */
abstract public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview); abstract public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview);
} }
...@@ -9,12 +9,6 @@ namespace OC\Preview; ...@@ -9,12 +9,6 @@ namespace OC\Preview;
use Imagick; use Imagick;
if (extension_loaded('imagick')) {
$checkImagick = new Imagick();
if(count($checkImagick->queryFormats('SVG')) === 1) {
class SVG extends Provider { class SVG extends Provider {
public function getMimeType() { public function getMimeType() {
...@@ -50,9 +44,4 @@ if (extension_loaded('imagick')) { ...@@ -50,9 +44,4 @@ if (extension_loaded('imagick')) {
//check if image object is valid //check if image object is valid
return $image->valid() ? $image : false; return $image->valid() ? $image : false;
} }
} }
\OC\Preview::registerProvider('OC\Preview\SVG');
}
}
...@@ -8,28 +8,22 @@ ...@@ -8,28 +8,22 @@
namespace OC\Preview; namespace OC\Preview;
class TXT extends Provider { class TXT extends Provider {
/**
* {@inheritDoc}
*/
public function getMimeType() { public function getMimeType() {
return '/text\/plain/'; return '/text\/plain/';
} }
/** /**
* Check if a preview can be generated for $path * {@inheritDoc}
*
* @param \OC\Files\FileInfo $file
* @return bool
*/ */
public function isAvailable($file) { public function isAvailable($file) {
return $file->getSize() > 5; return $file->getSize() > 5;
} }
/** /**
* @param string $path * {@inheritDoc}
* @param int $maxX
* @param int $maxY
* @param boolean $scalingup
* @param \OC\Files\View $fileview
* @return bool|\OC_Image
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
...@@ -80,14 +74,9 @@ class TXT extends Provider { ...@@ -80,14 +74,9 @@ class TXT extends Provider {
} }
} }
\OC\Preview::registerProvider('OC\Preview\TXT');
class MarkDown extends TXT { class MarkDown extends TXT {
public function getMimeType() { public function getMimeType() {
return '/text\/(x-)?markdown/'; return '/text\/(x-)?markdown/';
} }
} }
\OC\Preview::registerProvider('OC\Preview\MarkDown');
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment