DocumentController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 2
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\IConfig;
17
use \OCP\IL10N;
18
use \OCP\AppFramework\Http\JSONResponse;
19
use OCP\AppFramework\Http\TemplateResponse;
20
21
use \OCA\Documents\Db;
22
use \OCA\Documents\Helper;
23
use \OCA\Documents\Storage;
24
use \OCA\Documents\Download;
25
use \OCA\Documents\DownloadResponse;
26
use \OCA\Documents\File;
27
use OCA\Documents\Genesis;
28
use \OC\Files\View;
29
30
class DocumentController extends Controller{
31
	
32
	private $uid;
33
	private $l10n;
34
	private $settings;
35
	
36
	const ODT_TEMPLATE_PATH = '/assets/new.odt';
37
	
38 2
	public function __construct($appName, IRequest $request, IConfig $settings, IL10N $l10n, $uid){
39 2
		parent::__construct($appName, $request);
40 2
		$this->uid = $uid;
41 2
		$this->l10n = $l10n;
42 2
		$this->settings = $settings;
43 2
	}
44
	
45
	/**
46
	 * @NoAdminRequired
47
	 * @NoCSRFRequired
48
	 */
49
	public function index(){
50
		\OC::$server->getNavigationManager()->setActiveEntry( 'documents_index' );
51
		$maxUploadFilesize = \OCP\Util::maxUploadFilesize("/");
52
		return new TemplateResponse('documents', 'documents', [
53
			'enable_previews' => 		$this->settings->getSystemValue('enable_previews', true),
54
			'useUnstable' => 		$this->settings->getAppValue('documents', 'unstable', 'false'),
55
			'savePath' => 			$this->settings->getUserValue($this->uid, 'documents', 'save_path', '/'),
56
			'uploadMaxFilesize' =>		$maxUploadFilesize,
57
			'uploadMaxHumanFilesize' =>	\OCP\Util::humanFileSize($maxUploadFilesize),
58
			'allowShareWithLink' => 	$this->settings->getAppValue('core', 'shareapi_allow_links', 'yes'),
59
		]);
60
	}
61
	
62
	/**
63
	 * @NoAdminRequired
64
	 */
65 1
	public function create(){
66 1
		$view = new View('/' . $this->uid . '/files');
67 1
		$dir = $this->settings->getUserValue($this->uid, $this->appName, 'save_path', '/');
68 1
		if (!$view->is_dir($dir)){
69 1
			$dir = '/';
70 1
		}
71 1
		$path = Helper::getNewFileName($view, $dir . '/New Document.odt');
72
		
73 1
		$content = '';
74 1
		if (class_exists('\OC\Files\Type\TemplateManager')){
75 1
			$manager = \OC_Helper::getFileTemplateManager();
76 1
			$content = $manager->getTemplate(Storage::MIMETYPE_LIBREOFFICE_WORDPROCESSOR);
77 1
		}
78
		
79 1
		if (!$content){
80
			$content = file_get_contents(dirname(__DIR__) . self::ODT_TEMPLATE_PATH);
81
		}
82
		
83 1
		if ($content && $view->file_put_contents($path, $content)){
84 1
			$info = $view->getFileInfo($path);
85
			$response =  array(
86 1
				'status' => 'success',
87 1
				'fileid' => $info['fileid']
88 1
			);
89 1
		} else {
90
			$response =  array(
91
				'status' => 'error',
92
				'message' => (string) $this->l10n->t('Can\'t create document')
93
			);
94
		}
95 1
		return $response;
96
	}
97
98
	/**
99
	 * @NoAdminRequired
100
	 * @PublicPage
101
	 * Process partial/complete file download
102
	 */
103
	public function serve($esId){
104
		$session = new Db\Session();
105
		$session->load($esId);
106
		
107
		$filename = $session->getGenesisUrl() ? $session->getGenesisUrl() : '';
108
		return new DownloadResponse($this->request, $session->getOwner(), $filename);
109
	}
110
	
111
	/**
112
	 * @NoAdminRequired
113
	 */
114
	public function download($path){
115
		if (!$path){
116
			$response = new JSONResponse();
117
			$response->setStatus(Http::STATUS_BAD_REQUEST);
118
			return $response;
119
		}
120
		
121
		$fullPath = '/files' . $path;
122
		$fileInfo = \OC\Files\Filesystem::getFileInfo($path);
123
		if ($fileInfo){
124
			if($fileInfo->getMimeType() !== \OCA\Documents\Filter\Office::NATIVE_MIMETYPE){
125
				$file = new File($fileInfo->getId());
126
				$genesis = new Genesis($file);
127
				$fullPath = $genesis->getPath();
128
			}
129
		}
130
		return new DownloadResponse($this->request, $this->uid, $fullPath);
131
	}
132
	
133
134
	/**
135
	 * @NoAdminRequired
136
	 */
137 1
	public function rename($fileId){
138 1
		$name = $this->request->post['name'];
139
		$result = [
140 1
			'status' => 'error',
141 1
			'message' => (string) $this->l10n->t('You don\'t have permission to rename this document')
142 1
		];
143
144 1
		$view = \OC\Files\Filesystem::getView();
145
		try {
146 1
			$path = $view->getPath($fileId);
147
			if ($name && $view->is_file($path) && $view->isUpdatable($path)) {
148
				$newPath = dirname($path) . '/' . $name;
149
				if ($view->rename($path, $newPath)) {
150
					$result = ['status' => 'success'];
151
				}
152
			}
153 1
		} catch (\Exception $e){
154
			$result = [
155 1
				'status' => 'error',
156 1
				'message' => (string) $this->l10n->t('File does not exist')
157 1
			];
158
		}
159 1
		return $result;
160
	}
161
162
	/**
163
	 * @NoAdminRequired
164
	 * lists the documents the user has access to (including shared files, once the code in core has been fixed)
165
	 * also adds session and member info for these files
166
	 */
167
	public function listAll(){
168
		$found = Storage::getDocuments();
169
170
		$fileIds = array();
171
		$documents = array();
172
		foreach ($found as $key=>$document) {
173
			$fileData = $document->getData();
174
			$documents[$key]['fileid'] = $document->getId();
175
			$documents[$key]['path'] = $fileData->getPath();
176
			$documents[$key]['name'] = $document->getName();
177
			$documents[$key]['etag'] = $document->getEtag();
178
179
			$documents[$key]['icon'] = preg_replace('/\.png$/', '.svg', \OCP\Template::mimetype_icon($document['mimetype']));
180
			$documents[$key]['hasPreview'] = \OC::$server->getPreviewManager()->isMimeSupported($document['mimetype']);
181
			$fileIds[] = $document['fileid'];
182
		}
183
184
		usort($documents, function($a, $b){
185
			return @$b['mtime']-@$a['mtime'];
186
		});
187
		
188
		$session = new Db\Session();
189
		$sessions = $session->getCollectionBy('file_id', $fileIds);
190
191
		$members = array();
192
		$member = new Db\Member();
193
		foreach ($sessions as $session) {			
194
			$members[$session['es_id']] = $member->getActiveCollection($session['es_id']);
195
		}
196
197
		return array(
198
			'status' => 'success', 'documents' => $documents,'sessions' => $sessions,'members' => $members
199
		);
200
	}
201
	
202
}
203