MS formats. First approach

pull/1/head
Victor Dubiniuk 10 years ago
parent 74b117bccb
commit e4ce2258e1

@ -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");

@ -0,0 +1,42 @@
<?php
/**
* ownCloud - Documents App
*
* @author Victor Dubiniuk
* @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Documents;
class Config {
const APP_NAME = 'documents';
public static function getConverter(){
return self::getAppValue('converter', 'local');
}
public static function setConverter($value){
return self::setAppValue('converter', $value);
}
public static function getConverterUrl(){
return self::getAppValue('converter_url', 'http://localhost:16080');
}
public static function setConverterUrl($value){
return self::setAppValue('converter_url', $value);
}
protected static function getAppValue($key, $default){
return \OCP\Config::getAppValue(self::APP_NAME, $key, $default);
}
protected static function setAppValue($key, $value){
return \OCP\Config::setAppValue(self::APP_NAME, $key, $value);
}
}

@ -0,0 +1,94 @@
<?php
/**
* ownCloud - Documents App
*
* @author Victor Dubiniuk
* @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Documents;
class Converter {
public static function convert($input, $targetFilter, $targetExtension){
if (Config::getConverter() == 'local'){
$output = self::convertLocal($input, $targetFilter, $targetExtension);
} else {
$output = self::convertExternal($input, $targetExtension);
}
if (empty($output)){
\OCP\Util::writeLog('Documents', 'Empty conversion output', \OCP\Util::WARN);
throw new \RuntimeException('Empty conversion output');
}
return $output;
}
public static function checkConnection(){
$expected = file_get_contents(__DIR__ . '/response.odt');
$converted = self::convertExternal('');
return $converted === $expected;
}
/**
* convert via openOffice hosted on the same server
* @param string $input
* @param string $targetFilter
* @param string $targetExtension
* @return string
*/
protected static function convertLocal($input, $targetFilter, $targetExtension){
$infile = \OCP\Files::tmpFile();
$outdir = \OCP\Files::tmpFolder();
$cmd = Helper::findOpenOffice();
$params = ' --headless --convert-to ' . $targetFilter . ' --outdir '
. escapeshellarg($outdir)
. ' --writer '. escapeshellarg($infile)
;
file_put_contents($infile, $input);
shell_exec($cmd . $params);
$output = file_get_contents($outdir . '/' . basename($infile) . '.' . $targetExtension);
return $output;
}
/**
* convert via format-filter-server installed on the same host with openOffice
* @param string $input
* @return string
*/
protected static function convertExternal($input, $targetExtension){
$options = array(
CURLOPT_RETURNTRANSFER => 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;
}
}

@ -0,0 +1,85 @@
<?php
/**
* ownCloud - Documents App
*
* @author Victor Dubiniuk
* @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Documents;
class Filter_Office {
const NATIVE_MIMETYPE = 'application/vnd.oasis.opendocument.text';
private static $readSpec;
private static $writeSpec;
/* sample mimespec
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'
)
);
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']
)
);
}
}

@ -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;
}
}

Loading…
Cancel
Save