MS formats. First approach
parent
74b117bccb
commit
e4ce2258e1
@ -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']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue