any preview requiring the which command will not be used on Windows

This commit is contained in:
Thomas Müller 2013-10-15 00:15:45 +02:00
parent 86f6f0ca92
commit e3489b36ff
3 changed files with 144 additions and 129 deletions

View File

@ -8,40 +8,44 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
$isShellExecEnabled = !in_array('shell_exec', explode(', ', ini_get('disable_functions'))); // movie preview is currently not supported on Windows
$whichAVCONV = shell_exec('which avconv'); if (!\OC_Util::runningOnWindows()) {
$isAVCONVAvailable = !empty($whichAVCONV); $isShellExecEnabled = !in_array('shell_exec', explode(', ', ini_get('disable_functions')));
$whichAVCONV = shell_exec('which avconv');
$isAVCONVAvailable = !empty($whichAVCONV);
if($isShellExecEnabled && $isAVCONVAvailable) { if($isShellExecEnabled && $isAVCONVAvailable) {
class Movie extends Provider { class Movie extends Provider {
public function getMimeType() { public function getMimeType() {
return '/video\/.*/'; return '/video\/.*/';
}
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
$absPath = \OC_Helper::tmpFile();
$tmpPath = \OC_Helper::tmpFile();
$handle = $fileview->fopen($path, 'rb');
$firstmb = stream_get_contents($handle, 1048576); //1024 * 1024 = 1048576
file_put_contents($absPath, $firstmb);
//$cmd = 'ffmpeg -y -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1 -ss 1 -s ' . escapeshellarg($maxX) . 'x' . escapeshellarg($maxY) . ' ' . $tmpPath;
$cmd = 'avconv -an -y -ss 1 -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1 ' . escapeshellarg($tmpPath);
shell_exec($cmd);
$image = new \OC_Image($tmpPath);
unlink($absPath);
unlink($tmpPath);
return $image->valid() ? $image : false;
}
} }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { \OC\Preview::registerProvider('OC\Preview\Movie');
$absPath = \OC_Helper::tmpFile();
$tmpPath = \OC_Helper::tmpFile();
$handle = $fileview->fopen($path, 'rb');
$firstmb = stream_get_contents($handle, 1048576); //1024 * 1024 = 1048576
file_put_contents($absPath, $firstmb);
//$cmd = 'ffmpeg -y -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1 -ss 1 -s ' . escapeshellarg($maxX) . 'x' . escapeshellarg($maxY) . ' ' . $tmpPath;
$cmd = 'avconv -an -y -ss 1 -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1 ' . escapeshellarg($tmpPath);
shell_exec($cmd);
$image = new \OC_Image($tmpPath);
unlink($absPath);
unlink($tmpPath);
return $image->valid() ? $image : false;
}
} }
}
\OC\Preview::registerProvider('OC\Preview\Movie');
}

View File

@ -7,128 +7,132 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
//we need imagick to convert // office preview is currently not supported on Windows
class Office extends Provider { if (!\OC_Util::runningOnWindows()) {
private $cmd; //we need imagick to convert
class Office extends Provider {
public function getMimeType() { private $cmd;
return null;
}
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getMimeType() {
$this->initCmd(); return null;
if(is_null($this->cmd)) {
return false;
} }
$absPath = $fileview->toTmpFile($path); public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
$this->initCmd();
if(is_null($this->cmd)) {
return false;
}
$tmpDir = get_temp_dir(); $absPath = $fileview->toTmpFile($path);
$defaultParameters = ' --headless --nologo --nofirststartwizard --invisible --norestore -convert-to pdf -outdir '; $tmpDir = get_temp_dir();
$clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters);
$exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath); $defaultParameters = ' --headless --nologo --nofirststartwizard --invisible --norestore -convert-to pdf -outdir ';
$export = 'export HOME=/' . $tmpDir; $clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters);
shell_exec($export . "\n" . $exec); $exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
$export = 'export HOME=/' . $tmpDir;
shell_exec($export . "\n" . $exec);
//create imagick object from pdf
try{
$pdf = new \imagick($absPath . '.pdf' . '[0]');
$pdf->setImageFormat('jpg');
} catch (\Exception $e) {
unlink($absPath);
unlink($absPath . '.pdf');
\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
return false;
}
$image = new \OC_Image($pdf);
//create imagick object from pdf
try{
$pdf = new \imagick($absPath . '.pdf' . '[0]');
$pdf->setImageFormat('jpg');
} catch (\Exception $e) {
unlink($absPath); unlink($absPath);
unlink($absPath . '.pdf'); unlink($absPath . '.pdf');
\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
return false; return $image->valid() ? $image : false;
} }
$image = new \OC_Image($pdf); private function initCmd() {
$cmd = '';
unlink($absPath); if(is_string(\OC_Config::getValue('preview_libreoffice_path', null))) {
unlink($absPath . '.pdf'); $cmd = \OC_Config::getValue('preview_libreoffice_path', null);
}
return $image->valid() ? $image : false; $whichLibreOffice = shell_exec('which libreoffice');
if($cmd === '' && !empty($whichLibreOffice)) {
$cmd = 'libreoffice';
}
$whichOpenOffice = shell_exec('which openoffice');
if($cmd === '' && !empty($whichOpenOffice)) {
$cmd = 'openoffice';
}
if($cmd === '') {
$cmd = null;
}
$this->cmd = $cmd;
}
} }
private function initCmd() { //.doc, .dot
$cmd = ''; class MSOfficeDoc extends Office {
if(is_string(\OC_Config::getValue('preview_libreoffice_path', null))) { public function getMimeType() {
$cmd = \OC_Config::getValue('preview_libreoffice_path', null); return '/application\/msword/';
} }
$whichLibreOffice = shell_exec('which libreoffice'); }
if($cmd === '' && !empty($whichLibreOffice)) {
$cmd = 'libreoffice'; \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-.*/';
} }
$whichOpenOffice = shell_exec('which openoffice'); }
if($cmd === '' && !empty($whichOpenOffice)) {
$cmd = 'openoffice'; \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.*/';
} }
if($cmd === '') { }
$cmd = null;
\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.*/';
} }
$this->cmd = $cmd;
}
}
//.doc, .dot
class MSOfficeDoc extends Office {
public function getMimeType() {
return '/application\/msword/';
} }
} \OC\Preview::registerProvider('OC\Preview\OpenDocument');
\OC\Preview::registerProvider('OC\Preview\MSOfficeDoc'); //.sxw, .stw, .sxc, .stc, .sxd, .std, .sxi, .sti, .sxg, .sxm
class StarOffice extends Office {
//.docm, .dotm, .xls(m), .xlt(m), .xla(m), .ppt(m), .pot(m), .pps(m), .ppa(m) public function getMimeType() {
class MSOffice2003 extends Office { return '/application\/vnd.sun.xml.*/';
}
public function getMimeType() {
return '/application\/vnd.ms-.*/';
} }
\OC\Preview::registerProvider('OC\Preview\StarOffice');
} }
\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');

View File

@ -8,15 +8,22 @@
//both, libreoffice backend and php fallback, need imagick //both, libreoffice backend and php fallback, need imagick
if (extension_loaded('imagick')) { if (extension_loaded('imagick')) {
$isShellExecEnabled = !in_array('shell_exec', explode(', ', ini_get('disable_functions'))); $isShellExecEnabled = !in_array('shell_exec', explode(', ', ini_get('disable_functions')));
$whichLibreOffice = shell_exec('which libreoffice');
$isLibreOfficeAvailable = !empty($whichLibreOffice); // movie preview is currently not supported on Windows
$whichOpenOffice = shell_exec('which libreoffice'); if (!\OC_Util::runningOnWindows()) {
$isOpenOfficeAvailable = !empty($whichOpenOffice); $whichLibreOffice = shell_exec('which libreoffice');
//let's see if there is libreoffice or openoffice on this machine $isLibreOfficeAvailable = !empty($whichLibreOffice);
if($isShellExecEnabled && ($isLibreOfficeAvailable || $isOpenOfficeAvailable || is_string(\OC_Config::getValue('preview_libreoffice_path', null)))) { $whichOpenOffice = shell_exec('which libreoffice');
require_once('office-cl.php'); $isOpenOfficeAvailable = !empty($whichOpenOffice);
}else{ //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');
}else{
//in case there isn't, use our fallback
require_once('office-fallback.php');
}
} else {
//in case there isn't, use our fallback //in case there isn't, use our fallback
require_once('office-fallback.php'); require_once('office-fallback.php');
} }
} }