|
12 | 12 | use GuzzleHttp\Client as GuzzleHttpClient; |
13 | 13 | use GuzzleHttp\Cookie\CookieJar; |
14 | 14 | use GuzzleHttp\Exception\RequestException; |
| 15 | +use GuzzleHttp\Handler\CurlHandler; |
15 | 16 | use GuzzleHttp\HandlerStack; |
16 | 17 | use InvalidArgumentException; |
17 | 18 | use JmapClient\Authentication\Basic; |
|
32 | 33 | use JmapClient\Session\Session; |
33 | 34 | use Psr\Http\Message\RequestInterface; |
34 | 35 | use Psr\Http\Message\ResponseInterface; |
| 36 | +use Psr\Http\Message\StreamInterface; |
35 | 37 |
|
36 | 38 | /** |
37 | 39 | * JMAP Client for communicating with JMAP-compliant servers |
@@ -743,6 +745,52 @@ public function download(string $account, string $identifier, &$data, string $ty |
743 | 745 | } |
744 | 746 | } |
745 | 747 |
|
| 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 | + |
746 | 794 | /** |
747 | 795 | * Uploads a blob/file to the JMAP server from a variable or stream resource |
748 | 796 | */ |
|
0 commit comments