1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Bart Visscher <[email protected]> |
4
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
5
|
|
|
* @author Morris Jobke <[email protected]> |
6
|
|
|
* @author Robin Appelman <[email protected]> |
7
|
|
|
* @author Thomas Müller <[email protected]> |
8
|
|
|
* @author Vincent Petry <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
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 OCA\Files_External\Lib\Storage; |
28
|
|
|
|
29
|
|
|
abstract class StreamWrapper extends \OC\Files\Storage\Common { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $path |
33
|
|
|
* @return string|null |
34
|
|
|
*/ |
35
|
|
|
abstract public function constructUrl($path); |
36
|
|
|
|
37
|
|
|
public function mkdir($path) { |
38
|
|
|
return mkdir($this->constructUrl($path)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function rmdir($path) { |
42
|
|
|
if ($this->is_dir($path) && $this->isDeletable($path)) { |
43
|
|
|
$dh = $this->opendir($path); |
44
|
|
|
if (!is_resource($dh)) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
View Code Duplication |
while (($file = readdir($dh)) !== false) { |
|
|
|
|
48
|
|
|
if ($this->is_dir($path . '/' . $file)) { |
49
|
|
|
$this->rmdir($path . '/' . $file); |
50
|
|
|
} else { |
51
|
|
|
$this->unlink($path . '/' . $file); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
$url = $this->constructUrl($path); |
55
|
|
|
$success = rmdir($url); |
56
|
|
|
clearstatcache(false, $url); |
57
|
|
|
return $success; |
58
|
|
|
} else { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function opendir($path) { |
64
|
|
|
return opendir($this->constructUrl($path)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function filetype($path) { |
68
|
|
|
return @filetype($this->constructUrl($path)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function file_exists($path) { |
72
|
|
|
return file_exists($this->constructUrl($path)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function unlink($path) { |
76
|
|
|
$url = $this->constructUrl($path); |
77
|
|
|
$success = unlink($url); |
78
|
|
|
// normally unlink() is supposed to do this implicitly, |
79
|
|
|
// but doing it anyway just to be sure |
80
|
|
|
clearstatcache(false, $url); |
81
|
|
|
return $success; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function fopen($path, $mode) { |
85
|
|
|
return fopen($this->constructUrl($path), $mode); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function touch($path, $mtime = null) { |
89
|
|
|
if ($this->file_exists($path)) { |
90
|
|
|
if (is_null($mtime)) { |
91
|
|
|
$fh = $this->fopen($path, 'a'); |
92
|
|
|
fwrite($fh, ''); |
93
|
|
|
fclose($fh); |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} else { |
97
|
|
|
return false; //not supported |
98
|
|
|
} |
99
|
|
|
} else { |
100
|
|
|
$this->file_put_contents($path, ''); |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $path |
107
|
|
|
* @param string $target |
108
|
|
|
*/ |
109
|
|
|
public function getFile($path, $target) { |
110
|
|
|
return copy($this->constructUrl($path), $target); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $target |
115
|
|
|
*/ |
116
|
|
|
public function uploadFile($path, $target) { |
117
|
|
|
return copy($path, $this->constructUrl($target)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function rename($path1, $path2) { |
121
|
|
|
return rename($this->constructUrl($path1), $this->constructUrl($path2)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function stat($path) { |
125
|
|
|
return stat($this->constructUrl($path)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.