From e4ce2258e120e79e1cdb6044ee28bc73f8dc6f7d Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 28 Mar 2014 22:14:51 +0300 Subject: [PATCH] MS formats. First approach --- appinfo/app.php | 35 ++++++++++++++++ lib/config.php | 42 +++++++++++++++++++ lib/converter.php | 94 +++++++++++++++++++++++++++++++++++++++++++ lib/filter/office.php | 85 ++++++++++++++++++++++++++++++++++++++ lib/helper.php | 25 ++++++++++++ 5 files changed, 281 insertions(+) create mode 100644 lib/config.php create mode 100644 lib/converter.php create mode 100644 lib/filter/office.php diff --git a/appinfo/app.php b/appinfo/app.php index d2b5136c..fa8b41db 100755 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -44,9 +44,44 @@ OC::$CLASSPATH['OCA\Documents\Download_Range'] = 'documents/lib/download/range.p OC::$CLASSPATH['OCA\Documents\Db_Session'] = 'documents/lib/db/session.php'; OC::$CLASSPATH['OCA\Documents\Db_Member'] = 'documents/lib/db/member.php'; OC::$CLASSPATH['OCA\Documents\Db_Op'] = 'documents/lib/db/op.php'; +OC::$CLASSPATH['OCA\Documents\Filter_Office'] = 'documents/lib/filter/office.php'; //Script for registering file actions OCP\Util::addScript('documents', 'viewer/viewer'); +$docFilter = new OCA\Documents\Filter_Office( + array( + 'read' => + array ( + 'target' => 'application/vnd.oasis.opendocument.text', + 'format' => 'odt:writer8', + 'extension' => 'odt' + ), + 'write' => + array ( + 'target' => 'application/msword', + 'format' => 'doc', + 'extension' => 'doc' + ) + ) +); + +$docxFilter = new OCA\Documents\Filter_Office( + array ( + 'read' => + array ( + 'target' => 'application/vnd.oasis.opendocument.text', + 'format' => 'odt:writer8', + 'extension' => 'odt' + ), + 'write' => + array ( + 'target' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'format' => 'docx', + 'extension' => 'docx' + ) + ) +); + //Listen to delete file signal OCP\Util::connectHook('OC_Filesystem', 'delete', "OCA\Documents\Storage", "onDelete"); diff --git a/lib/config.php b/lib/config.php new file mode 100644 index 00000000..d8b7be4b --- /dev/null +++ b/lib/config.php @@ -0,0 +1,42 @@ + true, + CURLOPT_HEADER => false, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_ENCODING => "", + CURLOPT_AUTOREFERER => true, + CURLOPT_CONNECTTIMEOUT => 120, + CURLOPT_TIMEOUT => 120, + CURLOPT_MAXREDIRS => 2, + CURLOPT_POST => 1, + CURLOPT_POSTFIELDS => $input, + CURLOPT_SSL_VERIFYHOST => 0, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_VERBOSE => 1 + ); + + $ch = curl_init(Config::getConverterUrl() . '?target_format=' . $targetExtension); + curl_setopt_array($ch, $options); + $content = curl_exec($ch); + if (curl_errno($ch)){ + \OCP\Util::writeLog('Documents', 'cURL error' . curl_errno($ch) . ':' . curl_error($ch), \OCP\Util::DEBUG); + } + curl_close($ch); + + return $content; + } + +} diff --git a/lib/filter/office.php b/lib/filter/office.php new file mode 100644 index 00000000..f04456af --- /dev/null +++ b/lib/filter/office.php @@ -0,0 +1,85 @@ + + array ( + 'target' => 'application/vnd.oasis.opendocument.text', + 'format' => 'odt:writer8', + 'extension' => 'odt' + ), + 'write' => + array ( + 'target' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'format' => 'docx', + 'extension' => 'docx' + ) + ); + + array( + 'read' => + array ( + 'target' => 'application/vnd.oasis.opendocument.text', + 'format' => 'odt:writer8', + 'extension' => 'odt' + ), + 'write' => + array ( + 'target' => 'application/msword', + 'format' => 'doc', + 'extension' => 'doc' + ) + ); + + */ + + public function __construct($mimeSpec){ + $this->readSpec = $mimeSpec['read']; + $this->writeSpec = $mimeSpec['write']; + + Filter::add($mimeSpec['write']['target'], $this); + } + + public function read($data){ + return array( + 'mimetype' => $this->readSpec['target'], + 'content' => + Converter::convert( + $data['content'], + $this->readSpec['format'], + $this->readSpec['extension'] + ) + ); + } + + public function write($data){ + return array( + 'mimetype' => $this->writeSpec['target'], + 'content' => + Converter::convert( + $data['content'], + $this->writeSpec['format'], + $this->writeSpec['extension'] + ) + ); + } + +} diff --git a/lib/helper.php b/lib/helper.php index 2bd9cd6e..460a7f0a 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -140,5 +140,30 @@ class Helper { $dB = str_pad(dechex(round($dB)), 2, "0", STR_PAD_LEFT); return $dR.$dG.$dB; } + + public static function findOpenOffice(){ + $cmd = ''; + if (is_string(\OC_Config::getValue('preview_libreoffice_path', null))){ + $cmd = \OC_Config::getValue('preview_libreoffice_path', null); + } + + $whichLibreOffice = shell_exec('which libreoffice'); + if ($cmd === '' && !empty($whichLibreOffice)){ + $cmd = 'libreoffice'; + } + + $whichOpenOffice = shell_exec('which openoffice'); + if ($cmd === '' && !empty($whichOpenOffice)){ + $cmd = 'openoffice'; + } + + if (empty($cmd)){ + \OCP\Util::writeLog('Documents', 'Pure configuration issue. Missing open office binary that is mandatory for conversion.', \OCP\Util::WARN); + \OCP\Util::writeLog('Documents', 'If openoffice or libreoffice is already installed please specify the path to it using preview_libreoffice_path config. Refer to admin manual for details.', \OCP\Util::WARN); + throw new \RuntimeException('Missing open office binary that is mandatory for conversion.'); + } + + return $cmd; + } }