Passed
Push — master ( 76c318...963d96 )
by Morris
88:25 queued 74:01
created

File::getExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 * @author Vincent Petry <[email protected]>
10
 *
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\GenericFileException;
30
use OCP\Files\NotPermittedException;
31
32
class File extends Node implements \OCP\Files\File {
33
	/**
34
	 * Creates a Folder that represents a non-existing path
35
	 *
36
	 * @param string $path path
37
	 * @return string non-existing node class
38
	 */
39
	protected function createNonExistingNode($path) {
40
		return new NonExistingFile($this->root, $this->view, $path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new OC\Files\Node...ot, $this->view, $path) returns the type OC\Files\Node\NonExistingFile which is incompatible with the documented return type string.
Loading history...
41
	}
42
43
	/**
44
	 * @return string
45
	 * @throws \OCP\Files\NotPermittedException
46
	 */
47
	public function getContent() {
48
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
49
			/**
50
			 * @var \OC\Files\Storage\Storage $storage;
51
			 */
52
			return $this->view->file_get_contents($this->path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->view->file...t_contents($this->path) could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
53
		} else {
54
			throw new NotPermittedException();
55
		}
56
	}
57
58
	/**
59
	 * @param string|resource $data
60
	 * @throws \OCP\Files\NotPermittedException
61
	 * @throws \OCP\Files\GenericFileException
62
	 */
63
	public function putContent($data) {
64
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
65
			$this->sendHooks(array('preWrite'));
66
			if ($this->view->file_put_contents($this->path, $data) === false) {
67
				throw new GenericFileException('file_put_contents failed');
68
			}
69
			$this->fileInfo = null;
70
			$this->sendHooks(array('postWrite'));
71
		} else {
72
			throw new NotPermittedException();
73
		}
74
	}
75
76
	/**
77
	 * @param string $mode
78
	 * @return resource
79
	 * @throws \OCP\Files\NotPermittedException
80
	 */
81
	public function fopen($mode) {
82
		$preHooks = array();
83
		$postHooks = array();
84
		$requiredPermissions = \OCP\Constants::PERMISSION_READ;
85
		switch ($mode) {
86
			case 'r+':
87
			case 'rb+':
88
			case 'w+':
89
			case 'wb+':
90
			case 'x+':
91
			case 'xb+':
92
			case 'a+':
93
			case 'ab+':
94
			case 'w':
95
			case 'wb':
96
			case 'x':
97
			case 'xb':
98
			case 'a':
99
			case 'ab':
100
				$preHooks[] = 'preWrite';
101
				$postHooks[] = 'postWrite';
102
				$requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
103
				break;
104
		}
105
106
		if ($this->checkPermissions($requiredPermissions)) {
107
			$this->sendHooks($preHooks);
108
			$result = $this->view->fopen($this->path, $mode);
109
			$this->sendHooks($postHooks);
110
			return $result;
111
		} else {
112
			throw new NotPermittedException();
113
		}
114
	}
115
116
	public function delete() {
117
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
118
			$this->sendHooks(array('preDelete'));
119
			$fileInfo = $this->getFileInfo();
120
			$this->view->unlink($this->path);
121
			$nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
122
			$this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
123
			$this->exists = false;
0 ignored issues
show
Bug Best Practice introduced by
The property exists does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
124
			$this->fileInfo = null;
125
		} else {
126
			throw new NotPermittedException();
127
		}
128
	}
129
130
	/**
131
	 * @param string $type
132
	 * @param bool $raw
133
	 * @return string
134
	 */
135
	public function hash($type, $raw = false) {
136
		return $this->view->hash($type, $this->path, $raw);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->view->hash...ype, $this->path, $raw) also could return the type boolean which is incompatible with the documented return type string.
Loading history...
137
	}
138
139
	/**
140
	 * @inheritdoc
141
	 */
142
	public function getChecksum() {
143
		return $this->getFileInfo()->getChecksum();
144
	}
145
146
	public function getExtension(): string {
147
		return $this->getFileInfo()->getExtension();
148
	}
149
}
150