1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This program is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* This program is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace fkooman\RemoteStorage; |
19
|
|
|
|
20
|
|
|
use fkooman\RemoteStorage\Exception\RemoteStorageException; |
21
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
22
|
|
|
|
23
|
|
|
class RemoteStorage |
24
|
|
|
{ |
25
|
|
|
/** @var MetadataStorage */ |
26
|
|
|
private $md; |
27
|
|
|
|
28
|
|
|
/** @var DocumentStorage */ |
29
|
|
|
private $d; |
30
|
|
|
|
31
|
|
|
public function __construct(MetadataStorage $md, DocumentStorage $d) |
32
|
|
|
{ |
33
|
|
|
$this->md = $md; |
34
|
|
|
$this->d = $d; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function putDocument(Path $p, $contentType, $documentData, array $ifMatch = null, array $ifNoneMatch = null) |
38
|
|
|
{ |
39
|
|
View Code Duplication |
if (null !== $ifMatch && !in_array($this->md->getVersion($p), $ifMatch)) { |
|
|
|
|
40
|
|
|
throw new HttpException('version mismatch', 412); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
if (null !== $ifNoneMatch && in_array('*', $ifNoneMatch) && null !== $this->md->getVersion($p)) { |
|
|
|
|
44
|
|
|
throw new HttpException('document already exists', 412); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$updatedEntities = $this->d->putDocument($p, $documentData); |
48
|
|
|
$this->md->updateDocument($p, $contentType); |
49
|
|
|
foreach ($updatedEntities as $u) { |
50
|
|
|
$this->md->updateFolder(new Path($u)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function deleteDocument(Path $p, array $ifMatch = null) |
55
|
|
|
{ |
56
|
|
View Code Duplication |
if (null !== $ifMatch && !in_array($this->md->getVersion($p), $ifMatch)) { |
|
|
|
|
57
|
|
|
throw new HttpException('version mismatch', 412); |
58
|
|
|
} |
59
|
|
|
$deletedEntities = $this->d->deleteDocument($p); |
60
|
|
|
foreach ($deletedEntities as $d) { |
61
|
|
|
$this->md->deleteNode(new Path($d)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// increment the version from the folder containing the last deleted |
65
|
|
|
// folder and up to the user root, we cannot conveniently do this from |
66
|
|
|
// the MetadataStorage class :( |
67
|
|
|
foreach ($p->getFolderTreeToUserRoot() as $i) { |
68
|
|
|
if (null !== $this->md->getVersion(new Path($i))) { |
69
|
|
|
$this->md->updateFolder(new Path($i)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getVersion(Path $p) |
75
|
|
|
{ |
76
|
|
|
return $this->md->getVersion($p); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getContentType(Path $p) |
80
|
|
|
{ |
81
|
|
|
return $this->md->getContentType($p); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getDocument(Path $p, array $ifNoneMatch = null) |
85
|
|
|
{ |
86
|
|
View Code Duplication |
if (null !== $ifNoneMatch && in_array($this->md->getVersion($p), $ifNoneMatch)) { |
|
|
|
|
87
|
|
|
throw new RemoteStorageException('document not modified'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->d->getDocumentPath($p); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getFolder(Path $p, array $ifNoneMatch = null) |
94
|
|
|
{ |
95
|
|
View Code Duplication |
if (null !== $ifNoneMatch && in_array($this->md->getVersion($p), $ifNoneMatch)) { |
|
|
|
|
96
|
|
|
throw new RemoteStorageException('folder not modified'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$f = [ |
100
|
|
|
'@context' => 'http://remotestorage.io/spec/folder-description', |
101
|
|
|
'items' => $this->d->getFolder($p), |
102
|
|
|
]; |
103
|
|
|
foreach ($f['items'] as $name => $meta) { |
104
|
|
|
$f['items'][$name]['ETag'] = $this->md->getVersion(new Path($p->getFolderPath().$name)); |
105
|
|
|
|
106
|
|
|
// if item is a folder we don't want Content-Type |
107
|
|
|
if (strrpos($name, '/') !== strlen($name) - 1) { |
108
|
|
|
$f['items'][$name]['Content-Type'] = $this->md->getContentType(new Path($p->getFolderPath().$name)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return json_encode($f, JSON_FORCE_OBJECT); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getFolderSize(Path $p) |
116
|
|
|
{ |
117
|
|
|
return self::sizeToHuman($this->d->getFolderSize($p)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function sizeToHuman($byteSize) |
121
|
|
|
{ |
122
|
|
|
$kB = 1024; |
123
|
|
|
$MB = $kB * 1024; |
124
|
|
|
$GB = $MB * 1024; |
125
|
|
|
|
126
|
|
|
if ($byteSize > $GB) { |
127
|
|
|
return sprintf('%0.2fGB', $byteSize / $GB); |
128
|
|
|
} |
129
|
|
|
if ($byteSize > $MB) { |
130
|
|
|
return sprintf('%0.2fMB', $byteSize / $MB); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return sprintf('%0.0fkB', $byteSize / $kB); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.