Completed
Push — master ( 446472...7ef246 )
by Lukas
02:50
created

WopiController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 159
ccs 0
cts 89
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
C checkFileInfo() 0 42 8
B getFile() 0 25 3
B putFile() 0 26 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Richdocuments\Controller;
23
24
use OCA\Richdocuments\Db\Wopi;
25
use OCA\Richdocuments\WOPI\Parser;
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http;
28
use OCP\AppFramework\Http\JSONResponse;
29
use OCP\Files\File;
30
use OCP\Files\IRootFolder;
31
use OCP\IRequest;
32
use OCP\IUserManager;
33
use OCP\AppFramework\Http\StreamResponse;
34
35
class WopiController extends Controller {
36
	/** @var IRootFolder */
37
	private $rootFolder;
38
	/** @var string */
39
	private $userId;
40
	/** @var IUserManager */
41
	private $userManager;
42
	/** @var Parser */
43
	private $wopiParser;
44
45
	/**
46
	 * @param string $appName
47
	 * @param IRequest $request
48
	 * @param IRootFolder $rootFolder
49
	 * @param string $UserId
50
	 * @param IUserManager $userManager
51
	 * @param Parser $wopiParser
52
	 */
53
	public function __construct($appName,
54
								$UserId,
55
								IRequest $request,
56
								IRootFolder $rootFolder,
57
								IUserManager $userManager,
58
								Parser $wopiParser) {
59
		parent::__construct($appName, $request);
60
		$this->rootFolder = $rootFolder;
61
		$this->userId = $UserId;
62
		$this->userManager = $userManager;
63
		$this->wopiParser = $wopiParser;
64
	}
65
66
	/**
67
	 * Returns general info about a file.
68
	 *
69
	 * @NoAdminRequired
70
	 * @NoCSRFRequired
71
	 * @PublicPage
72
	 *
73
	 * @param string $fileId
74
	 * @return JSONResponse
75
	 */
76
	public function checkFileInfo($fileId) {
77
		$token = $this->request->getParam('access_token');
78
79
		$arr = explode('_', $fileId, 2);
80
		$version = '0';
81
		if (count($arr) === 2) {
82
			list($fileId, $version) = $arr;
83
		}
84
85
		$row = new Wopi();
86
		$row->loadBy('token', $token);
87
88
		$res = $row->getPathForToken($fileId, $version, $token);
89
		if ($res === false) {
90
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
91
		}
92
93
		// Login the user to see his mount locations
94
		try {
95
			/** @var File $file */
96
			$userFolder = $this->rootFolder->getUserFolder($res['owner']);
97
			$file = $userFolder->getById($fileId)[0];
98
		} catch (\Exception $e) {
99
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
100
		}
101
102
		if(!($file instanceof File)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
103
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
104
		}
105
106
		return new JSONResponse(
107
			[
108
				'BaseFileName' => $file->getName(),
109
				'Size' => $file->getSize(),
110
				'Version' => $version,
111
				'UserId' => $res['editor'] !== '' ? $res['editor'] : 'Guest user',
112
				'UserFriendlyName' => $res['editor'] !== '' ? $res['editor'] : 'Guest user',
113
				'UserCanWrite' => $res['canwrite'] ? true : false,
114
				'PostMessageOrigin' => $res['server_host'],
115
			]
116
		);
117
	}
118
119
	/**
120
	 * Given an access token and a fileId, returns the contents of the file.
121
	 * Expects a valid token in access_token parameter.
122
	 *
123
	 * @PublicPage
124
	 * @NoCSRFRequired
125
	 *
126
	 * @param string $fileId
127
	 * @param string $access_token
128
	 * @return Http\Response
129
	 */
130
	public function getFile($fileId,
131
							$access_token) {
132
		$arr = explode('_', $fileId, 2);
133
		$version = '0';
134
		if (count($arr) === 2) {
135
			list($fileId, $version) = $arr;
136
		}
137
138
		$row = new Wopi();
139
		$row->loadBy('token', $access_token);
140
141
		$res = $row->getPathForToken($fileId, $version, $access_token);
142
143
		try {
144
			/** @var File $file */
145
			$userFolder = $this->rootFolder->getUserFolder($res['owner']);
146
			$file = $userFolder->getById($fileId)[0];
147
			$response = new StreamResponse($file->fopen('rb'));
148
			$response->addHeader('Content-Disposition', 'attachment');
149
			$response->addHeader('Content-Type', 'application/octet-stream');
150
			return $response;
151
		} catch (\Exception $e) {
152
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
153
		}
154
	}
155
156
	/**
157
	 * Given an access token and a fileId, replaces the files with the request body.
158
	 * Expects a valid token in access_token parameter.
159
	 *
160
	 * @PublicPage
161
	 * @NoCSRFRequired
162
	 *
163
	 * @param string $fileId
164
	 * @param string $access_token
165
	 * @return JSONResponse
166
	 */
167
	public function putFile($fileId, $access_token) {
168
		$arr = explode('_', $fileId, 2);
169
		$version = '0';
170
		if (count($arr) === 2) {
171
			list($fileId, $version) = $arr;
172
		}
173
174
		$row = new Wopi();
175
		$row->loadBy('token', $access_token);
176
177
		$res = $row->getPathForToken($fileId, $version, $access_token);
178
		if (!$res['canwrite']) {
179
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
180
		}
181
182
		try {
183
			/** @var File $file */
184
			$userFolder = $this->rootFolder->getUserFolder($res['owner']);
185
			$file = $userFolder->getById($fileId)[0];
186
			$content = fopen('php://input', 'rb');
187
			$file->putContent($content);
188
			return new JSONResponse();
189
		} catch (\Exception $e) {
190
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
191
		}
192
	}
193
}
194