Completed
Pull Request — master (#71)
by Roeland
02:47
created

PublicFileHandlingController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 91
ccs 0
cts 52
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
C load() 0 50 10
1
<?php
2
/**
3
 * @copyright 2017, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\FilesTextEditor\Controller;
24
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\Files\Folder;
29
use OCP\Files\NotFoundException;
30
use OCP\IL10N;
31
use OCP\IRequest;
32
use OCP\ISession;
33
use OCP\Share\Exceptions\ShareNotFound;
34
use OCP\Share\IManager as ShareManager;
35
36
class PublicFileHandlingController extends Controller{
37
38
	/** @var IL10N */
39
	private $l;
40
41
	/** @var ShareManager */
42
	private $shareManager;
43
44
	/** @var ISession */
45
	private $session;
46
47
	/**
48
	 *
49
	 * @param string $AppName
50
	 * @param IRequest $request
51
	 * @param IL10N $l10n
52
	 * @param ShareManager $shareManager
53
	 * @param ISession $session
54
	 */
55
	public function __construct($AppName,
56
								IRequest $request,
57
								IL10N $l10n,
58
								ShareManager $shareManager,
59
								ISession $session) {
60
		parent::__construct($AppName, $request);
61
		$this->l = $l10n;
62
		$this->shareManager = $shareManager;
63
		$this->session = $session;
64
	}
65
66
	/**
67
	 * load text file
68
	 *
69
	 * @NoAdminRequired
70
	 * @PublicPage
71
	 * @NoCSRFRequired
72
	 *
73
	 * @param string $token
74
	 * @return DataResponse
75
	 */
76
	public function load($token) {
77
		try {
78
			$share = $this->shareManager->getShareByToken($token);
79
		} catch (ShareNotFound $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Share\Exceptions\ShareNotFound does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
			return new DataResponse(['message' => $this->l->t('Share not found'), Http::STATUS_NOT_FOUND]);
81
		}
82
83
		if ($share->getPassword() !== null &&
84
			(!$this->session->exists('public_link_authenticated')
85
			|| $this->session->get('public_link_authenticated') !== (string)$share->getId())) {
86
			return new DataResponse(['message' => $this->l->t('You are not authorized to open this share'), Http::STATUS_BAD_REQUEST]);
87
		}
88
89
		try {
90
			$node = $share->getNode();
91
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
92
			return new DataResponse(['message' => $this->l->t('Share not found'), Http::STATUS_NOT_FOUND]);
93
		}
94
95
		if ($node instanceof Folder) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Folder 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...
96
			return new DataResponse(['message' => $this->l->t('You can not open a folder')], Http::STATUS_BAD_REQUEST);
97
		}
98
99
		// default of 4MB
100
		$maxSize = 4194304;
101
		if ($node->getSize() > $maxSize) {
102
			return new DataResponse(['message' => $this->l->t('This file is too big to be opened. Please download the file instead.')], Http::STATUS_BAD_REQUEST);
103
		}
104
105
		$fileContents = $node->getContent();
106
		if ($fileContents !== false) {
107
			$encoding = mb_detect_encoding($fileContents . 'a', 'UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true);
108
			if ($encoding === '') {
109
				// set default encoding if it couldn't be detected
110
				$encoding = 'ISO-8859-15';
111
			}
112
			$fileContents = iconv($encoding, 'UTF-8', $fileContents);
113
			return new DataResponse(
114
				[
115
					'filecontents' => $fileContents,
116
					'writeable' => false,
117
					'mime' => $node->getMimeType(),
118
					'mtime' => $node->getMTime(),
119
				],
120
				Http::STATUS_OK
121
			);
122
		}
123
124
		return new DataResponse(['message' => $this->l->t('Cannot read the file.')], Http::STATUS_BAD_REQUEST);
125
	}
126
}
127