Completed
Push — master ( 8ccb99...229289 )
by Morris
20:27
created

SimpleFile::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2016 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 OC\Files\SimpleFS;
24
25
use OCP\Files\File;
26
use OCP\Files\NotFoundException;
27
use OCP\Files\NotPermittedException;
28
use OCP\Files\SimpleFS\ISimpleFile;
29
30
class SimpleFile implements ISimpleFile  {
31
32
	/** @var File $file */
33
	private $file;
34
35
	/**
36
	 * File constructor.
37
	 *
38
	 * @param File $file
39
	 */
40
	public function __construct(File $file) {
41
		$this->file = $file;
42
	}
43
44
	/**
45
	 * Get the name
46
	 *
47
	 * @return string
48
	 */
49
	public function getName() {
50
		return $this->file->getName();
51
	}
52
53
	/**
54
	 * Get the size in bytes
55
	 *
56
	 * @return int
57
	 */
58
	public function getSize() {
59
		return $this->file->getSize();
60
	}
61
62
	/**
63
	 * Get the ETag
64
	 *
65
	 * @return string
66
	 */
67
	public function getETag() {
68
		return $this->file->getEtag();
69
	}
70
71
	/**
72
	 * Get the last modification time
73
	 *
74
	 * @return int
75
	 */
76
	public function getMTime() {
77
		return $this->file->getMTime();
78
	}
79
80
	/**
81
	 * Get the content
82
	 *
83
	 * @throws NotPermittedException
84
	 * @throws NotFoundException
85
	 * @return string
86
	 */
87
	public function getContent() {
88
		$result = $this->file->getContent();
89
90
		if ($result === false) {
91
			$this->checkFile();
92
		}
93
94
		return $result;
95
	}
96
97
	/**
98
	 * Overwrite the file
99
	 *
100
	 * @param string|resource $data
101
	 * @throws NotPermittedException
102
	 */
103
	public function putContent($data) {
104
		$this->file->putContent($data);
105
	}
106
107
	/**
108
	 * Sometimes there are some issues with the AppData. Most of them are from
109
	 * user error. But we should handle them gracefull anyway.
110
	 *
111
	 * If for some reason the current file can't be found. We remove it.
112
	 * Then traverse up and check all folders if they exists. This so that the
113
	 * next request will have a valid appdata structure again.
114
	 *
115
	 * @throws NotFoundException
116
	 */
117
	private function checkFile() {
118
		$cur = $this->file;
119
120
		while ($cur->stat() === false) {
121
			$parent = $cur->getParent();
122
			$cur->delete();
123
			$cur = $parent;
124
		}
125
126
		if ($cur !== $this->file) {
127
			throw new NotFoundException('File does not exist');
128
		}
129
	}
130
131
132
	/**
133
	 * Delete the file
134
	 *
135
	 * @throws NotPermittedException
136
	 */
137
	public function delete() {
138
		$this->file->delete();
139
	}
140
141
	/**
142
	 * Get the MimeType
143
	 *
144
	 * @return string
145
	 */
146
	public function getMimeType() {
147
		return $this->file->getMimeType();
148
	}
149
150
	/**
151
	 * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
152
	 *
153
	 * @return resource
154
	 * @throws \OCP\Files\NotPermittedException
155
	 * @since 14.0.0
156
	 */
157
	public function read() {
158
		return $this->file->fopen('r');
159
	}
160
161
	/**
162
	 * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
163
	 *
164
	 * @return resource
165
	 * @throws \OCP\Files\NotPermittedException
166
	 * @since 14.0.0
167
	 */
168
	public function write() {
169
		return $this->file->fopen('w');
170
	}
171
172
}
173