diff options
author | Victor Dubiniuk <victor.dubiniuk@gmail.com> | 2015-09-18 22:10:27 +0300 |
---|---|---|
committer | Victor Dubiniuk <victor.dubiniuk@gmail.com> | 2015-09-19 00:20:28 +0300 |
commit | 76ef4e48aa2ad9218e474bcd41a9df7a89118c92 (patch) | |
tree | 7cb38df75480fb74ab23471b82952b821e826bda /lib | |
parent | 5c8a7923cbff95c023b3731f8236f2f5b69bd7c5 (diff) |
ByeBye static Config class
Diffstat (limited to 'lib')
-rw-r--r-- | lib/appconfig.php | 86 | ||||
-rw-r--r-- | lib/config.php | 76 | ||||
-rw-r--r-- | lib/converter.php | 47 |
3 files changed, 131 insertions, 78 deletions
diff --git a/lib/appconfig.php b/lib/appconfig.php new file mode 100644 index 00000000..1f4463d0 --- /dev/null +++ b/lib/appconfig.php @@ -0,0 +1,86 @@ +<?php +/** + * ownCloud - Documents App + * + * @author Victor Dubiniuk + * @copyright 2015 Victor Dubiniuk victor.dubiniuk@gmail.com + * + * This file is licensed under the Affero General Public License version 3 or + * later. + */ + +namespace OCA\Documents; + +use \OCP\IConfig; + + class AppConfig{ + private $appName = 'documents'; + private $defaults = [ + 'converter' => 'off', + 'converter_url' => 'http://localhost:16080', + 'unstable' => 'false' + ]; + + private $config; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + /** + * Can we convert anything to odt? + * @return bool + */ + public function isConverterEnabled(){ + return $this->getAppValue('converter') !== 'off'; + } + + /** + * Get a value by key + * @param string $key + * @return string + */ + public function getAppValue($key) { + $defaultValue = null; + if (array_key_exists($key, $this->defaults)){ + $defaultValue = $this->defaults[$key]; + } + return $this->config->getAppValue($this->appName, $key, $defaultValue); + } + + /** + * Set a value by key + * @param string $key + * @param string $value + * @return string + */ + public function setAppValue($key, $value) { + return $this->config->setAppValue($this->appName, $key, $value); + } + + /** + * Get a value by key for a user + * @param string $userId + * @param string $key + * @return string + */ + public function getUserValue($userId, $key) { + $defaultValue = null; + if (array_key_exists($key, $this->defaults)){ + $defaultValue = $this->defaults[$key]; + } + return $this->config->getUserValue($userId, $this->appName, $key, $defaultValue); + } + + /** + * Set a value by key for a user + * @param string $userId + * @param string $key + * @param string $value + * @return string + */ + public function setUserValue($userId, $key, $value) { + return $this->config->setAppValue($userId, $this->appName, $key, $value); + } + } +
\ No newline at end of file diff --git a/lib/config.php b/lib/config.php deleted file mode 100644 index 6065de87..00000000 --- a/lib/config.php +++ /dev/null @@ -1,76 +0,0 @@ -<?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'; - const TEST_DOC_PATH = '/assets/test.doc'; - - public static function testConversion(){ - $targetFilter = 'odt:writer8'; - $targetExtension = 'odt'; - $input = file_get_contents(dirname(__DIR__) . self::TEST_DOC_PATH); - $infile = \OC::$server->getTempManager()->getTemporaryFile(); - $outdir = \OC::$server->getTempManager()->getTemporaryFolder(); - $outfile = $outdir . '/' . basename($infile) . '.' . $targetExtension; - $cmd = Helper::findOpenOffice(); - - $params = ' --headless --convert-to ' . escapeshellarg($targetFilter) . ' --outdir ' - . escapeshellarg($outdir) - . ' --writer '. escapeshellarg($infile) - . ' -env:UserInstallation=file://' - . escapeshellarg(get_temp_dir() . '/owncloud-' . \OC_Util::getInstanceId().'/') . ' 2>&1' - ; - file_put_contents($infile, $input); - - $result = shell_exec($cmd . $params); - $exists = file_exists($outfile); - - if (!$exists){ - \OC::$server->getLogger()->warn( - 'Conversion test failed. Raw output:' . $result, - ['app' => 'documents'] - - ); - return false; - } else { - unlink($outfile); - } - return true; - } - - public static function getConverter(){ - return self::getAppValue('converter', 'off'); - } - - 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 \OC::$server->getConfig()->getAppValue(self::APP_NAME, $key, $default); - } - - protected static function setAppValue($key, $value){ - return \OC::$server->getConfig()->setAppValue(self::APP_NAME, $key, $value); - } - -} diff --git a/lib/converter.php b/lib/converter.php index ccd3d6b3..0ba1a4b1 100644 --- a/lib/converter.php +++ b/lib/converter.php @@ -12,10 +12,47 @@ namespace OCA\Documents; +use OCA\Documents\AppInfo\Application; + class Converter { + const TEST_DOC_PATH = '/assets/test.doc'; + + public static function testConversion(){ + $targetFilter = 'odt:writer8'; + $targetExtension = 'odt'; + $input = file_get_contents(dirname(__DIR__) . self::TEST_DOC_PATH); + $infile = \OC::$server->getTempManager()->getTemporaryFile(); + $outdir = \OC::$server->getTempManager()->getTemporaryFolder(); + $outfile = $outdir . '/' . basename($infile) . '.' . $targetExtension; + $cmd = Helper::findOpenOffice(); + + $params = ' --headless --convert-to ' . escapeshellarg($targetFilter) . ' --outdir ' + . escapeshellarg($outdir) + . ' --writer '. escapeshellarg($infile) + . ' -env:UserInstallation=file://' + . escapeshellarg(\OC::$server->getTempManager()->getTempBaseDir() . '/owncloud-' . \OC_Util::getInstanceId().'/') . ' 2>&1' + ; + file_put_contents($infile, $input); + + $result = shell_exec($cmd . $params); + $exists = file_exists($outfile); + + if (!$exists){ + \OC::$server->getLogger()->warn( + 'Conversion test failed. Raw output:' . $result, + ['app' => 'documents'] + + ); + return false; + } else { + unlink($outfile); + } + return true; + } + public static function convert($input, $targetFilter, $targetExtension){ - if (Config::getConverter() == 'local'){ + if (self::getAppConfig()->getAppValue('converter') === 'local'){ $output = self::convertLocal($input, $targetFilter, $targetExtension); } else { $output = self::convertExternal($input, $targetExtension); @@ -86,7 +123,7 @@ class Converter { CURLOPT_VERBOSE => 1 ); - $ch = curl_init(Config::getConverterUrl() . '?target_format=' . $targetExtension); + $ch = curl_init(self::getAppConfig()->getAppValue('converter_url') . '?target_format=' . $targetExtension); curl_setopt_array($ch, $options); $content = curl_exec($ch); if (curl_errno($ch)){ @@ -100,5 +137,11 @@ class Converter { return $content; } + + protected static function getAppConfig(){ + $app = new Application(); + $c = $app->getContainer(); + return $c->query('AppConfig'); + } } |