Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stream download release #618

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 81 additions & 47 deletions lib/s3storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Aws\S3\Exception\S3Exception;
use Aws\S3\ObjectUploader;
use Aws\S3\S3Client;
use GuzzleHttp\Handler\StreamHandler;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\Middleware;
use OC\ServiceUnavailableException;
Expand All @@ -49,6 +50,9 @@ class S3Storage implements IObjectStore, IVersionedObjectStorage {
*/
private $connection;

/** @var S3Client|null */
private $downConnection;

/**
* @var array
*/
Expand Down Expand Up @@ -88,57 +92,20 @@ protected function init(): void {
}
$config = $this->params['options'];
if ($useGuzzle5) {
/*
* Note: phan runs in CI with the latest core, which has Guzzle7 or later.
* So various things that phan reports for this Guzzle5 code have to be suppressed.
*/
/* @phan-suppress-next-line PhanUndeclaredClassMethod */
$client = new \GuzzleHttp\Client(['handler' => new \GuzzleHttp\Ring\Client\CurlMultiHandler()]);
/* @phan-suppress-next-line PhanDeprecatedFunction */
$emitter = $client->getEmitter();
/* @phan-suppress-next-line PhanUndeclaredTypeParameter */
$emitter->on('before', static function (\GuzzleHttp\Event\BeforeEvent $event) {
/* @phan-suppress-next-line PhanUndeclaredClassMethod */
$request = $event->getRequest();
if ($request->getMethod() !== 'PUT') {
return;
}
$body = $request->getBody();
if ($body !== null && $body->getSize() !== 0) {
return;
}
if ($request->hasHeader('Content-Length')) {
return;
}
// force content length header on empty body
$request->setHeader('Content-Length', '0');
});
$h = new \Aws\Handler\GuzzleV5\GuzzleHandler($client);
$h = $this->getHandlerV5(false); // curlMultiHandler
$dh = $this->getHandlerV5(true); // streamHandler for downloads
} else {
// Create a handler stack that has all of the default middlewares attached
$handler = \GuzzleHttp\HandlerStack::create(new CurlMultiHandler());
// Push the handler onto the handler stack
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
if ($request->getMethod() !== 'PUT') {
return $request;
}
$body = $request->getBody();
if ($body !== null && $body->getSize() !== 0) {
return $request;
}
if ($request->hasHeader('Content-Length')) {
return $request;
}
// force content length header on empty body
return $request->withHeader('Content-Length', '0');
}));
// Inject the handler into the client
$client = new \GuzzleHttp\Client(['handler' => $handler]);
$h = new GuzzleHandler($client);
$h = $this->getHandlerV7(false); // curlMultiHandler
$dh = $this->getHandlerV7(true); // streamHandler for downloads
}
$config['http_handler'] = $h;
/* @phan-suppress-next-line PhanDeprecatedFunction */
$this->connection = S3Client::factory($config);

// replace the http_handler for the download connection
$config['http_handler'] = $dh;
/* @phan-suppress-next-line PhanDeprecatedFunction */
$this->downConnection = S3Client::factory($config);
try {
$this->connection->listBuckets();
} catch (S3Exception $exception) {
Expand All @@ -156,6 +123,73 @@ protected function init(): void {
}
}

private function getHandlerV5($isStream) {
/*
* Note: phan runs in CI with the latest core, which has Guzzle7 or later.
* So various things that phan reports for this Guzzle5 code have to be suppressed.
*/
if ($isStream) {
/* @phan-suppress-next-line PhanUndeclaredClassMethod */
$client = new \GuzzleHttp\Client(['handler' => new \GuzzleHttp\Ring\Client\StreamHandler()]);
} else {
/* @phan-suppress-next-line PhanUndeclaredClassMethod */
$client = new \GuzzleHttp\Client(['handler' => new \GuzzleHttp\Ring\Client\CurlMultiHandler()]);
}

/* @phan-suppress-next-line PhanDeprecatedFunction */
$emitter = $client->getEmitter();
/* @phan-suppress-next-line PhanUndeclaredTypeParameter */
$beforeEventFunc = static function (\GuzzleHttp\Event\BeforeEvent $event) {
/* @phan-suppress-next-line PhanUndeclaredClassMethod */
$request = $event->getRequest();
if ($request->getMethod() !== 'PUT') {
return;
}
$body = $request->getBody();
if ($body !== null && $body->getSize() !== 0) {
return;
}
if ($request->hasHeader('Content-Length')) {
return;
}
// force content length header on empty body
$request->setHeader('Content-Length', '0');
};
$emitter->on('before', $beforeEventFunc);
$h = new \Aws\Handler\GuzzleV5\GuzzleHandler($client);
return $h;
}

private function getHandlerV7($isStream) {
// Create a handler stack that has all of the default middlewares attached
if ($isStream) {
$handler = \GuzzleHttp\HandlerStack::create(new StreamHandler());
} else {
$handler = \GuzzleHttp\HandlerStack::create(new CurlMultiHandler());
}

$requestFunc = function (RequestInterface $request) {
if ($request->getMethod() !== 'PUT') {
return $request;
}
$body = $request->getBody();
if ($body !== null && $body->getSize() !== 0) {
return $request;
}
if ($request->hasHeader('Content-Length')) {
return $request;
}
// force content length header on empty body
return $request->withHeader('Content-Length', '0');
};
// Push the handler onto the handler stack
$handler->push(Middleware::mapRequest($requestFunc));
// Inject the handler into the client
$client = new \GuzzleHttp\Client(['handler' => $handler]);
$h = new GuzzleHandler($client);
return $h;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -228,7 +262,7 @@ public function readObject($urn) {
$this->init();
try {
$context = stream_context_create([
's3' => ['seekable' => true]
's3' => ['seekable' => true, 'client' => $this->downConnection]
]);
return \fopen($this->getUrl($urn), 'rb', false, $context);
} catch (AwsException $ex) {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parameters:
- '#Method OCA\\Files_Primary_S3\\Panels\\Admin::getPanel\(\) should return OCP\\AppFramework\\Http\\TemplateResponse|OCP\\Template but returns null.#'
- '#Comparison operation ">=" between 7 and 7 is always true.#'
- '#Call to an undefined method GuzzleHttp\\Client::getEmitter\(\).#'
- '#Instantiated class GuzzleHttp\\Ring\\Client\\StreamHandler not found.#'
- '#Instantiated class GuzzleHttp\\Ring\\Client\\CurlMultiHandler not found.#'
- '#Parameter \$event of anonymous function has invalid typehint type GuzzleHttp\\Event\\BeforeEvent.#'
- '#Call to method getRequest\(\) on an unknown class GuzzleHttp\\Event\\BeforeEvent.#'