migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de>
*
* @author Morris Jobke <hey@morrisjobke.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
class AppdataPreviewObjectStoreStorage extends ObjectStoreStorage {
/** @var string */
private $internalId;
public function __construct($params) {
if (!isset($params['internal-id'])) {
throw new \Exception('missing id in parameters');
}
$this->internalId = (string)$params['internal-id'];
parent::__construct($params);
}
public function getId() {
return 'object::appdata::preview:' . $this->internalId;
}
}
@@ -0,0 +1,136 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
*
* @author Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use OCP\Files\ObjectStore\IObjectStore;
class Azure implements IObjectStore {
/** @var string */
private $containerName;
/** @var string */
private $accountName;
/** @var string */
private $accountKey;
/** @var BlobRestProxy|null */
private $blobClient = null;
/** @var string|null */
private $endpoint = null;
/** @var bool */
private $autoCreate = false;
/**
* @param array $parameters
*/
public function __construct($parameters) {
$this->containerName = $parameters['container'];
$this->accountName = $parameters['account_name'];
$this->accountKey = $parameters['account_key'];
if (isset($parameters['endpoint'])) {
$this->endpoint = $parameters['endpoint'];
}
if (isset($parameters['autocreate'])) {
$this->autoCreate = $parameters['autocreate'];
}
}
/**
* @return BlobRestProxy
*/
private function getBlobClient() {
if (!$this->blobClient) {
$protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https';
$connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey;
if ($this->endpoint) {
$connectionString .= ';BlobEndpoint=' . $this->endpoint;
}
$this->blobClient = BlobRestProxy::createBlobService($connectionString);
if ($this->autoCreate) {
try {
$this->blobClient->createContainer($this->containerName);
} catch (ServiceException $e) {
if ($e->getCode() === 409) {
// already exists
} else {
throw $e;
}
}
}
}
return $this->blobClient;
}
/**
* @return string the container or bucket name where objects are stored
*/
public function getStorageId() {
return 'azure::blob::' . $this->containerName;
}
/**
* @param string $urn the unified resource name used to identify the object
* @return resource stream with the read data
* @throws \Exception when something goes wrong, message will be logged
*/
public function readObject($urn) {
$blob = $this->getBlobClient()->getBlob($this->containerName, $urn);
return $blob->getContentStream();
}
public function writeObject($urn, $stream, string $mimetype = null) {
$options = new CreateBlockBlobOptions();
if ($mimetype) {
$options->setContentType($mimetype);
}
$this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream, $options);
}
/**
* @param string $urn the unified resource name used to identify the object
* @return void
* @throws \Exception when something goes wrong, message will be logged
*/
public function deleteObject($urn) {
$this->getBlobClient()->deleteBlob($this->containerName, $urn);
}
public function objectExists($urn) {
try {
$this->getBlobClient()->getBlobMetadata($this->containerName, $urn);
return true;
} catch (ServiceException $e) {
if ($e->getCode() === 404) {
return false;
} else {
throw $e;
}
}
}
public function copyObject($from, $to) {
$this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from);
}
}
@@ -0,0 +1,68 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Björn Schießle <bjoern@schiessle.org>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\ObjectStore;
use OC\User\User;
use OCP\IUser;
class HomeObjectStoreStorage extends ObjectStoreStorage implements \OCP\Files\IHomeStorage {
/**
* The home user storage requires a user object to create a unique storage id
* @param array $params
*/
public function __construct($params) {
if (! isset($params['user']) || ! $params['user'] instanceof User) {
throw new \Exception('missing user object in parameters');
}
$this->user = $params['user'];
parent::__construct($params);
}
public function getId() {
return 'object::user:' . $this->user->getUID();
}
/**
* get the owner of a path
*
* @param string $path The path to get the owner
* @return false|string uid
*/
public function getOwner($path) {
if (is_object($this->user)) {
return $this->user->getUID();
}
return false;
}
/**
* @param string $path, optional
* @return \OC\User\User
*/
public function getUser($path = null): IUser {
return $this->user;
}
}
@@ -0,0 +1,69 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\ObjectStore;
use OCP\IConfig;
use OCP\IUser;
/**
* Class Mapper
*
* @package OC\Files\ObjectStore
*
* Map a user to a bucket.
*/
class Mapper {
/** @var IUser */
private $user;
/** @var IConfig */
private $config;
/**
* Mapper constructor.
*
* @param IUser $user
* @param IConfig $config
*/
public function __construct(IUser $user, IConfig $config) {
$this->user = $user;
$this->config = $config;
}
/**
* @param int $numBuckets
* @return string
*/
public function getBucket($numBuckets = 64) {
// Get the bucket config and shift if provided.
// Allow us to prevent writing in old filled buckets
$config = $this->config->getSystemValue('objectstore_multibucket');
$minBucket = is_array($config) && isset($config['arguments']['min_bucket'])
? (int)$config['arguments']['min_bucket']
: 0;
$hash = md5($this->user->getUID());
$num = hexdec(substr($hash, 0, 4));
return (string)(($num % ($numBuckets - $minBucket)) + $minBucket);
}
}
@@ -0,0 +1,98 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\ObjectStore;
use OC\Files\Cache\Scanner;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\FileInfo;
class ObjectStoreScanner extends Scanner {
public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true, $data = null) {
return [];
}
public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
return [];
}
protected function scanChildren(string $path, $recursive, int $reuse, int $folderId, bool $lock, int|float $oldSize, &$etagChanged = false) {
return 0;
}
public function backgroundScan() {
$lastPath = null;
// find any path marked as unscanned and run the scanner until no more paths are unscanned (or we get stuck)
// we sort by path DESC to ensure that contents of a folder are handled before the parent folder
while (($path = $this->getIncomplete()) !== false && $path !== $lastPath) {
$this->runBackgroundScanJob(function () use ($path) {
$item = $this->cache->get($path);
if ($item && $item->getMimeType() !== FileInfo::MIMETYPE_FOLDER) {
$fh = $this->storage->fopen($path, 'r');
if ($fh) {
$stat = fstat($fh);
if ($stat['size']) {
$this->cache->update($item->getId(), ['size' => $stat['size']]);
}
}
}
}, $path);
// FIXME: this won't proceed with the next item, needs revamping of getIncomplete()
// to make this possible
$lastPath = $path;
}
}
/**
* Unlike the default Cache::getIncomplete this one sorts by path.
*
* This is needed since self::backgroundScan doesn't fix child entries when running on a parent folder.
* By sorting by path we ensure that we encounter the child entries first.
*
* @return false|string
* @throws \OCP\DB\Exception
*/
private function getIncomplete() {
$query = $this->connection->getQueryBuilder();
$query->select('path')
->from('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->orderBy('path', 'DESC')
->setMaxResults(1);
$result = $query->executeQuery();
$path = $result->fetchOne();
$result->closeCursor();
if ($path === false) {
return false;
}
// Make sure Oracle does not continue with null for empty strings
return (string)$path;
}
}
@@ -0,0 +1,741 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Bjoern Schiessle <bjoern@schiessle.org>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Marcel Klehr <mklehr@gmx.net>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
*
* @license AGPL-3.0
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\ObjectStore;
use Aws\S3\Exception\S3Exception;
use Aws\S3\Exception\S3MultipartUploadException;
use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\CountWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Files\Cache\Cache;
use OC\Files\Cache\CacheEntry;
use OC\Files\Storage\PolyFill\CopyDirectory;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileInfo;
use OCP\Files\GenericFileException;
use OCP\Files\NotFoundException;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
use OCP\Files\Storage\IChunkedFileWrite;
use OCP\Files\Storage\IStorage;
class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFileWrite {
use CopyDirectory;
/**
* @var \OCP\Files\ObjectStore\IObjectStore $objectStore
*/
protected $objectStore;
/**
* @var string $id
*/
protected $id;
/**
* @var \OC\User\User $user
*/
protected $user;
private $objectPrefix = 'urn:oid:';
private $logger;
private bool $handleCopiesAsOwned;
/** @var bool */
protected $validateWrites = true;
public function __construct($params) {
if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) {
$this->objectStore = $params['objectstore'];
} else {
throw new \Exception('missing IObjectStore instance');
}
if (isset($params['storageid'])) {
$this->id = 'object::store:' . $params['storageid'];
} else {
$this->id = 'object::store:' . $this->objectStore->getStorageId();
}
if (isset($params['objectPrefix'])) {
$this->objectPrefix = $params['objectPrefix'];
}
if (isset($params['validateWrites'])) {
$this->validateWrites = (bool)$params['validateWrites'];
}
$this->handleCopiesAsOwned = (bool)($params['handleCopiesAsOwned'] ?? false);
$this->logger = \OC::$server->getLogger();
}
public function mkdir($path, bool $force = false) {
$path = $this->normalizePath($path);
if (!$force && $this->file_exists($path)) {
$this->logger->warning("Tried to create an object store folder that already exists: $path");
return false;
}
$mTime = time();
$data = [
'mimetype' => 'httpd/unix-directory',
'size' => 0,
'mtime' => $mTime,
'storage_mtime' => $mTime,
'permissions' => \OCP\Constants::PERMISSION_ALL,
];
if ($path === '') {
//create root on the fly
$data['etag'] = $this->getETag('');
$this->getCache()->put('', $data);
return true;
} else {
// if parent does not exist, create it
$parent = $this->normalizePath(dirname($path));
$parentType = $this->filetype($parent);
if ($parentType === false) {
if (!$this->mkdir($parent)) {
// something went wrong
$this->logger->warning("Parent folder ($parent) doesn't exist and couldn't be created");
return false;
}
} elseif ($parentType === 'file') {
// parent is a file
$this->logger->warning("Parent ($parent) is a file");
return false;
}
// finally create the new dir
$mTime = time(); // update mtime
$data['mtime'] = $mTime;
$data['storage_mtime'] = $mTime;
$data['etag'] = $this->getETag($path);
$this->getCache()->put($path, $data);
return true;
}
}
/**
* @param string $path
* @return string
*/
private function normalizePath($path) {
$path = trim($path, '/');
//FIXME why do we sometimes get a path like 'files//username'?
$path = str_replace('//', '/', $path);
// dirname('/folder') returns '.' but internally (in the cache) we store the root as ''
if (!$path || $path === '.') {
$path = '';
}
return $path;
}
/**
* Object Stores use a NoopScanner because metadata is directly stored in
* the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere.
*
* @param string $path
* @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
* @return \OC\Files\ObjectStore\ObjectStoreScanner
*/
public function getScanner($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($this->scanner)) {
$this->scanner = new ObjectStoreScanner($storage);
}
return $this->scanner;
}
public function getId() {
return $this->id;
}
public function rmdir($path) {
$path = $this->normalizePath($path);
$entry = $this->getCache()->get($path);
if (!$entry || $entry->getMimeType() !== ICacheEntry::DIRECTORY_MIMETYPE) {
return false;
}
return $this->rmObjects($entry);
}
private function rmObjects(ICacheEntry $entry): bool {
$children = $this->getCache()->getFolderContentsById($entry->getId());
foreach ($children as $child) {
if ($child->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
if (!$this->rmObjects($child)) {
return false;
}
} else {
if (!$this->rmObject($child)) {
return false;
}
}
}
$this->getCache()->remove($entry->getPath());
return true;
}
public function unlink($path) {
$path = $this->normalizePath($path);
$entry = $this->getCache()->get($path);
if ($entry instanceof ICacheEntry) {
if ($entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
return $this->rmObjects($entry);
} else {
return $this->rmObject($entry);
}
}
return false;
}
public function rmObject(ICacheEntry $entry): bool {
try {
$this->objectStore->deleteObject($this->getURN($entry->getId()));
} catch (\Exception $ex) {
if ($ex->getCode() !== 404) {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not delete object ' . $this->getURN($entry->getId()) . ' for ' . $entry->getPath(),
]);
return false;
}
//removing from cache is ok as it does not exist in the objectstore anyway
}
$this->getCache()->remove($entry->getPath());
return true;
}
public function stat($path) {
$path = $this->normalizePath($path);
$cacheEntry = $this->getCache()->get($path);
if ($cacheEntry instanceof CacheEntry) {
return $cacheEntry->getData();
} else {
if ($path === '') {
$this->mkdir('', true);
$cacheEntry = $this->getCache()->get($path);
if ($cacheEntry instanceof CacheEntry) {
return $cacheEntry->getData();
}
}
return false;
}
}
public function getPermissions($path) {
$stat = $this->stat($path);
if (is_array($stat) && isset($stat['permissions'])) {
return $stat['permissions'];
}
return parent::getPermissions($path);
}
/**
* Override this method if you need a different unique resource identifier for your object storage implementation.
* The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users.
* You may need a mapping table to store your URN if it cannot be generated from the fileid.
*
* @param int $fileId the fileid
* @return null|string the unified resource name used to identify the object
*/
public function getURN($fileId) {
if (is_numeric($fileId)) {
return $this->objectPrefix . $fileId;
}
return null;
}
public function opendir($path) {
$path = $this->normalizePath($path);
try {
$files = [];
$folderContents = $this->getCache()->getFolderContents($path);
foreach ($folderContents as $file) {
$files[] = $file['name'];
}
return IteratorDirectory::wrap($files);
} catch (\Exception $e) {
$this->logger->logException($e);
return false;
}
}
public function filetype($path) {
$path = $this->normalizePath($path);
$stat = $this->stat($path);
if ($stat) {
if ($stat['mimetype'] === 'httpd/unix-directory') {
return 'dir';
}
return 'file';
} else {
return false;
}
}
public function fopen($path, $mode) {
$path = $this->normalizePath($path);
if (strrpos($path, '.') !== false) {
$ext = substr($path, strrpos($path, '.'));
} else {
$ext = '';
}
switch ($mode) {
case 'r':
case 'rb':
$stat = $this->stat($path);
if (is_array($stat)) {
$filesize = $stat['size'] ?? 0;
// Reading 0 sized files is a waste of time
if ($filesize === 0) {
return fopen('php://memory', $mode);
}
try {
$handle = $this->objectStore->readObject($this->getURN($stat['fileid']));
if ($handle === false) {
return false; // keep backward compatibility
}
$streamStat = fstat($handle);
$actualSize = $streamStat['size'] ?? -1;
if ($actualSize > -1 && $actualSize !== $filesize) {
$this->getCache()->update((int)$stat['fileid'], ['size' => $actualSize]);
}
return $handle;
} catch (NotFoundException $e) {
$this->logger->logException($e, [
'app' => 'objectstore',
'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
]);
throw $e;
} catch (\Exception $ex) {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
]);
return false;
}
} else {
return false;
}
// no break
case 'w':
case 'wb':
case 'w+':
case 'wb+':
$dirName = dirname($path);
$parentExists = $this->is_dir($dirName);
if (!$parentExists) {
return false;
}
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
$handle = fopen($tmpFile, $mode);
return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
$this->writeBack($tmpFile, $path);
unlink($tmpFile);
});
case 'a':
case 'ab':
case 'r+':
case 'a+':
case 'x':
case 'x+':
case 'c':
case 'c+':
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source);
}
$handle = fopen($tmpFile, $mode);
return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
$this->writeBack($tmpFile, $path);
unlink($tmpFile);
});
}
return false;
}
public function file_exists($path) {
$path = $this->normalizePath($path);
return (bool)$this->stat($path);
}
public function rename($source, $target) {
$source = $this->normalizePath($source);
$target = $this->normalizePath($target);
$this->remove($target);
$this->getCache()->move($source, $target);
$this->touch(dirname($target));
return true;
}
public function getMimeType($path) {
$path = $this->normalizePath($path);
return parent::getMimeType($path);
}
public function touch($path, $mtime = null) {
if (is_null($mtime)) {
$mtime = time();
}
$path = $this->normalizePath($path);
$dirName = dirname($path);
$parentExists = $this->is_dir($dirName);
if (!$parentExists) {
return false;
}
$stat = $this->stat($path);
if (is_array($stat)) {
// update existing mtime in db
$stat['mtime'] = $mtime;
$this->getCache()->update($stat['fileid'], $stat);
} else {
try {
//create a empty file, need to have at least on char to make it
// work with all object storage implementations
$this->file_put_contents($path, ' ');
$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
$stat = [
'etag' => $this->getETag($path),
'mimetype' => $mimeType,
'size' => 0,
'mtime' => $mtime,
'storage_mtime' => $mtime,
'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
];
$this->getCache()->put($path, $stat);
} catch (\Exception $ex) {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not create object for ' . $path,
]);
throw $ex;
}
}
return true;
}
public function writeBack($tmpFile, $path) {
$size = filesize($tmpFile);
$this->writeStream($path, fopen($tmpFile, 'r'), $size);
}
/**
* external changes are not supported, exclusive access to the object storage is assumed
*
* @param string $path
* @param int $time
* @return false
*/
public function hasUpdated($path, $time) {
return false;
}
public function needsPartFile() {
return false;
}
public function file_put_contents($path, $data) {
$handle = $this->fopen($path, 'w+');
if (!$handle) {
return false;
}
$result = fwrite($handle, $data);
fclose($handle);
return $result;
}
public function writeStream(string $path, $stream, int $size = null): int {
$stat = $this->stat($path);
if (empty($stat)) {
// create new file
$stat = [
'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
];
}
// update stat with new data
$mTime = time();
$stat['size'] = (int)$size;
$stat['mtime'] = $mTime;
$stat['storage_mtime'] = $mTime;
$mimetypeDetector = \OC::$server->getMimeTypeDetector();
$mimetype = $mimetypeDetector->detectPath($path);
$stat['mimetype'] = $mimetype;
$stat['etag'] = $this->getETag($path);
$stat['checksum'] = '';
$exists = $this->getCache()->inCache($path);
$uploadPath = $exists ? $path : $path . '.part';
if ($exists) {
$fileId = $stat['fileid'];
} else {
$fileId = $this->getCache()->put($uploadPath, $stat);
}
$urn = $this->getURN($fileId);
try {
//upload to object storage
if ($size === null) {
$countStream = CountWrapper::wrap($stream, function ($writtenSize) use ($fileId, &$size) {
$this->getCache()->update($fileId, [
'size' => $writtenSize,
]);
$size = $writtenSize;
});
$this->objectStore->writeObject($urn, $countStream, $mimetype);
if (is_resource($countStream)) {
fclose($countStream);
}
$stat['size'] = $size;
} else {
$this->objectStore->writeObject($urn, $stream, $mimetype);
if (is_resource($stream)) {
fclose($stream);
}
}
} catch (\Exception $ex) {
if (!$exists) {
/*
* Only remove the entry if we are dealing with a new file.
* Else people lose access to existing files
*/
$this->getCache()->remove($uploadPath);
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not create object ' . $urn . ' for ' . $path,
]);
} else {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not update object ' . $urn . ' for ' . $path,
]);
}
throw $ex; // make this bubble up
}
if ($exists) {
// Always update the unencrypted size, for encryption the Encryption wrapper will update this afterwards anyways
$stat['unencrypted_size'] = $stat['size'];
$this->getCache()->update($fileId, $stat);
} else {
if (!$this->validateWrites || $this->objectStore->objectExists($urn)) {
$this->getCache()->move($uploadPath, $path);
} else {
$this->getCache()->remove($uploadPath);
throw new \Exception("Object not found after writing (urn: $urn, path: $path)", 404);
}
}
return $size;
}
public function getObjectStore(): IObjectStore {
return $this->objectStore;
}
public function copyFromStorage(
IStorage $sourceStorage,
$sourceInternalPath,
$targetInternalPath,
$preserveMtime = false
) {
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) {
/** @var ObjectStoreStorage $sourceStorage */
if ($sourceStorage->getObjectStore()->getStorageId() === $this->getObjectStore()->getStorageId()) {
/** @var CacheEntry $sourceEntry */
$sourceEntry = $sourceStorage->getCache()->get($sourceInternalPath);
$sourceEntryData = $sourceEntry->getData();
// $sourceEntry['permissions'] here is the permissions from the jailed storage for the current
// user. Instead we use $sourceEntryData['scan_permissions'] that are the permissions from the
// unjailed storage.
if (is_array($sourceEntryData) && array_key_exists('scan_permissions', $sourceEntryData)) {
$sourceEntry['permissions'] = $sourceEntryData['scan_permissions'];
}
$this->copyInner($sourceStorage->getCache(), $sourceEntry, $targetInternalPath);
return true;
}
}
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
public function copy($source, $target) {
$source = $this->normalizePath($source);
$target = $this->normalizePath($target);
$cache = $this->getCache();
$sourceEntry = $cache->get($source);
if (!$sourceEntry) {
throw new NotFoundException('Source object not found');
}
$this->copyInner($cache, $sourceEntry, $target);
return true;
}
private function copyInner(ICache $sourceCache, ICacheEntry $sourceEntry, string $to) {
$cache = $this->getCache();
if ($sourceEntry->getMimeType() === FileInfo::MIMETYPE_FOLDER) {
if ($cache->inCache($to)) {
$cache->remove($to);
}
$this->mkdir($to);
foreach ($sourceCache->getFolderContentsById($sourceEntry->getId()) as $child) {
$this->copyInner($sourceCache, $child, $to . '/' . $child->getName());
}
} else {
$this->copyFile($sourceEntry, $to);
}
}
private function copyFile(ICacheEntry $sourceEntry, string $to) {
$cache = $this->getCache();
$sourceUrn = $this->getURN($sourceEntry->getId());
if (!$cache instanceof Cache) {
throw new \Exception("Invalid source cache for object store copy");
}
$targetId = $cache->copyFromCache($cache, $sourceEntry, $to);
$targetUrn = $this->getURN($targetId);
try {
$this->objectStore->copyObject($sourceUrn, $targetUrn);
if ($this->handleCopiesAsOwned) {
// Copied the file thus we gain all permissions as we are the owner now ! warning while this aligns with local storage it should not be used and instead fix local storage !
$cache->update($targetId, ['permissions' => \OCP\Constants::PERMISSION_ALL]);
}
} catch (\Exception $e) {
$cache->remove($to);
throw $e;
}
}
public function startChunkedWrite(string $targetPath): string {
if (!$this->objectStore instanceof IObjectStoreMultiPartUpload) {
throw new GenericFileException('Object store does not support multipart upload');
}
$cacheEntry = $this->getCache()->get($targetPath);
$urn = $this->getURN($cacheEntry->getId());
return $this->objectStore->initiateMultipartUpload($urn);
}
/**
*
* @throws GenericFileException
*/
public function putChunkedWritePart(
string $targetPath,
string $writeToken,
string $chunkId,
$data,
$size = null
): ?array {
if (!$this->objectStore instanceof IObjectStoreMultiPartUpload) {
throw new GenericFileException('Object store does not support multipart upload');
}
$cacheEntry = $this->getCache()->get($targetPath);
$urn = $this->getURN($cacheEntry->getId());
$result = $this->objectStore->uploadMultipartPart($urn, $writeToken, (int)$chunkId, $data, $size);
$parts[$chunkId] = [
'PartNumber' => $chunkId,
'ETag' => trim($result->get('ETag'), '"'),
];
return $parts[$chunkId];
}
public function completeChunkedWrite(string $targetPath, string $writeToken): int {
if (!$this->objectStore instanceof IObjectStoreMultiPartUpload) {
throw new GenericFileException('Object store does not support multipart upload');
}
$cacheEntry = $this->getCache()->get($targetPath);
$urn = $this->getURN($cacheEntry->getId());
$parts = $this->objectStore->getMultipartUploads($urn, $writeToken);
$sortedParts = array_values($parts);
sort($sortedParts);
try {
$size = $this->objectStore->completeMultipartUpload($urn, $writeToken, $sortedParts);
$stat = $this->stat($targetPath);
$mtime = time();
if (is_array($stat)) {
$stat['size'] = $size;
$stat['mtime'] = $mtime;
$stat['mimetype'] = $this->getMimeType($targetPath);
$this->getCache()->update($stat['fileid'], $stat);
}
} catch (S3MultipartUploadException|S3Exception $e) {
$this->objectStore->abortMultipartUpload($urn, $writeToken);
$this->logger->logException($e, [
'app' => 'objectstore',
'message' => 'Could not compete multipart upload ' . $urn . ' with uploadId ' . $writeToken,
]);
throw new GenericFileException('Could not write chunked file');
}
return $size;
}
public function cancelChunkedWrite(string $targetPath, string $writeToken): void {
if (!$this->objectStore instanceof IObjectStoreMultiPartUpload) {
throw new GenericFileException('Object store does not support multipart upload');
}
$cacheEntry = $this->getCache()->get($targetPath);
$urn = $this->getURN($cacheEntry->getId());
$this->objectStore->abortMultipartUpload($urn, $writeToken);
}
}
@@ -0,0 +1,113 @@
<?php
/**
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
*
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use Aws\Result;
use Exception;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
class S3 implements IObjectStore, IObjectStoreMultiPartUpload {
use S3ConnectionTrait;
use S3ObjectTrait;
public function __construct($parameters) {
$parameters['primary_storage'] = true;
$this->parseParams($parameters);
}
/**
* @return string the container or bucket name where objects are stored
* @since 7.0.0
*/
public function getStorageId() {
return $this->id;
}
public function initiateMultipartUpload(string $urn): string {
$upload = $this->getConnection()->createMultipartUpload([
'Bucket' => $this->bucket,
'Key' => $urn,
] + $this->getSSECParameters());
$uploadId = $upload->get('UploadId');
if ($uploadId === null) {
throw new Exception('No upload id returned');
}
return (string)$uploadId;
}
public function uploadMultipartPart(string $urn, string $uploadId, int $partId, $stream, $size): Result {
return $this->getConnection()->uploadPart([
'Body' => $stream,
'Bucket' => $this->bucket,
'Key' => $urn,
'ContentLength' => $size,
'PartNumber' => $partId,
'UploadId' => $uploadId,
] + $this->getSSECParameters());
}
public function getMultipartUploads(string $urn, string $uploadId): array {
$parts = [];
$isTruncated = true;
$partNumberMarker = 0;
while ($isTruncated) {
$result = $this->getConnection()->listParts([
'Bucket' => $this->bucket,
'Key' => $urn,
'UploadId' => $uploadId,
'MaxParts' => 1000,
'PartNumberMarker' => $partNumberMarker
] + $this->getSSECParameters());
$parts = array_merge($parts, $result->get('Parts') ?? []);
$isTruncated = $result->get('IsTruncated');
$partNumberMarker = $result->get('NextPartNumberMarker');
}
return $parts;
}
public function completeMultipartUpload(string $urn, string $uploadId, array $result): int {
$this->getConnection()->completeMultipartUpload([
'Bucket' => $this->bucket,
'Key' => $urn,
'UploadId' => $uploadId,
'MultipartUpload' => ['Parts' => $result],
] + $this->getSSECParameters());
$stat = $this->getConnection()->headObject([
'Bucket' => $this->bucket,
'Key' => $urn,
] + $this->getSSECParameters());
return (int)$stat->get('ContentLength');
}
public function abortMultipartUpload($urn, $uploadId): void {
$this->getConnection()->abortMultipartUpload([
'Bucket' => $this->bucket,
'Key' => $urn,
'UploadId' => $uploadId
]);
}
}
@@ -0,0 +1,271 @@
<?php
/**
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Florent <florent@coppint.com>
* @author James Letendre <James.Letendre@gmail.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author S. Cat <33800996+sparrowjack63@users.noreply.github.com>
* @author Stephen Cuppett <steve@cuppett.com>
* @author Jasper Weyne <jasperweyne@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use Aws\ClientResolver;
use Aws\Credentials\CredentialProvider;
use Aws\Credentials\Credentials;
use Aws\Exception\CredentialsException;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\RejectedPromise;
use OCP\ICertificateManager;
use Psr\Log\LoggerInterface;
trait S3ConnectionTrait {
/** @var array */
protected $params;
/** @var S3Client */
protected $connection;
/** @var string */
protected $id;
/** @var string */
protected $bucket;
/** @var int */
protected $timeout;
/** @var string */
protected $proxy;
/** @var string */
protected $storageClass;
/** @var int */
protected $uploadPartSize;
/** @var int */
private $putSizeLimit;
/** @var int */
private $copySizeLimit;
private bool $useMultipartCopy = true;
protected $test;
protected function parseParams($params) {
if (empty($params['bucket'])) {
throw new \Exception("Bucket has to be configured.");
}
$this->id = 'amazon::' . $params['bucket'];
$this->test = isset($params['test']);
$this->bucket = $params['bucket'];
$this->proxy = $params['proxy'] ?? false;
$this->timeout = $params['timeout'] ?? 15;
$this->storageClass = !empty($params['storageClass']) ? $params['storageClass'] : 'STANDARD';
$this->uploadPartSize = $params['uploadPartSize'] ?? 524288000;
$this->putSizeLimit = $params['putSizeLimit'] ?? 104857600;
$this->copySizeLimit = $params['copySizeLimit'] ?? 5242880000;
$this->useMultipartCopy = (bool)($params['useMultipartCopy'] ?? true);
$params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
$params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
if (!isset($params['port']) || $params['port'] === '') {
$params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
}
$params['verify_bucket_exists'] = $params['verify_bucket_exists'] ?? true;
$this->params = $params;
}
public function getBucket() {
return $this->bucket;
}
public function getProxy() {
return $this->proxy;
}
/**
* Returns the connection
*
* @return S3Client connected client
* @throws \Exception if connection could not be made
*/
public function getConnection() {
if (!is_null($this->connection)) {
return $this->connection;
}
$scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
$base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
// Adding explicit credential provider to the beginning chain.
// Including default credential provider (skipping AWS shared config files).
$provider = CredentialProvider::memoize(
CredentialProvider::chain(
$this->paramCredentialProvider(),
CredentialProvider::defaultProvider(['use_aws_shared_config_files' => false])
)
);
$options = [
'version' => $this->params['version'] ?? 'latest',
'credentials' => $provider,
'endpoint' => $base_url,
'region' => $this->params['region'],
'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()),
'csm' => false,
'use_arn_region' => false,
'http' => ['verify' => $this->getCertificateBundlePath()],
'use_aws_shared_config_files' => false,
];
if ($this->getProxy()) {
$options['http']['proxy'] = $this->getProxy();
}
if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
$options['signature_version'] = 'v2';
}
$this->connection = new S3Client($options);
if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
$logger = \OC::$server->get(LoggerInterface::class);
$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
['app' => 'objectstore']);
}
if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) {
$logger = \OC::$server->get(LoggerInterface::class);
try {
$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
}
$this->connection->createBucket(['Bucket' => $this->bucket]);
$this->testTimeout();
} catch (S3Exception $e) {
$logger->debug('Invalid remote storage.', [
'exception' => $e,
'app' => 'objectstore',
]);
throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
}
}
// google cloud's s3 compatibility doesn't like the EncodingType parameter
if (strpos($base_url, 'storage.googleapis.com')) {
$this->connection->getHandlerList()->remove('s3.auto_encode');
}
return $this->connection;
}
/**
* when running the tests wait to let the buckets catch up
*/
private function testTimeout() {
if ($this->test) {
sleep($this->timeout);
}
}
public static function legacySignatureProvider($version, $service, $region) {
switch ($version) {
case 'v2':
case 's3':
return new S3Signature();
default:
return null;
}
}
/**
* This function creates a credential provider based on user parameter file
*/
protected function paramCredentialProvider(): callable {
return function () {
$key = empty($this->params['key']) ? null : $this->params['key'];
$secret = empty($this->params['secret']) ? null : $this->params['secret'];
if ($key && $secret) {
return Promise\promise_for(
new Credentials($key, $secret)
);
}
$msg = 'Could not find parameters set for credentials in config file.';
return new RejectedPromise(new CredentialsException($msg));
};
}
protected function getCertificateBundlePath(): ?string {
if ((int)($this->params['use_nextcloud_bundle'] ?? "0")) {
// since we store the certificate bundles on the primary storage, we can't get the bundle while setting up the primary storage
if (!isset($this->params['primary_storage'])) {
/** @var ICertificateManager $certManager */
$certManager = \OC::$server->get(ICertificateManager::class);
return $certManager->getAbsoluteBundlePath();
} else {
return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
}
} else {
return null;
}
}
protected function getSSECKey(): ?string {
if (isset($this->params['sse_c_key'])) {
return $this->params['sse_c_key'];
}
return null;
}
protected function getSSECParameters(bool $copy = false): array {
$key = $this->getSSECKey();
if ($key === null) {
return [];
}
$rawKey = base64_decode($key);
if ($copy) {
return [
'CopySourceSSECustomerAlgorithm' => 'AES256',
'CopySourceSSECustomerKey' => $rawKey,
'CopySourceSSECustomerKeyMD5' => md5($rawKey, true)
];
}
return [
'SSECustomerAlgorithm' => 'AES256',
'SSECustomerKey' => $rawKey,
'SSECustomerKeyMD5' => md5($rawKey, true)
];
}
}
@@ -0,0 +1,219 @@
<?php
/**
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Florent <florent@coppint.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use Aws\S3\Exception\S3MultipartUploadException;
use Aws\S3\MultipartCopy;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Utils;
use OC\Files\Stream\SeekableHttpStream;
use Psr\Http\Message\StreamInterface;
trait S3ObjectTrait {
/**
* Returns the connection
*
* @return S3Client connected client
* @throws \Exception if connection could not be made
*/
abstract protected function getConnection();
abstract protected function getCertificateBundlePath(): ?string;
abstract protected function getSSECParameters(bool $copy = false): array;
/**
* @param string $urn the unified resource name used to identify the object
*
* @return resource stream with the read data
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
*/
public function readObject($urn) {
$fh = SeekableHttpStream::open(function ($range) use ($urn) {
$command = $this->getConnection()->getCommand('GetObject', [
'Bucket' => $this->bucket,
'Key' => $urn,
'Range' => 'bytes=' . $range,
] + $this->getSSECParameters());
$request = \Aws\serialize($command);
$headers = [];
foreach ($request->getHeaders() as $key => $values) {
foreach ($values as $value) {
$headers[] = "$key: $value";
}
}
$opts = [
'http' => [
'protocol_version' => $request->getProtocolVersion(),
'header' => $headers,
]
];
$bundle = $this->getCertificateBundlePath();
if ($bundle) {
$opts['ssl'] = [
'cafile' => $bundle
];
}
if ($this->getProxy()) {
$opts['http']['proxy'] = $this->getProxy();
$opts['http']['request_fulluri'] = true;
}
$context = stream_context_create($opts);
return fopen($request->getUri(), 'r', false, $context);
});
if (!$fh) {
throw new \Exception("Failed to read object $urn");
}
return $fh;
}
/**
* Single object put helper
*
* @param string $urn the unified resource name used to identify the object
* @param StreamInterface $stream stream with the data to write
* @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
* @throws \Exception when something goes wrong, message will be logged
*/
protected function writeSingle(string $urn, StreamInterface $stream, string $mimetype = null): void {
$this->getConnection()->putObject([
'Bucket' => $this->bucket,
'Key' => $urn,
'Body' => $stream,
'ACL' => 'private',
'ContentType' => $mimetype,
'StorageClass' => $this->storageClass,
] + $this->getSSECParameters());
}
/**
* Multipart upload helper that tries to avoid orphaned fragments in S3
*
* @param string $urn the unified resource name used to identify the object
* @param StreamInterface $stream stream with the data to write
* @param string|null $mimetype the mimetype to set for the remove object
* @throws \Exception when something goes wrong, message will be logged
*/
protected function writeMultiPart(string $urn, StreamInterface $stream, string $mimetype = null): void {
$uploader = new MultipartUploader($this->getConnection(), $stream, [
'bucket' => $this->bucket,
'key' => $urn,
'part_size' => $this->uploadPartSize,
'params' => [
'ContentType' => $mimetype,
'StorageClass' => $this->storageClass,
] + $this->getSSECParameters(),
]);
try {
$uploader->upload();
} catch (S3MultipartUploadException $e) {
// if anything goes wrong with multipart, make sure that you don´t poison and
// slow down s3 bucket with orphaned fragments
$uploadInfo = $e->getState()->getId();
if ($e->getState()->isInitiated() && (array_key_exists('UploadId', $uploadInfo))) {
$this->getConnection()->abortMultipartUpload($uploadInfo);
}
throw new \OCA\DAV\Connector\Sabre\Exception\BadGateway("Error while uploading to S3 bucket", 0, $e);
}
}
/**
* @param string $urn the unified resource name used to identify the object
* @param resource $stream stream with the data to write
* @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
*/
public function writeObject($urn, $stream, string $mimetype = null) {
$psrStream = Utils::streamFor($stream);
// ($psrStream->isSeekable() && $psrStream->getSize() !== null) evaluates to true for a On-Seekable stream
// so the optimisation does not apply
$buffer = new Psr7\Stream(fopen("php://memory", 'rwb+'));
Utils::copyToStream($psrStream, $buffer, $this->putSizeLimit);
$buffer->seek(0);
if ($buffer->getSize() < $this->putSizeLimit) {
// buffer is fully seekable, so use it directly for the small upload
$this->writeSingle($urn, $buffer, $mimetype);
} else {
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
$this->writeMultiPart($urn, $loadStream, $mimetype);
}
}
/**
* @param string $urn the unified resource name used to identify the object
* @return void
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
*/
public function deleteObject($urn) {
$this->getConnection()->deleteObject([
'Bucket' => $this->bucket,
'Key' => $urn,
]);
}
public function objectExists($urn) {
return $this->getConnection()->doesObjectExist($this->bucket, $urn, $this->getSSECParameters());
}
public function copyObject($from, $to, array $options = []) {
$sourceMetadata = $this->getConnection()->headObject([
'Bucket' => $this->getBucket(),
'Key' => $from,
] + $this->getSSECParameters());
$size = (int)($sourceMetadata->get('Size') ?? $sourceMetadata->get('ContentLength'));
if ($this->useMultipartCopy && $size > $this->copySizeLimit) {
$copy = new MultipartCopy($this->getConnection(), [
"source_bucket" => $this->getBucket(),
"source_key" => $from
], array_merge([
"bucket" => $this->getBucket(),
"key" => $to,
"acl" => "private",
"params" => $this->getSSECParameters() + $this->getSSECParameters(true),
"source_metadata" => $sourceMetadata
], $options));
$copy->copy();
} else {
$this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to, 'private', array_merge([
'params' => $this->getSSECParameters() + $this->getSSECParameters(true)
], $options));
}
}
}
@@ -0,0 +1,222 @@
<?php
/**
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Daniel Kesselberg <mail@danielkesselberg.de>
* @author Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use Aws\Credentials\CredentialsInterface;
use Aws\S3\S3Client;
use Aws\S3\S3UriParser;
use Aws\Signature\SignatureInterface;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* Legacy Amazon S3 signature implementation
*/
class S3Signature implements SignatureInterface {
/** @var array Query string values that must be signed */
private $signableQueryString = [
'acl', 'cors', 'delete', 'lifecycle', 'location', 'logging',
'notification', 'partNumber', 'policy', 'requestPayment',
'response-cache-control', 'response-content-disposition',
'response-content-encoding', 'response-content-language',
'response-content-type', 'response-expires', 'restore', 'tagging',
'torrent', 'uploadId', 'uploads', 'versionId', 'versioning',
'versions', 'website'
];
/** @var array Sorted headers that must be signed */
private $signableHeaders = ['Content-MD5', 'Content-Type'];
/** @var \Aws\S3\S3UriParser S3 URI parser */
private $parser;
public function __construct() {
$this->parser = new S3UriParser();
// Ensure that the signable query string parameters are sorted
sort($this->signableQueryString);
}
public function signRequest(
RequestInterface $request,
CredentialsInterface $credentials
) {
$request = $this->prepareRequest($request, $credentials);
$stringToSign = $this->createCanonicalizedString($request);
$auth = 'AWS '
. $credentials->getAccessKeyId() . ':'
. $this->signString($stringToSign, $credentials);
return $request->withHeader('Authorization', $auth);
}
public function presign(
RequestInterface $request,
CredentialsInterface $credentials,
$expires,
array $options = []
) {
$query = [];
// URL encoding already occurs in the URI template expansion. Undo that
// and encode using the same encoding as GET object, PUT object, etc.
$uri = $request->getUri();
$path = S3Client::encodeKey(rawurldecode($uri->getPath()));
$request = $request->withUri($uri->withPath($path));
// Make sure to handle temporary credentials
if ($token = $credentials->getSecurityToken()) {
$request = $request->withHeader('X-Amz-Security-Token', $token);
$query['X-Amz-Security-Token'] = $token;
}
if ($expires instanceof \DateTime) {
$expires = $expires->getTimestamp();
} elseif (!is_numeric($expires)) {
$expires = strtotime($expires);
}
// Set query params required for pre-signed URLs
$query['AWSAccessKeyId'] = $credentials->getAccessKeyId();
$query['Expires'] = $expires;
$query['Signature'] = $this->signString(
$this->createCanonicalizedString($request, $expires),
$credentials
);
// Move X-Amz-* headers to the query string
foreach ($request->getHeaders() as $name => $header) {
$name = strtolower($name);
if (str_starts_with($name, 'x-amz-')) {
$query[$name] = implode(',', $header);
}
}
$queryString = http_build_query($query, null, '&', PHP_QUERY_RFC3986);
return $request->withUri($request->getUri()->withQuery($queryString));
}
/**
* @param RequestInterface $request
* @param CredentialsInterface $creds
*
* @return RequestInterface
*/
private function prepareRequest(
RequestInterface $request,
CredentialsInterface $creds
) {
$modify = [
'remove_headers' => ['X-Amz-Date'],
'set_headers' => ['Date' => gmdate(\DateTimeInterface::RFC2822)]
];
// Add the security token header if one is being used by the credentials
if ($token = $creds->getSecurityToken()) {
$modify['set_headers']['X-Amz-Security-Token'] = $token;
}
return Psr7\Utils::modifyRequest($request, $modify);
}
private function signString($string, CredentialsInterface $credentials) {
return base64_encode(
hash_hmac('sha1', $string, $credentials->getSecretKey(), true)
);
}
private function createCanonicalizedString(
RequestInterface $request,
$expires = null
) {
$buffer = $request->getMethod() . "\n";
// Add the interesting headers
foreach ($this->signableHeaders as $header) {
$buffer .= $request->getHeaderLine($header) . "\n";
}
$date = $expires ?: $request->getHeaderLine('date');
$buffer .= "{$date}\n"
. $this->createCanonicalizedAmzHeaders($request)
. $this->createCanonicalizedResource($request);
return $buffer;
}
private function createCanonicalizedAmzHeaders(RequestInterface $request) {
$headers = [];
foreach ($request->getHeaders() as $name => $header) {
$name = strtolower($name);
if (str_starts_with($name, 'x-amz-')) {
$value = implode(',', $header);
if (strlen($value) > 0) {
$headers[$name] = $name . ':' . $value;
}
}
}
if (!$headers) {
return '';
}
ksort($headers);
return implode("\n", $headers) . "\n";
}
private function createCanonicalizedResource(RequestInterface $request) {
$data = $this->parser->parse($request->getUri());
$buffer = '/';
if ($data['bucket']) {
$buffer .= $data['bucket'];
if (!empty($data['key']) || !$data['path_style']) {
$buffer .= '/' . $data['key'];
}
}
// Add sub resource parameters if present.
$query = $request->getUri()->getQuery();
if ($query) {
$params = Psr7\Query::parse($query);
$first = true;
foreach ($this->signableQueryString as $key) {
if (array_key_exists($key, $params)) {
$value = $params[$key];
$buffer .= $first ? '?' : '&';
$first = false;
$buffer .= $key;
// Don't add values for empty sub-resources
if (strlen($value)) {
$buffer .= "={$value}";
}
}
}
}
return $buffer;
}
}
@@ -0,0 +1,94 @@
<?php
/**
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\Files\Storage\IStorage;
use function is_resource;
/**
* Object store that wraps a storage backend, mostly for testing purposes
*/
class StorageObjectStore implements IObjectStore {
/** @var IStorage */
private $storage;
/**
* @param IStorage $storage
*/
public function __construct(IStorage $storage) {
$this->storage = $storage;
}
/**
* @return string the container or bucket name where objects are stored
* @since 7.0.0
*/
public function getStorageId() {
$this->storage->getId();
}
/**
* @param string $urn the unified resource name used to identify the object
* @return resource stream with the read data
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
*/
public function readObject($urn) {
$handle = $this->storage->fopen($urn, 'r');
if (is_resource($handle)) {
return $handle;
}
throw new \Exception();
}
public function writeObject($urn, $stream, string $mimetype = null) {
$handle = $this->storage->fopen($urn, 'w');
if ($handle) {
stream_copy_to_stream($stream, $handle);
fclose($handle);
} else {
throw new \Exception();
}
}
/**
* @param string $urn the unified resource name used to identify the object
* @return void
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
*/
public function deleteObject($urn) {
$this->storage->unlink($urn);
}
public function objectExists($urn) {
return $this->storage->file_exists($urn);
}
public function copyObject($from, $to) {
$this->storage->copy($from, $to);
}
}
@@ -0,0 +1,155 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Adrian Brzezinski <adrian.brzezinski@eo.pl>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\ObjectStore;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Utils;
use Icewind\Streams\RetryWrapper;
use OCP\Files\NotFoundException;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\Files\StorageAuthException;
use Psr\Log\LoggerInterface;
const SWIFT_SEGMENT_SIZE = 1073741824; // 1GB
class Swift implements IObjectStore {
/**
* @var array
*/
private $params;
/** @var SwiftFactory */
private $swiftFactory;
public function __construct($params, SwiftFactory $connectionFactory = null) {
$this->swiftFactory = $connectionFactory ?: new SwiftFactory(
\OC::$server->getMemCacheFactory()->createDistributed('swift::'),
$params,
\OC::$server->get(LoggerInterface::class)
);
$this->params = $params;
}
/**
* @return \OpenStack\ObjectStore\v1\Models\Container
* @throws StorageAuthException
* @throws \OCP\Files\StorageNotAvailableException
*/
private function getContainer() {
return $this->swiftFactory->getContainer();
}
/**
* @return string the container name where objects are stored
*/
public function getStorageId() {
if (isset($this->params['bucket'])) {
return $this->params['bucket'];
}
return $this->params['container'];
}
public function writeObject($urn, $stream, string $mimetype = null) {
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile('swiftwrite');
file_put_contents($tmpFile, $stream);
$handle = fopen($tmpFile, 'rb');
if (filesize($tmpFile) < SWIFT_SEGMENT_SIZE) {
$this->getContainer()->createObject([
'name' => $urn,
'stream' => Utils::streamFor($handle),
'contentType' => $mimetype,
]);
} else {
$this->getContainer()->createLargeObject([
'name' => $urn,
'stream' => Utils::streamFor($handle),
'segmentSize' => SWIFT_SEGMENT_SIZE,
'contentType' => $mimetype,
]);
}
}
/**
* @param string $urn the unified resource name used to identify the object
* @return resource stream with the read data
* @throws \Exception from openstack or GuzzleHttp libs when something goes wrong
* @throws NotFoundException if file does not exist
*/
public function readObject($urn) {
try {
$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) {
throw new NotFoundException("object $urn not found in object store");
} else {
throw $e;
}
}
return RetryWrapper::wrap($response->getBody()->detach());
}
/**
* @param string $urn Unified Resource Name
* @return void
* @throws \Exception from openstack lib when something goes wrong
*/
public function deleteObject($urn) {
$this->getContainer()->getObject($urn)->delete();
}
/**
* @return void
* @throws \Exception from openstack lib when something goes wrong
*/
public function deleteContainer() {
$this->getContainer()->delete();
}
public function objectExists($urn) {
return $this->getContainer()->objectExists($urn);
}
public function copyObject($from, $to) {
$this->getContainer()->getObject($from)->copy([
'destination' => $this->getContainer()->name . '/' . $to
]);
}
}
@@ -0,0 +1,286 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
*
* @author Adrian Brzezinski <adrian.brzezinski@eo.pl>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Julien Lutran <julien.lutran@corp.ovh.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Volker <skydiablo@gmx.net>
* @author William Pain <pain.william@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use OCP\Files\StorageAuthException;
use OCP\Files\StorageNotAvailableException;
use OCP\ICache;
use OpenStack\Common\Auth\Token;
use OpenStack\Common\Error\BadResponseError;
use OpenStack\Common\Transport\Utils as TransportUtils;
use OpenStack\Identity\v2\Models\Catalog;
use OpenStack\Identity\v2\Service as IdentityV2Service;
use OpenStack\Identity\v3\Service as IdentityV3Service;
use OpenStack\ObjectStore\v1\Models\Container;
use OpenStack\OpenStack;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
class SwiftFactory {
private $cache;
private $params;
/** @var Container|null */
private $container = null;
private LoggerInterface $logger;
public const DEFAULT_OPTIONS = [
'autocreate' => false,
'urlType' => 'publicURL',
'catalogName' => 'swift',
'catalogType' => 'object-store'
];
public function __construct(ICache $cache, array $params, LoggerInterface $logger) {
$this->cache = $cache;
$this->params = $params;
$this->logger = $logger;
}
/**
* Gets currently cached token id
*
* @return string
* @throws StorageAuthException
*/
public function getCachedTokenId() {
if (!isset($this->params['cachedToken'])) {
throw new StorageAuthException('Unauthenticated ObjectStore connection');
}
// Is it V2 token?
if (isset($this->params['cachedToken']['token'])) {
return $this->params['cachedToken']['token']['id'];
}
return $this->params['cachedToken']['id'];
}
private function getCachedToken(string $cacheKey) {
$cachedTokenString = $this->cache->get($cacheKey . '/token');
if ($cachedTokenString) {
return json_decode($cachedTokenString, true);
} else {
return null;
}
}
private function cacheToken(Token $token, string $serviceUrl, string $cacheKey) {
if ($token instanceof \OpenStack\Identity\v3\Models\Token) {
// for v3 the catalog is cached as part of the token, so no need to cache $serviceUrl separately
$value = $token->export();
} else {
/** @var \OpenStack\Identity\v2\Models\Token $token */
$value = [
'serviceUrl' => $serviceUrl,
'token' => [
'issued_at' => $token->issuedAt->format('c'),
'expires' => $token->expires->format('c'),
'id' => $token->id,
'tenant' => $token->tenant
]
];
}
$this->params['cachedToken'] = $value;
$this->cache->set($cacheKey . '/token', json_encode($value));
}
/**
* @return OpenStack
* @throws StorageAuthException
*/
private function getClient() {
if (isset($this->params['bucket'])) {
$this->params['container'] = $this->params['bucket'];
}
if (!isset($this->params['container'])) {
$this->params['container'] = 'nextcloud';
}
if (isset($this->params['user']) && is_array($this->params['user'])) {
$userName = $this->params['user']['name'];
} else {
if (!isset($this->params['username']) && isset($this->params['user'])) {
$this->params['username'] = $this->params['user'];
}
$userName = $this->params['username'];
}
if (!isset($this->params['tenantName']) && isset($this->params['tenant'])) {
$this->params['tenantName'] = $this->params['tenant'];
}
if (isset($this->params['domain'])) {
$this->params['scope']['project']['name'] = $this->params['tenant'];
$this->params['scope']['project']['domain']['name'] = $this->params['domain'];
}
$this->params = array_merge(self::DEFAULT_OPTIONS, $this->params);
$cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container'];
$token = $this->getCachedToken($cacheKey);
$this->params['cachedToken'] = $token;
$httpClient = new Client([
'base_uri' => TransportUtils::normalizeUrl($this->params['url']),
'handler' => HandlerStack::create()
]);
if (isset($this->params['user']) && is_array($this->params['user']) && isset($this->params['user']['name'])) {
if (!isset($this->params['scope'])) {
throw new StorageAuthException('Scope has to be defined for V3 requests');
}
return $this->auth(IdentityV3Service::factory($httpClient), $cacheKey);
} else {
return $this->auth(SwiftV2CachingAuthService::factory($httpClient), $cacheKey);
}
}
/**
* @param IdentityV2Service|IdentityV3Service $authService
* @param string $cacheKey
* @return OpenStack
* @throws StorageAuthException
*/
private function auth($authService, string $cacheKey) {
$this->params['identityService'] = $authService;
$this->params['authUrl'] = $this->params['url'];
$cachedToken = $this->params['cachedToken'];
$hasValidCachedToken = false;
if (\is_array($cachedToken)) {
if ($authService instanceof IdentityV3Service) {
$token = $authService->generateTokenFromCache($cachedToken);
if (\is_null($token->catalog)) {
$this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken));
} elseif ($token->hasExpired()) {
$this->logger->debug('Cached token for swift expired');
} else {
$hasValidCachedToken = true;
}
} else {
try {
/** @var \OpenStack\Identity\v2\Models\Token $token */
$token = $authService->model(\OpenStack\Identity\v2\Models\Token::class, $cachedToken['token']);
$now = new \DateTimeImmutable("now");
if ($token->expires > $now) {
$hasValidCachedToken = true;
$this->params['v2cachedToken'] = $token;
$this->params['v2serviceUrl'] = $cachedToken['serviceUrl'];
} else {
$this->logger->debug('Cached token for swift expired');
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
}
if (!$hasValidCachedToken) {
unset($this->params['cachedToken']);
try {
[$token, $serviceUrl] = $authService->authenticate($this->params);
$this->cacheToken($token, $serviceUrl, $cacheKey);
} catch (ConnectException $e) {
throw new StorageAuthException('Failed to connect to keystone, verify the keystone url', $e);
} catch (ClientException $e) {
$statusCode = $e->getResponse()->getStatusCode();
if ($statusCode === 404) {
throw new StorageAuthException('Keystone not found, verify the keystone url', $e);
} elseif ($statusCode === 412) {
throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
} elseif ($statusCode === 401) {
throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
} else {
throw new StorageAuthException('Unknown error', $e);
}
} catch (RequestException $e) {
throw new StorageAuthException('Connection reset while connecting to keystone, verify the keystone url', $e);
}
}
$client = new OpenStack($this->params);
return $client;
}
/**
* @return \OpenStack\ObjectStore\v1\Models\Container
* @throws StorageAuthException
* @throws StorageNotAvailableException
*/
public function getContainer() {
if (is_null($this->container)) {
$this->container = $this->createContainer();
}
return $this->container;
}
/**
* @return \OpenStack\ObjectStore\v1\Models\Container
* @throws StorageAuthException
* @throws StorageNotAvailableException
*/
private function createContainer() {
$client = $this->getClient();
$objectStoreService = $client->objectStoreV1();
$autoCreate = isset($this->params['autocreate']) && $this->params['autocreate'] === true;
try {
$container = $objectStoreService->getContainer($this->params['container']);
if ($autoCreate) {
$container->getMetadata();
}
return $container;
} catch (BadResponseError $ex) {
// if the container does not exist and autocreate is true try to create the container on the fly
if ($ex->getResponse()->getStatusCode() === 404 && $autoCreate) {
return $objectStoreService->createContainer([
'name' => $this->params['container']
]);
} else {
throw new StorageNotAvailableException('Invalid response while trying to get container info', StorageNotAvailableException::STATUS_ERROR, $ex);
}
} catch (ConnectException $e) {
/** @var RequestInterface $request */
$request = $e->getRequest();
$host = $request->getUri()->getHost() . ':' . $request->getUri()->getPort();
$this->logger->error("Can't connect to object storage server at $host", ['exception' => $e]);
throw new StorageNotAvailableException("Can't connect to object storage server at $host", StorageNotAvailableException::STATUS_ERROR, $e);
}
}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
*
* @author Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Files\ObjectStore;
use OpenStack\Identity\v2\Service;
class SwiftV2CachingAuthService extends Service {
public function authenticate(array $options = []): array {
if (!empty($options['v2cachedToken'])) {
return [$options['v2cachedToken'], $options['v2serviceUrl']];
} else {
return parent::authenticate($options);
}
}
}