|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Robin Appelman <[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
|
|
|
|
|
24
|
|
|
namespace OC\Files\ObjectStore; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\Files\ObjectStore\IObjectStore; |
|
27
|
|
|
use OCP\Files\Storage\IStorage; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Object store that wraps a storage backend, mostly for testing purposes |
|
31
|
|
|
*/ |
|
32
|
|
|
class StorageObjectStore implements IObjectStore { |
|
33
|
|
|
/** @var IStorage */ |
|
34
|
|
|
private $storage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param IStorage $storage |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(IStorage $storage) { |
|
40
|
|
|
$this->storage = $storage; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string the container or bucket name where objects are stored |
|
45
|
|
|
* @since 7.0.0 |
|
46
|
|
|
*/ |
|
47
|
|
|
function getStorageId() { |
|
|
|
|
|
|
48
|
|
|
$this->storage->getId(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
53
|
|
|
* @return resource stream with the read data |
|
54
|
|
|
* @throws \Exception when something goes wrong, message will be logged |
|
55
|
|
|
* @since 7.0.0 |
|
56
|
|
|
*/ |
|
57
|
|
|
function readObject($urn) { |
|
|
|
|
|
|
58
|
|
|
$handle = $this->storage->fopen($urn, 'r'); |
|
59
|
|
|
if ($handle) { |
|
|
|
|
|
|
60
|
|
|
return $handle; |
|
61
|
|
|
} else { |
|
62
|
|
|
throw new \Exception(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
68
|
|
|
* @param resource $stream stream with the data to write |
|
69
|
|
|
* @throws \Exception when something goes wrong, message will be logged |
|
70
|
|
|
* @since 7.0.0 |
|
71
|
|
|
*/ |
|
72
|
|
|
function writeObject($urn, $stream) { |
|
|
|
|
|
|
73
|
|
|
$handle = $this->storage->fopen($urn, 'w'); |
|
74
|
|
|
if ($handle) { |
|
|
|
|
|
|
75
|
|
|
stream_copy_to_stream($stream, $handle); |
|
76
|
|
|
fclose($handle); |
|
77
|
|
|
} else { |
|
78
|
|
|
throw new \Exception(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
84
|
|
|
* @return void |
|
85
|
|
|
* @throws \Exception when something goes wrong, message will be logged |
|
86
|
|
|
* @since 7.0.0 |
|
87
|
|
|
*/ |
|
88
|
|
|
function deleteObject($urn) { |
|
|
|
|
|
|
89
|
|
|
$this->storage->unlink($urn); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function objectExists($urn) { |
|
93
|
|
|
return $this->storage->file_exists($urn); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.