2014-06-11 07:57:24 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2014-06-11 07:57:24 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-06-11 07:57:24 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\ObjectStore;
|
|
|
|
|
|
2020-01-14 03:47:18 -05:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
2021-10-21 06:37:51 -04:00
|
|
|
use GuzzleHttp\Psr7\Utils;
|
2017-12-04 10:34:53 -05:00
|
|
|
use Icewind\Streams\RetryWrapper;
|
2018-11-16 14:21:21 -05:00
|
|
|
use OCP\Files\NotFoundException;
|
2014-06-20 06:27:47 -04:00
|
|
|
use OCP\Files\ObjectStore\IObjectStore;
|
2017-03-28 05:02:18 -04:00
|
|
|
use OCP\Files\StorageAuthException;
|
2022-03-17 12:26:27 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-06-11 07:57:24 -04:00
|
|
|
|
2020-01-17 05:18:23 -05:00
|
|
|
const SWIFT_SEGMENT_SIZE = 1073741824; // 1GB
|
|
|
|
|
|
2014-06-20 06:27:47 -04:00
|
|
|
class Swift implements IObjectStore {
|
2014-06-24 08:42:52 -04:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $params;
|
2014-06-11 07:57:24 -04:00
|
|
|
|
2018-02-12 09:08:27 -05:00
|
|
|
/** @var SwiftFactory */
|
2018-02-15 04:50:23 -05:00
|
|
|
private $swiftFactory;
|
2017-03-28 07:06:09 -04:00
|
|
|
|
2024-03-28 11:13:19 -04:00
|
|
|
public function __construct($params, ?SwiftFactory $connectionFactory = null) {
|
2018-03-16 10:20:16 -04:00
|
|
|
$this->swiftFactory = $connectionFactory ?: new SwiftFactory(
|
|
|
|
|
\OC::$server->getMemCacheFactory()->createDistributed('swift::'),
|
|
|
|
|
$params,
|
2022-03-17 12:26:27 -04:00
|
|
|
\OC::$server->get(LoggerInterface::class)
|
2018-03-16 10:20:16 -04:00
|
|
|
);
|
2014-06-24 08:42:52 -04:00
|
|
|
$this->params = $params;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 08:10:33 -05:00
|
|
|
/**
|
2018-02-15 04:50:23 -05:00
|
|
|
* @return \OpenStack\ObjectStore\v1\Models\Container
|
|
|
|
|
* @throws StorageAuthException
|
2018-02-12 09:08:27 -05:00
|
|
|
* @throws \OCP\Files\StorageNotAvailableException
|
2018-02-08 08:10:33 -05:00
|
|
|
*/
|
2018-02-15 04:50:23 -05:00
|
|
|
private function getContainer() {
|
2018-02-12 09:08:27 -05:00
|
|
|
return $this->swiftFactory->getContainer();
|
2017-03-28 05:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
2014-06-20 06:27:47 -04:00
|
|
|
/**
|
|
|
|
|
* @return string the container name where objects are stored
|
|
|
|
|
*/
|
2014-06-18 09:20:26 -04:00
|
|
|
public function getStorageId() {
|
2018-03-12 14:59:33 -04:00
|
|
|
if (isset($this->params['bucket'])) {
|
|
|
|
|
return $this->params['bucket'];
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-24 08:42:52 -04:00
|
|
|
return $this->params['container'];
|
2014-06-18 09:20:26 -04:00
|
|
|
}
|
2014-06-17 16:06:56 -04:00
|
|
|
|
2024-03-28 11:13:19 -04:00
|
|
|
public function writeObject($urn, $stream, ?string $mimetype = null) {
|
2019-04-18 15:04:27 -04:00
|
|
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile('swiftwrite');
|
|
|
|
|
file_put_contents($tmpFile, $stream);
|
|
|
|
|
$handle = fopen($tmpFile, 'rb');
|
2019-02-27 16:03:54 -05:00
|
|
|
|
2020-01-17 05:18:23 -05:00
|
|
|
if (filesize($tmpFile) < SWIFT_SEGMENT_SIZE) {
|
|
|
|
|
$this->getContainer()->createObject([
|
|
|
|
|
'name' => $urn,
|
2021-10-21 06:37:51 -04:00
|
|
|
'stream' => Utils::streamFor($handle),
|
2021-04-15 11:14:57 -04:00
|
|
|
'contentType' => $mimetype,
|
2020-01-17 05:18:23 -05:00
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$this->getContainer()->createLargeObject([
|
|
|
|
|
'name' => $urn,
|
2021-10-21 06:37:51 -04:00
|
|
|
'stream' => Utils::streamFor($handle),
|
2020-11-05 10:30:05 -05:00
|
|
|
'segmentSize' => SWIFT_SEGMENT_SIZE,
|
2021-04-15 11:14:57 -04:00
|
|
|
'contentType' => $mimetype,
|
2020-01-17 05:18:23 -05:00
|
|
|
]);
|
|
|
|
|
}
|
2014-06-11 07:57:24 -04:00
|
|
|
}
|
2014-06-11 16:15:42 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-06-20 06: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 03:47:18 -05:00
|
|
|
* @throws \Exception from openstack or GuzzleHttp libs when something goes wrong
|
2018-11-19 09:34:07 -05:00
|
|
|
* @throws NotFoundException if file does not exist
|
2014-06-11 16:15:42 -04:00
|
|
|
*/
|
2014-06-20 06:27:47 -04:00
|
|
|
public function readObject($urn) {
|
2018-11-16 14:21:21 -05:00
|
|
|
try {
|
2020-01-14 03:47:18 -05:00
|
|
|
$publicUri = $this->getContainer()->getObject($urn)->getPublicUri();
|
|
|
|
|
$tokenId = $this->swiftFactory->getCachedTokenId();
|
|
|
|
|
|
|
|
|
|
$response = (new Client())->request('GET', $publicUri,
|
|
|
|
|
[
|
|
|
|
|
'stream' => true,
|
|
|
|
|
'headers' => [
|
|
|
|
|
'X-Auth-Token' => $tokenId,
|
2020-11-05 10:30:05 -05:00
|
|
|
'Cache-Control' => 'no-cache',
|
2020-01-14 03:47:18 -05:00
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
|
if ($e->getResponse() && $e->getResponse()->getStatusCode() === 404) {
|
2018-11-16 14:21:21 -05:00
|
|
|
throw new NotFoundException("object $urn not found in object store");
|
|
|
|
|
} else {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-20 06:27:47 -04:00
|
|
|
|
2020-01-14 03:47:18 -05:00
|
|
|
return RetryWrapper::wrap($response->getBody()->detach());
|
2014-06-11 07:57:24 -04:00
|
|
|
}
|
2014-06-11 16:15:42 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $urn Unified Resource Name
|
|
|
|
|
* @return void
|
2018-02-15 04:50:23 -05:00
|
|
|
* @throws \Exception from openstack lib when something goes wrong
|
2014-06-11 16:15:42 -04:00
|
|
|
*/
|
2014-06-17 16:06:56 -04:00
|
|
|
public function deleteObject($urn) {
|
2018-02-15 04:50:23 -05:00
|
|
|
$this->getContainer()->getObject($urn)->delete();
|
2014-06-11 07:57:24 -04:00
|
|
|
}
|
|
|
|
|
|
2018-02-15 04:50:23 -05:00
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Exception from openstack lib when something goes wrong
|
|
|
|
|
*/
|
|
|
|
|
public function deleteContainer() {
|
|
|
|
|
$this->getContainer()->delete();
|
2014-06-13 11:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
2018-12-10 11:20:45 -05:00
|
|
|
public function objectExists($urn) {
|
|
|
|
|
return $this->getContainer()->objectExists($urn);
|
|
|
|
|
}
|
2020-11-05 10:30:05 -05:00
|
|
|
|
|
|
|
|
public function copyObject($from, $to) {
|
|
|
|
|
$this->getContainer()->getObject($from)->copy([
|
|
|
|
|
'destination' => $this->getContainer()->name . '/' . $to
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-08-15 08:04:25 -04:00
|
|
|
public function preSignedUrl(string $urn, \DateTimeInterface $expiration): ?string {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-06-11 07:57:24 -04:00
|
|
|
}
|