1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright 2016 Roeland Jago Douma <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Christoph Wurst <[email protected]> |
6
|
|
|
* @author Julius Härtl <[email protected]> |
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license GNU AGPL version 3 or any later version |
10
|
|
|
* |
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
14
|
|
|
* License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OC\Files\SimpleFS; |
27
|
|
|
|
28
|
|
|
use OCP\Files\File; |
29
|
|
|
use OCP\Files\NotFoundException; |
30
|
|
|
use OCP\Files\NotPermittedException; |
31
|
|
|
use OCP\Files\SimpleFS\ISimpleFile; |
32
|
|
|
|
33
|
|
|
class SimpleFile implements ISimpleFile { |
34
|
|
|
|
35
|
|
|
/** @var File $file */ |
36
|
|
|
private $file; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* File constructor. |
40
|
|
|
* |
41
|
|
|
* @param File $file |
42
|
|
|
*/ |
43
|
|
|
public function __construct(File $file) { |
44
|
|
|
$this->file = $file; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the name |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getName() { |
53
|
|
|
return $this->file->getName(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the size in bytes |
58
|
|
|
* |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
public function getSize() { |
62
|
|
|
return $this->file->getSize(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the ETag |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getETag() { |
71
|
|
|
return $this->file->getEtag(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the last modification time |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
public function getMTime() { |
80
|
|
|
return $this->file->getMTime(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the content |
85
|
|
|
* |
86
|
|
|
* @throws NotPermittedException |
87
|
|
|
* @throws NotFoundException |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getContent() { |
91
|
|
|
$result = $this->file->getContent(); |
92
|
|
|
|
93
|
|
|
if ($result === false) { |
|
|
|
|
94
|
|
|
$this->checkFile(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Overwrite the file |
102
|
|
|
* |
103
|
|
|
* @param string|resource $data |
104
|
|
|
* @throws NotPermittedException |
105
|
|
|
* @throws NotFoundException |
106
|
|
|
*/ |
107
|
|
|
public function putContent($data) { |
108
|
|
|
try { |
109
|
|
|
return $this->file->putContent($data); |
110
|
|
|
} catch (NotFoundException $e) { |
111
|
|
|
$this->checkFile(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sometimes there are some issues with the AppData. Most of them are from |
117
|
|
|
* user error. But we should handle them gracefull anyway. |
118
|
|
|
* |
119
|
|
|
* If for some reason the current file can't be found. We remove it. |
120
|
|
|
* Then traverse up and check all folders if they exists. This so that the |
121
|
|
|
* next request will have a valid appdata structure again. |
122
|
|
|
* |
123
|
|
|
* @throws NotFoundException |
124
|
|
|
*/ |
125
|
|
|
private function checkFile() { |
126
|
|
|
$cur = $this->file; |
127
|
|
|
|
128
|
|
|
while ($cur->stat() === false) { |
129
|
|
|
$parent = $cur->getParent(); |
130
|
|
|
try { |
131
|
|
|
$cur->delete(); |
132
|
|
|
} catch (NotFoundException $e) { |
133
|
|
|
// Just continue then |
134
|
|
|
} |
135
|
|
|
$cur = $parent; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($cur !== $this->file) { |
139
|
|
|
throw new NotFoundException('File does not exist'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Delete the file |
146
|
|
|
* |
147
|
|
|
* @throws NotPermittedException |
148
|
|
|
*/ |
149
|
|
|
public function delete() { |
150
|
|
|
$this->file->delete(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the MimeType |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getMimeType() { |
159
|
|
|
return $this->file->getMimeType(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen |
164
|
|
|
* |
165
|
|
|
* @return resource |
166
|
|
|
* @throws \OCP\Files\NotPermittedException |
167
|
|
|
* @since 14.0.0 |
168
|
|
|
*/ |
169
|
|
|
public function read() { |
170
|
|
|
return $this->file->fopen('r'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen |
175
|
|
|
* |
176
|
|
|
* @return resource |
177
|
|
|
* @throws \OCP\Files\NotPermittedException |
178
|
|
|
* @since 14.0.0 |
179
|
|
|
*/ |
180
|
|
|
public function write() { |
181
|
|
|
return $this->file->fopen('w'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function getId(): int { |
185
|
|
|
return $this->file->getId(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|