ByeBye static Config class
This commit is contained in:
parent
5c8a7923cb
commit
76ef4e48aa
@ -53,7 +53,7 @@ if (isset($request->server['REQUEST_URI'])) {
|
||||
}
|
||||
}
|
||||
|
||||
if (Config::getConverter() !== 'off'){
|
||||
if ($c->query('AppConfig')->isConverterEnabled()){
|
||||
$docFilter = new Office(
|
||||
[
|
||||
'read' =>
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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'=>
|
||||
|
86
lib/appconfig.php
Normal file
86
lib/appconfig.php
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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)){
|
||||
@ -101,4 +138,10 @@ class Converter {
|
||||
return $content;
|
||||
}
|
||||
|
||||
protected static function getAppConfig(){
|
||||
$app = new Application();
|
||||
$c = $app->getContainer();
|
||||
return $c->query('AppConfig');
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user