Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PublicFileHandlingController.php « Controller « lib - github.com/nextcloud/files_texteditor.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eca32b7a6bd16aeb5ad3cbb6ee9e05ccfc8cc45d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
 * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OCA\FilesTextEditor\Controller;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;

class PublicFileHandlingController extends Controller{

	/** @var IL10N */
	private $l;

	/** @var ShareManager */
	private $shareManager;

	/** @var ISession */
	private $session;

	/**
	 *
	 * @param string $AppName
	 * @param IRequest $request
	 * @param IL10N $l10n
	 * @param ShareManager $shareManager
	 * @param ISession $session
	 */
	public function __construct($AppName,
								IRequest $request,
								IL10N $l10n,
								ShareManager $shareManager,
								ISession $session) {
		parent::__construct($AppName, $request);
		$this->l = $l10n;
		$this->shareManager = $shareManager;
		$this->session = $session;
	}

	/**
	 * load text file
	 *
	 * @NoAdminRequired
	 * @PublicPage
	 * @NoCSRFRequired
	 *
	 * @param string $token
	 * @return DataResponse
	 */
	public function load($token) {
		try {
			$share = $this->shareManager->getShareByToken($token);
		} catch (ShareNotFound $e) {
			return new DataResponse(['message' => $this->l->t('Share not found'), Http::STATUS_NOT_FOUND]);
		}

		if ($share->getPassword() !== null &&
			(!$this->session->exists('public_link_authenticated')
			|| $this->session->get('public_link_authenticated') !== (string)$share->getId())) {
			return new DataResponse(['message' => $this->l->t('You are not authorized to open this share'), Http::STATUS_BAD_REQUEST]);
		}

		try {
			$node = $share->getNode();
		} catch (NotFoundException $e) {
			return new DataResponse(['message' => $this->l->t('Share not found'), Http::STATUS_NOT_FOUND]);
		}

		if ($node instanceof Folder) {
			return new DataResponse(['message' => $this->l->t('You can not open a folder')], Http::STATUS_BAD_REQUEST);
		}

		$range = $this->request->getHeader('Range');
		if ($range !== '') {
			$matches = [];
			if (preg_match('/bytes=0-(\d+)$/', $range, $matches) === 0) {
				return new DataResponse(['message' => $this->l->t('Invalid range request')], Http::STATUS_REQUEST_RANGE_NOT_SATISFIABLE);
			}
			$range = (int)$matches[1];
		} else {
			$range = -1;
		}

		// default of 4MB
		$maxSize = 4194304;
		if ($node->getSize() > $maxSize) {
			return new DataResponse(['message' => $this->l->t('This file is too big to be opened. Please download the file instead.')], Http::STATUS_BAD_REQUEST);
		}

		$fileContents = $node->getContent();
		if ($fileContents !== false) {
			$encoding = mb_detect_encoding($fileContents . 'a', 'UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true);
			if ($encoding === '') {
				// set default encoding if it couldn't be detected
				$encoding = 'ISO-8859-15';
			}
			$fileContents = iconv($encoding, 'UTF-8', $fileContents);

			if ($range !== -1) {
				$fileContents = mb_substr($fileContents, 0, $range);
			}

			return new Http\DataDisplayResponse(
				$fileContents,
				Http::STATUS_OK,
				['Content-Type' => 'text/plain; charset="utf-8"']
			);
		}

		return new DataResponse(['message' => $this->l->t('Cannot read the file.')], Http::STATUS_BAD_REQUEST);
	}
}