SettingsController::setConverter()   C
last analyzed

Complexity

Conditions 8
Paths 28

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 42
cp 0
rs 5.5555
c 0
b 0
f 0
cc 8
eloc 30
nc 28
nop 2
crap 72
1
<?php
2
/**
3
 * ownCloud - Documents App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2014 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Documents\Controller;
13
14
use \OCP\AppFramework\Controller;
15
use \OCP\IRequest;
16
use \OCP\IL10N;
17
use \OCP\AppFramework\Http\JSONResponse;
18
use OCP\AppFramework\Http\TemplateResponse;
19
20
use OCA\Documents\AppConfig;
21
use OCA\Documents\Converter;
22
use OCA\Documents\Filter;
23
24
class SettingsController extends Controller{
25
	
26
	private $userId;
27
	private $l10n;
28
	private $appConfig;
29
	
30
	public function __construct($appName, IRequest $request, IL10N $l10n, AppConfig $appConfig, $userId){
31
		parent::__construct($appName, $request);
32
		$this->userId = $userId;
33
		$this->l10n = $l10n;
34
		$this->appConfig = $appConfig;
35
	}
36
	
37
	/**
38
	 * @NoAdminRequired
39
	 */
40
	public function getSupportedMimes(){
41
		return array(
42
			'status' => 'success',
43
			'mimes' => Filter::getAll()
44
		);
45
	}
46
	
47
	/**
48
	 * @NoAdminRequired
49
	 * @NoCSRFRequired
50
	 */
51
	public function personalIndex(){
52
		return new TemplateResponse(
53
			$this->appName, 
54
			'personal',
55
			[ 'save_path' => $this->appConfig->getUserValue($this->userId, 'save_path') ],
56
			'blank'
57
		);
58
	}
59
	
60
	/**
61
	 * @NoCSRFRequired
62
	 */
63
	public function settingsIndex(){
64
		return new TemplateResponse(
65
			$this->appName, 
66
			'settings',
67
			[ 'unstable' => $this->appConfig->getAppValue('unstable') ],
68
			'blank'
69
		);
70
	}
71
	
72
	/**
73
	 * @NoCSRFRequired
74
	 */
75
	public function adminIndex(){
76
		return new TemplateResponse(
77
			$this->appName, 
78
			'admin',
79
			[
80
				'converter' => $this->appConfig->getAppValue('converter'),
81
				'converter_url' => $this->appConfig->getAppValue('converter_url'),
82
			],
83
			'blank'
84
		);
85
	}
86
	
87
	/**
88
	 * @NoAdminRequired
89
	 */
90
	public function savePersonal($savePath){
91
		if (is_null($savePath)){
92
			$savePath = '/';
93
		}
94
		$status = true;
95
		if (\OC\Files\Filesystem::file_exists($savePath) === false ){
96
			$status = \OC\Files\Filesystem::mkdir($savePath);
97
		}
98
		
99
		if ($status){
100
			$this->appConfig->setUserValue($this->userId, 'save_path', $savePath);
101
			$response = array(
102
				'status' => 'success',
103
				'data' => array('message'=> $this->l10n->t('Directory saved successfully.'))
104
			);
105
		} else {
106
				$response = array(
107
					'status' => 'error',
108
					'data' => array(
109
							'message'=> $this->l10n->t('An error occurred while changing directory.')
110
					)
111
				);
112
		}
113
		return $response;
114
	}
115
	
116
	public function setUnstable($unstable){
117
		if (!is_null($unstable)){
118
			$this->appConfig->setAppValue('unstable', $unstable);
119
		}
120
		return array('status' => 'success');
121
	}
122
	
123
	public function setConverter($converter, $url){
124
		if (!is_null($converter)){
125
			$this->appConfig->setAppValue('converter', $converter);
126
		}
127
	
128
		if (!is_null($url)){
129
			$this->appConfig->setAppValue('converter_url', $url);
130
		}
131
		
132
		$response = array(
133
			'status' => 'success',
134
			'data' => array('message' => (string) $this->l10n->t('Saved'))
135
		);
136
		
137
		$currentConverter = $this->appConfig->getAppValue('converter');
138
		if ($currentConverter == 'external'){
139
			if (!Converter::checkConnection()){
140
				\OC::$server->getLogger()->warning(
141
					'Bad response from Format Filter Server', 
142
					['app' => $this->appName]
143
				);
144
					$response = array(
145
						'status' => 'error',
146
						'data'=>
147
						array('message' => (string) $this->l10n->t('Format filter server is down or misconfigured') )
148
					);
149
			}
150
		} elseif ($currentConverter === 'local') {
151
			try {
152
				if (!Converter::testConversion()){
153
					$response = array( 
154
						'status' => 'error',
155
						'data'=>
156
						array('message' => (string) $this->l10n->t('Conversion failed. Check log for details.') )
157
					);
158
				}
159
			} catch (\Exception $e){
160
				$response = array(
161
					'status' => 'error',
162
					'data'=> array('message' => $e->getMessage())
163
				);
164
			}
165
		}
166
		
167
		return $response;
168
	}
169
}
170