Skip to content

Commit bc2470a

Browse files
Merge pull request #25 from SebastianKrupinski/feat/download-stream
feat: download stream
2 parents d3bd949 + 1240f0c commit bc2470a

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

lib/Client.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use GuzzleHttp\Client as GuzzleHttpClient;
1313
use GuzzleHttp\Cookie\CookieJar;
1414
use GuzzleHttp\Exception\RequestException;
15+
use GuzzleHttp\Handler\CurlHandler;
1516
use GuzzleHttp\HandlerStack;
1617
use InvalidArgumentException;
1718
use JmapClient\Authentication\Basic;
@@ -32,6 +33,7 @@
3233
use JmapClient\Session\Session;
3334
use Psr\Http\Message\RequestInterface;
3435
use Psr\Http\Message\ResponseInterface;
36+
use Psr\Http\Message\StreamInterface;
3537

3638
/**
3739
* JMAP Client for communicating with JMAP-compliant servers
@@ -743,6 +745,52 @@ public function download(string $account, string $identifier, &$data, string $ty
743745
}
744746
}
745747

748+
/**
749+
* Opens a streamed blob/file download from the JMAP server.
750+
*/
751+
public function downloadStream(string $account, string $identifier, string $type = 'application/octet-stream', string $name = 'file.bin'): StreamInterface
752+
{
753+
// evaluate if client is connected
754+
if (!$this->_SessionConnected) {
755+
$this->connect();
756+
}
757+
758+
// replace command options
759+
$location = $this->_ServiceDownloadLocation;
760+
$location = str_replace('{accountId}', $account, $location);
761+
$location = str_replace('{blobId}', $identifier, $location);
762+
$location = str_replace('{type}', $type, $location);
763+
$location = str_replace('{name}', $name, $location);
764+
765+
// modify specific headers
766+
$transportHeaders = $this->_transportHeaders;
767+
unset(
768+
$transportHeaders['Content-Type'],
769+
$transportHeaders['Accept']
770+
);
771+
772+
$clientOptions = $this->_transportOptions;
773+
$clientOptions['handler'] = new CurlHandler();
774+
$clientOptions['version'] = self::TRANSPORT_VERSION_1_1;
775+
776+
if ($this->_client !== null) {
777+
$cookies = $this->_client->getConfig('cookies');
778+
if ($cookies !== null) {
779+
$clientOptions['cookies'] = $cookies;
780+
}
781+
}
782+
783+
$client = new GuzzleHttpClient($clientOptions);
784+
785+
$response = $client->request('GET', $location, [
786+
'headers' => $transportHeaders,
787+
'stream' => true,
788+
'version' => self::TRANSPORT_VERSION_1_1,
789+
]);
790+
791+
return $response->getBody();
792+
}
793+
746794
/**
747795
* Uploads a blob/file to the JMAP server from a variable or stream resource
748796
*/

0 commit comments

Comments
 (0)