2014-06-11 15:57:24 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2014-06-11 15:57:24 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2014-06-11 15:57:24 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-06-11 15:57:24 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-06-11 15:57:24 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-06-11 15:57:24 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-06-11 15:57:24 +04:00
|
|
|
namespace OC\Files\ObjectStore;
|
|
|
|
|
2020-01-14 11:47:18 +03:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
2018-02-15 12:50:23 +03:00
|
|
|
use function GuzzleHttp\Psr7\stream_for;
|
2017-12-04 18:34:53 +03:00
|
|
|
use Icewind\Streams\RetryWrapper;
|
2018-11-16 22:21:21 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2014-06-20 14:27:47 +04:00
|
|
|
use OCP\Files\ObjectStore\IObjectStore;
|
2017-03-28 12:02:18 +03:00
|
|
|
use OCP\Files\StorageAuthException;
|
2014-06-11 15:57:24 +04:00
|
|
|
|
2020-01-17 13:18:23 +03:00
|
|
|
const SWIFT_SEGMENT_SIZE = 1073741824; // 1GB
|
|
|
|
|
2014-06-20 14:27:47 +04:00
|
|
|
class Swift implements IObjectStore {
|
2014-06-24 16:42:52 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $params;
|
2014-06-11 15:57:24 +04:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
/** @var SwiftFactory */
|
2018-02-15 12:50:23 +03:00
|
|
|
private $swiftFactory;
|
2017-03-28 14:06:09 +03:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
public function __construct($params, SwiftFactory $connectionFactory = null) {
|
2018-03-16 17:20:16 +03:00
|
|
|
$this->swiftFactory = $connectionFactory ?: new SwiftFactory(
|
|
|
|
\OC::$server->getMemCacheFactory()->createDistributed('swift::'),
|
|
|
|
$params,
|
|
|
|
\OC::$server->getLogger()
|
|
|
|
);
|
2014-06-24 16:42:52 +04:00
|
|
|
$this->params = $params;
|
|
|
|
}
|
|
|
|
|
2018-02-08 16:10:33 +03:00
|
|
|
/**
|
2018-02-15 12:50:23 +03:00
|
|
|
* @return \OpenStack\ObjectStore\v1\Models\Container
|
|
|
|
* @throws StorageAuthException
|
2018-02-12 17:08:27 +03:00
|
|
|
* @throws \OCP\Files\StorageNotAvailableException
|
2018-02-08 16:10:33 +03:00
|
|
|
*/
|
2018-02-15 12:50:23 +03:00
|
|
|
private function getContainer() {
|
2018-02-12 17:08:27 +03:00
|
|
|
return $this->swiftFactory->getContainer();
|
2017-03-28 12:39:25 +03:00
|
|
|
}
|
|
|
|
|
2014-06-20 14:27:47 +04:00
|
|
|
/**
|
|
|
|
* @return string the container name where objects are stored
|
|
|
|
*/
|
2014-06-18 17:20:26 +04:00
|
|
|
public function getStorageId() {
|
2018-03-12 21:59:33 +03:00
|
|
|
if (isset($this->params['bucket'])) {
|
|
|
|
return $this->params['bucket'];
|
|
|
|
}
|
|
|
|
|
2014-06-24 16:42:52 +04:00
|
|
|
return $this->params['container'];
|
2014-06-18 17:20:26 +04:00
|
|
|
}
|
2014-06-18 00:06:56 +04:00
|
|
|
|
2014-06-12 00:15:42 +04:00
|
|
|
/**
|
2014-06-20 14:27:47 +04:00
|
|
|
* @param string $urn the unified resource name used to identify the object
|
|
|
|
* @param resource $stream stream with the data to write
|
2018-02-15 12:50:23 +03:00
|
|
|
* @throws \Exception from openstack lib when something goes wrong
|
2014-06-12 00:15:42 +04:00
|
|
|
*/
|
2014-06-20 14:27:47 +04:00
|
|
|
public function writeObject($urn, $stream) {
|
2019-04-18 22:04:27 +03:00
|
|
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile('swiftwrite');
|
|
|
|
file_put_contents($tmpFile, $stream);
|
|
|
|
$handle = fopen($tmpFile, 'rb');
|
2019-02-28 00:03:54 +03:00
|
|
|
|
2020-01-17 13:18:23 +03:00
|
|
|
if (filesize($tmpFile) < SWIFT_SEGMENT_SIZE) {
|
|
|
|
$this->getContainer()->createObject([
|
|
|
|
'name' => $urn,
|
|
|
|
'stream' => stream_for($handle)
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$this->getContainer()->createLargeObject([
|
|
|
|
'name' => $urn,
|
|
|
|
'stream' => stream_for($handle),
|
|
|
|
'segmentSize' => SWIFT_SEGMENT_SIZE
|
|
|
|
]);
|
|
|
|
}
|
2014-06-11 15:57:24 +04:00
|
|
|
}
|
2014-06-12 00:15:42 +04:00
|
|
|
|
|
|
|
/**
|
2014-06-20 14:27:47 +04:00
|
|
|
* @param string $urn the unified resource name used to identify the object
|
|
|
|
* @return resource stream with the read data
|
2020-01-14 11:47:18 +03:00
|
|
|
* @throws \Exception from openstack or GuzzleHttp libs when something goes wrong
|
2018-11-19 17:34:07 +03:00
|
|
|
* @throws NotFoundException if file does not exist
|
2014-06-12 00:15:42 +04:00
|
|
|
*/
|
2014-06-20 14:27:47 +04:00
|
|
|
public function readObject($urn) {
|
2018-11-16 22:21:21 +03:00
|
|
|
try {
|
2020-01-14 11:47:18 +03:00
|
|
|
$publicUri = $this->getContainer()->getObject($urn)->getPublicUri();
|
|
|
|
$tokenId = $this->swiftFactory->getCachedTokenId();
|
|
|
|
|
|
|
|
$response = (new Client())->request('GET', $publicUri,
|
|
|
|
[
|
|
|
|
'stream' => true,
|
|
|
|
'headers' => [
|
|
|
|
'X-Auth-Token' => $tokenId,
|
|
|
|
'Cache-Control' => 'no-cache'
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
if ($e->getResponse() && $e->getResponse()->getStatusCode() === 404) {
|
2018-11-16 22:21:21 +03:00
|
|
|
throw new NotFoundException("object $urn not found in object store");
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 14:27:47 +04:00
|
|
|
|
2020-01-14 11:47:18 +03:00
|
|
|
return RetryWrapper::wrap($response->getBody()->detach());
|
2014-06-11 15:57:24 +04:00
|
|
|
}
|
2014-06-12 00:15:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $urn Unified Resource Name
|
|
|
|
* @return void
|
2018-02-15 12:50:23 +03:00
|
|
|
* @throws \Exception from openstack lib when something goes wrong
|
2014-06-12 00:15:42 +04:00
|
|
|
*/
|
2014-06-18 00:06:56 +04:00
|
|
|
public function deleteObject($urn) {
|
2018-02-15 12:50:23 +03:00
|
|
|
$this->getContainer()->getObject($urn)->delete();
|
2014-06-11 15:57:24 +04:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:50:23 +03:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception from openstack lib when something goes wrong
|
|
|
|
*/
|
|
|
|
public function deleteContainer() {
|
|
|
|
$this->getContainer()->delete();
|
2014-06-13 19:22:21 +04:00
|
|
|
}
|
|
|
|
|
2018-12-10 19:20:45 +03:00
|
|
|
public function objectExists($urn) {
|
|
|
|
return $this->getContainer()->objectExists($urn);
|
|
|
|
}
|
2014-06-11 15:57:24 +04:00
|
|
|
}
|