public function testGetFileSizeViaExec()
 {
     if (!\OC_Helper::is_function_enabled('exec')) {
         $this->markTestSkipped('The exec() function needs to be enabled for this test.');
     }
     $this->assertSame($this->fileSize, $this->helper->getFileSizeViaExec($this->filename));
 }
Example #2
0
/**
 * Try to find a programm
 *
 * @param string $program
 * @return null|string
 */
function findBinaryPath($program)
{
    if (OC_Helper::is_function_enabled('exec')) {
        exec('command -v ' . escapeshellarg($program) . ' 2> /dev/null', $output, $returnCode);
        if ($returnCode === 0 && count($output) > 0) {
            return escapeshellcmd($output[0]);
        }
    }
    return null;
}
Example #3
0
 /**
  * @brief Tries to get the size of a file via an exec() call.
  *
  * @param string $filename Path to the file.
  *
  * @return null|int|float Number of bytes as number (float or int) or
  *                        null on failure.
  */
 public function getFileSizeViaExec($filename)
 {
     if (\OC_Helper::is_function_enabled('exec')) {
         $os = strtolower(php_uname('s'));
         $arg = escapeshellarg($filename);
         $result = null;
         if (strpos($os, 'linux') !== false) {
             $result = $this->exec("stat -c %s {$arg}");
         } else {
             if (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) {
                 $result = $this->exec("stat -f %z {$arg}");
             } else {
                 if (strpos($os, 'win') !== false) {
                     $result = $this->exec("for %F in ({$arg}) do @echo %~zF");
                     if (is_null($result)) {
                         // PowerShell
                         $result = $this->exec("(Get-Item {$arg}).length");
                     }
                 }
             }
         }
         return $result;
     }
     return null;
 }
 /**
  * Register the default providers (if enabled)
  */
 protected function registerCoreProviders()
 {
     if ($this->registeredCoreProviders) {
         return;
     }
     $this->registeredCoreProviders = true;
     $this->registerCoreProvider('OC\\Preview\\TXT', '/text\\/plain/');
     $this->registerCoreProvider('OC\\Preview\\MarkDown', '/text\\/(x-)?markdown/');
     $this->registerCoreProvider('OC\\Preview\\PNG', '/image\\/png/');
     $this->registerCoreProvider('OC\\Preview\\JPEG', '/image\\/jpeg/');
     $this->registerCoreProvider('OC\\Preview\\GIF', '/image\\/gif/');
     $this->registerCoreProvider('OC\\Preview\\BMP', '/image\\/bmp/');
     $this->registerCoreProvider('OC\\Preview\\XBitmap', '/image\\/x-xbitmap/');
     $this->registerCoreProvider('OC\\Preview\\MP3', '/audio\\/mpeg/');
     // SVG, Office and Bitmap require imagick
     if (extension_loaded('imagick')) {
         $checkImagick = new \Imagick();
         $imagickProviders = ['SVG' => ['mimetype' => '/image\\/svg\\+xml/', 'class' => '\\OC\\Preview\\SVG'], 'TIFF' => ['mimetype' => '/image\\/tiff/', 'class' => '\\OC\\Preview\\TIFF'], 'PDF' => ['mimetype' => '/application\\/pdf/', 'class' => '\\OC\\Preview\\PDF'], 'AI' => ['mimetype' => '/application\\/illustrator/', 'class' => '\\OC\\Preview\\Illustrator'], 'PSD' => ['mimetype' => '/application\\/x-photoshop/', 'class' => '\\OC\\Preview\\Photoshop'], 'EPS' => ['mimetype' => '/application\\/postscript/', 'class' => '\\OC\\Preview\\Postscript'], 'TTF' => ['mimetype' => '/application\\/(?:font-sfnt|x-font$)/', 'class' => '\\OC\\Preview\\Font']];
         foreach ($imagickProviders as $queryFormat => $provider) {
             $class = $provider['class'];
             if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
                 continue;
             }
             if (count($checkImagick->queryFormats($queryFormat)) === 1) {
                 $this->registerCoreProvider($class, $provider['mimetype']);
             }
         }
         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($this->config->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) {
                     $this->registerCoreProvider('\\OC\\Preview\\MSOfficeDoc', '/application\\/msword/');
                     $this->registerCoreProvider('\\OC\\Preview\\MSOffice2003', '/application\\/vnd.ms-.*/');
                     $this->registerCoreProvider('\\OC\\Preview\\MSOffice2007', '/application\\/vnd.openxmlformats-officedocument.*/');
                     $this->registerCoreProvider('\\OC\\Preview\\OpenDocument', '/application\\/vnd.oasis.opendocument.*/');
                     $this->registerCoreProvider('\\OC\\Preview\\StarOffice', '/application\\/vnd.sun.xml.*/');
                 }
             }
         }
     }
     // Video requires avconv or ffmpeg and is therefor
     // currently not supported on Windows.
     if (in_array('OC\\Preview\\Movie', $this->getEnabledDefaultProvider()) && !\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;
             $this->registerCoreProvider('\\OC\\Preview\\Movie', '/video\\/.*/');
         }
     }
 }
Example #5
0
 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', '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');
         }
     }
 }
Example #6
0
<?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 libreoffice') : '';
            $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';
            }
        }
    }
}
 private static function getFileSizeFromOS($fullPath)
 {
     $name = strtolower(php_uname('s'));
     // Windows OS: we use COM to access the filesystem
     if (strpos($name, 'win') !== false) {
         if (class_exists('COM')) {
             $fsobj = new \COM("Scripting.FileSystemObject");
             $f = $fsobj->GetFile($fullPath);
             return $f->Size;
         }
     } else {
         if (strpos($name, 'bsd') !== false) {
             if (\OC_Helper::is_function_enabled('exec')) {
                 return (double) exec('stat -f %z ' . escapeshellarg($fullPath));
             }
         } else {
             if (strpos($name, 'linux') !== false) {
                 if (\OC_Helper::is_function_enabled('exec')) {
                     return (double) exec('stat -c %s ' . escapeshellarg($fullPath));
                 }
             } else {
                 \OC_Log::write('core', 'Unable to determine file size of "' . $fullPath . '". Unknown OS: ' . $name, \OC_Log::ERROR);
             }
         }
     }
     return 0;
 }
Example #8
0
 * later.
 * See the COPYING-README file.
 */
namespace OC\Preview;

function findBinaryPath($program)
{
    exec('command -v ' . escapeshellarg($program) . ' 2> /dev/null', $output, $returnCode);
    if ($returnCode === 0 && count($output) > 0) {
        return escapeshellcmd($output[0]);
    }
    return null;
}
// movie preview is currently not supported on Windows
if (!\OC_Util::runningOnWindows()) {
    $isExecEnabled = \OC_Helper::is_function_enabled('exec');
    $ffmpegBinary = null;
    $avconvBinary = null;
    if ($isExecEnabled) {
        $avconvBinary = findBinaryPath('avconv');
        if (!$avconvBinary) {
            $ffmpegBinary = findBinaryPath('ffmpeg');
        }
    }
    if ($isExecEnabled && ($avconvBinary || $ffmpegBinary)) {
        class Movie extends Provider
        {
            public static $avconvBinary;
            public static $ffmpegBinary;
            public function getMimeType()
            {