Passed
Push — master ( e165dc...217243 )
by Roeland
12:21 queued 10s
created

StorageObjectStore::copyObject()   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 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Robin Appelman <[email protected]>
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Files\ObjectStore;
26
27
use OCP\Files\ObjectStore\IObjectStore;
28
use OCP\Files\Storage\IStorage;
29
30
/**
31
 * Object store that wraps a storage backend, mostly for testing purposes
32
 */
33
class StorageObjectStore implements IObjectStore {
34
	/** @var IStorage */
35
	private $storage;
36
37
	/**
38
	 * @param IStorage $storage
39
	 */
40
	public function __construct(IStorage $storage) {
41
		$this->storage = $storage;
42
	}
43
44
	/**
45
	 * @return string the container or bucket name where objects are stored
46
	 * @since 7.0.0
47
	 */
48
	public function getStorageId() {
49
		$this->storage->getId();
50
	}
51
52
	/**
53
	 * @param string $urn the unified resource name used to identify the object
54
	 * @return resource stream with the read data
55
	 * @throws \Exception when something goes wrong, message will be logged
56
	 * @since 7.0.0
57
	 */
58
	public function readObject($urn) {
59
		$handle = $this->storage->fopen($urn, 'r');
60
		if ($handle) {
0 ignored issues
show
introduced by
$handle is of type false|resource, thus it always evaluated to false.
Loading history...
61
			return $handle;
62
		} else {
63
			throw new \Exception();
64
		}
65
	}
66
67
	/**
68
	 * @param string $urn the unified resource name used to identify the object
69
	 * @param resource $stream stream with the data to write
70
	 * @throws \Exception when something goes wrong, message will be logged
71
	 * @since 7.0.0
72
	 */
73
	public function writeObject($urn, $stream) {
74
		$handle = $this->storage->fopen($urn, 'w');
75
		if ($handle) {
0 ignored issues
show
introduced by
$handle is of type false|resource, thus it always evaluated to false.
Loading history...
76
			stream_copy_to_stream($stream, $handle);
77
			fclose($handle);
78
		} else {
79
			throw new \Exception();
80
		}
81
	}
82
83
	/**
84
	 * @param string $urn the unified resource name used to identify the object
85
	 * @return void
86
	 * @throws \Exception when something goes wrong, message will be logged
87
	 * @since 7.0.0
88
	 */
89
	public function deleteObject($urn) {
90
		$this->storage->unlink($urn);
91
	}
92
93
	public function objectExists($urn) {
94
		return $this->storage->file_exists($urn);
95
	}
96
97
	public function copyObject($from, $to) {
98
		$this->storage->copy($from, $to);
99
	}
100
}
101