From 76ef4e48aa2ad9218e474bcd41a9df7a89118c92 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 18 Sep 2015 22:10:27 +0300 Subject: [PATCH] ByeBye static Config class --- appinfo/app.php | 2 +- appinfo/application.php | 9 +++- controller/settingscontroller.php | 35 ++++++------- lib/appconfig.php | 86 +++++++++++++++++++++++++++++++ lib/config.php | 76 --------------------------- lib/converter.php | 47 ++++++++++++++++- 6 files changed, 157 insertions(+), 98 deletions(-) create mode 100644 lib/appconfig.php delete mode 100644 lib/config.php diff --git a/appinfo/app.php b/appinfo/app.php index 4155214e..66c3b464 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -53,7 +53,7 @@ if (isset($request->server['REQUEST_URI'])) { } } -if (Config::getConverter() !== 'off'){ +if ($c->query('AppConfig')->isConverterEnabled()){ $docFilter = new Office( [ 'read' => diff --git a/appinfo/application.php b/appinfo/application.php index a36f7855..1dda6655 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -17,6 +17,7 @@ use \OCA\Documents\Controller\UserController; use \OCA\Documents\Controller\SessionController; use \OCA\Documents\Controller\DocumentController; use \OCA\Documents\Controller\SettingsController; +use \OCA\Documents\AppConfig; class Application extends App { public function __construct (array $urlParams = array()) { @@ -54,12 +55,18 @@ class Application extends App { return new SettingsController( $c->query('AppName'), $c->query('Request'), - $c->query('CoreConfig'), $c->query('L10N'), + $c->query('AppConfig'), $c->query('UserId') ); }); + $container->registerService('AppConfig', function($c) { + return new AppConfig( + $c->query('CoreConfig') + ); + }); + /** * Core */ diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index fdd3e097..c9d90a54 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -13,26 +13,25 @@ namespace OCA\Documents\Controller; use \OCP\AppFramework\Controller; use \OCP\IRequest; -use \OCP\IConfig; use \OCP\IL10N; use \OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCA\Documents\AppConfig; use OCA\Documents\Converter; -use OCA\Documents\Config; use OCA\Documents\Filter; class SettingsController extends Controller{ private $userId; - private $settings; private $l10n; + private $appConfig; - public function __construct($appName, IRequest $request, IConfig $settings, IL10N $l10n, $userId){ + public function __construct($appName, IRequest $request, IL10N $l10n, AppConfig $appConfig, $userId){ parent::__construct($appName, $request); $this->userId = $userId; - $this->settings = $settings; $this->l10n = $l10n; + $this->appConfig = $appConfig; } /** @@ -51,9 +50,9 @@ class SettingsController extends Controller{ */ public function personalIndex(){ return new TemplateResponse( - 'documents', + $this->appName, 'personal', - [ 'save_path' => $this->settings->getUserValue($this->userId, 'documents', 'save_path', '/') ], + [ 'save_path' => $this->appConfig->getUserValue($this->userId, 'save_path') ], 'blank' ); } @@ -63,9 +62,9 @@ class SettingsController extends Controller{ */ public function settingsIndex(){ return new TemplateResponse( - 'documents', + $this->appName, 'settings', - [ 'unstable' => $this->settings->getAppValue('documents', 'unstable', 'false') ], + [ 'unstable' => $this->appConfig->getAppValue('unstable') ], 'blank' ); } @@ -75,11 +74,11 @@ class SettingsController extends Controller{ */ public function adminIndex(){ return new TemplateResponse( - 'documents', + $this->appName, 'admin', [ - 'converter' => Config::getConverter(), - 'converter_url' => Config::getConverterUrl(), + 'converter' => $this->appConfig->getAppValue('converter'), + 'converter_url' => $this->appConfig->getAppValue('converter_url'), ], 'blank' ); @@ -98,7 +97,7 @@ class SettingsController extends Controller{ } if ($status){ - $this->settings->setUserValue($this->userId, $this->appName, 'save_path', $savePath); + $this->appConfig->setUserValue($this->userId, 'save_path', $savePath); $response = array( 'status' => 'success', 'data' => array('message'=> $this->l10n->t('Directory saved successfully.')) @@ -116,18 +115,18 @@ class SettingsController extends Controller{ public function setUnstable($unstable){ if (!is_null($unstable)){ - $this->settings->setAppValue($this->appName, 'unstable', $unstable); + $this->appConfig->setAppValue('unstable', $unstable); } return array('status' => 'success'); } public function setConverter($converter, $url){ if (!is_null($converter)){ - $this->settings->setAppValue($this->appName, 'converter', $converter); + $this->appConfig->setAppValue('converter', $converter); } if (!is_null($url)){ - $this->settings->setAppValue($this->appName, 'converter_url', $url); + $this->appConfig->setAppValue('converter_url', $url); } $response = array( @@ -135,7 +134,7 @@ class SettingsController extends Controller{ 'data' => array('message' => (string) $this->l10n->t('Saved')) ); - $currentConverter = $this->settings->getAppValue($this->appName, 'converter', 'off'); + $currentConverter = $this->appConfig->getAppValue('converter'); if ($currentConverter == 'external'){ if (!Converter::checkConnection()){ \OC::$server->getLogger()->warning( @@ -150,7 +149,7 @@ class SettingsController extends Controller{ } } elseif ($currentConverter === 'local') { try { - if (!Config::testConversion()){ + if (!Converter::testConversion()){ $response = array( 'status' => 'error', 'data'=> 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 @@ + '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 @@ -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'); + } }