DirectFile   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A put() 0 2 1
A __construct() 0 4 1
A get() 0 6 1
A getContentType() 0 4 1
A getETag() 0 4 1
A delete() 0 2 1
A setName() 0 2 1
A getFile() 0 13 3
A getSize() 0 4 1
A getName() 0 2 1
A getLastModified() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2018, Roeland Jago Douma <[email protected]>
7
 *
8
 * @author Robin Appelman <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
namespace OCA\DAV\Direct;
28
29
use OCA\DAV\Db\Direct;
30
use OCA\DAV\Events\BeforeFileDirectDownloadedEvent;
31
use OCP\EventDispatcher\IEventDispatcher;
32
use OCP\Files\File;
33
use OCP\Files\IRootFolder;
34
use Sabre\DAV\Exception\Forbidden;
35
use Sabre\DAV\Exception\NotFound;
36
use Sabre\DAV\IFile;
37
38
class DirectFile implements IFile {
39
	/** @var Direct */
40
	private $direct;
41
42
	/** @var IRootFolder */
43
	private $rootFolder;
44
45
	/** @var File */
46
	private $file;
47
48
	private $eventDispatcher;
49
50
	public function __construct(Direct $direct, IRootFolder $rootFolder, IEventDispatcher $eventDispatcher) {
51
		$this->direct = $direct;
52
		$this->rootFolder = $rootFolder;
53
		$this->eventDispatcher = $eventDispatcher;
54
	}
55
56
	public function put($data) {
57
		throw new Forbidden();
58
	}
59
60
	public function get() {
61
		$this->getFile();
62
63
		$this->eventDispatcher->dispatchTyped(new BeforeFileDirectDownloadedEvent($this->file));
64
65
		return $this->file->fopen('rb');
66
	}
67
68
	public function getContentType() {
69
		$this->getFile();
70
71
		return $this->file->getMimeType();
72
	}
73
74
	public function getETag() {
75
		$this->getFile();
76
77
		return $this->file->getEtag();
78
	}
79
80
	/**
81
	 * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
82
	 * @return int|float
83
	 */
84
	public function getSize() {
85
		$this->getFile();
86
87
		return $this->file->getSize();
88
	}
89
90
	public function delete() {
91
		throw new Forbidden();
92
	}
93
94
	public function getName() {
95
		return $this->direct->getToken();
96
	}
97
98
	public function setName($name) {
99
		throw new Forbidden();
100
	}
101
102
	public function getLastModified() {
103
		$this->getFile();
104
105
		return $this->file->getMTime();
106
	}
107
108
	private function getFile() {
109
		if ($this->file === null) {
110
			$userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
111
			$files = $userFolder->getById($this->direct->getFileId());
112
113
			if ($files === []) {
114
				throw new NotFound();
115
			}
116
117
			$this->file = array_shift($files);
118
		}
119
120
		return $this->file;
121
	}
122
}
123