1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Joas Schilling <[email protected]> |
4
|
|
|
* @author Morris Jobke <[email protected]> |
5
|
|
|
* @author Robin Appelman <[email protected]> |
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
7
|
|
|
* @author Thomas Müller <[email protected]> |
8
|
|
|
* @author Vincent Petry <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
11
|
|
|
* @license AGPL-3.0 |
12
|
|
|
* |
13
|
|
|
* This code is free software: you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
15
|
|
|
* as published by the Free Software Foundation. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OC\Files\Node; |
28
|
|
|
|
29
|
|
|
use OCP\Files\IPreviewNode; |
30
|
|
|
use OCP\Files\NotPermittedException; |
31
|
|
|
use OCP\IImage; |
32
|
|
|
use Psr\Http\Message\StreamInterface; |
33
|
|
|
|
34
|
|
|
class File extends Node implements \OCP\Files\File, IPreviewNode { |
35
|
|
|
/** |
36
|
|
|
* Creates a Folder that represents a non-existing path |
37
|
|
|
* |
38
|
|
|
* @param string $path path |
39
|
|
|
* @return string non-existing node class |
40
|
|
|
*/ |
41
|
|
|
protected function createNonExistingNode($path) { |
42
|
|
|
return new NonExistingFile($this->root, $this->view, $path); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
* @throws \OCP\Files\NotPermittedException |
48
|
|
|
*/ |
49
|
|
|
public function getContent() { |
50
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
51
|
|
|
/** |
52
|
|
|
* @var \OC\Files\Storage\Storage $storage; |
53
|
|
|
*/ |
54
|
|
|
return $this->view->file_get_contents($this->path); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new NotPermittedException(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $data |
62
|
|
|
* @throws \OCP\Files\NotPermittedException |
63
|
|
|
*/ |
64
|
|
|
public function putContent($data) { |
65
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
66
|
|
|
$this->sendHooks(['preWrite']); |
67
|
|
|
$this->view->file_put_contents($this->path, $data); |
68
|
|
|
$this->fileInfo = null; |
69
|
|
|
$this->sendHooks(['postWrite']); |
70
|
|
|
} else { |
71
|
|
|
throw new NotPermittedException(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $mode |
77
|
|
|
* @return resource |
78
|
|
|
* @throws \OCP\Files\NotPermittedException |
79
|
|
|
* @deprecated 11.0.0 |
80
|
|
|
*/ |
81
|
|
|
public function fopen($mode) { |
82
|
|
|
throw new \BadMethodCallException('fopen is no longer allowed to be called'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function delete() { |
86
|
|
|
if (!$this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
87
|
|
|
throw new NotPermittedException(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->sendHooks(['preDelete']); |
91
|
|
|
$fileInfo = $this->getFileInfo(); |
92
|
|
|
$this->view->unlink($this->path); |
93
|
|
|
$nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); |
94
|
|
|
$this->root->emit('\OC\Files', 'postDelete', [$nonExisting]); |
95
|
|
|
$this->exists = false; |
|
|
|
|
96
|
|
|
$this->fileInfo = null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $type |
101
|
|
|
* @param bool $raw |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function hash($type, $raw = false) { |
105
|
|
|
return $this->view->hash($type, $this->path, $raw); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
|
|
public function getChecksum() { |
112
|
|
|
return $this->getFileInfo()->getChecksum(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $options |
117
|
|
|
* @return IImage |
118
|
|
|
* @since 10.1.0 |
119
|
|
|
*/ |
120
|
|
|
public function getThumbnail($options) { |
121
|
|
|
$maxX = \array_key_exists('x', $options) ? (int)$options['x'] : 32; |
122
|
|
|
$maxY = \array_key_exists('y', $options) ? (int)$options['y'] : 32; |
123
|
|
|
$scalingUp = \array_key_exists('scalingup', $options) ? (bool)$options['scalingup'] : true; |
124
|
|
|
$keepAspect = \array_key_exists('a', $options) ? true : false; |
125
|
|
|
$mode = \array_key_exists('mode', $options) ? $options['mode'] : 'fill'; |
126
|
|
|
|
127
|
|
|
$preview = new \OC\Preview(); |
128
|
|
|
$preview->setFile($this); |
129
|
|
|
$preview->setMaxX($maxX); |
130
|
|
|
$preview->setMaxY($maxY); |
131
|
|
|
$preview->setScalingUp($scalingUp); |
132
|
|
|
$preview->setMode($mode); |
133
|
|
|
$preview->setKeepAspect($keepAspect); |
134
|
|
|
if (\array_key_exists('mimeType', $options)) { |
135
|
|
|
$preview->setMimetype($options['mimeType']); |
136
|
|
|
} |
137
|
|
|
return $preview->getPreview(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $options |
142
|
|
|
* @return StreamInterface |
143
|
|
|
* @since 11.0.0 |
144
|
|
|
*/ |
145
|
|
|
public function readFile(array $options = []): StreamInterface { |
146
|
|
|
if (!$this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
147
|
|
|
throw new NotPermittedException(); |
148
|
|
|
} |
149
|
|
|
return $this->view->readFile($this->path, $options); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param StreamInterface $stream |
154
|
|
|
* @return int |
155
|
|
|
* @since 11.0.0 |
156
|
|
|
*/ |
157
|
|
|
public function writeFile(StreamInterface $stream): int { |
158
|
|
|
if (!$this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_READ)) { |
159
|
|
|
throw new NotPermittedException(); |
160
|
|
|
} |
161
|
|
|
$this->sendHooks(['preWrite']); |
162
|
|
|
$count = $this->view->writeFile($this->path, $stream); |
163
|
|
|
$this->sendHooks(['postWrite']); |
164
|
|
|
|
165
|
|
|
return $count; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: