diff --git a/src/Dropbox/Dropbox.php b/src/Dropbox/Dropbox.php index b017c38..0efe10b 100644 --- a/src/Dropbox/Dropbox.php +++ b/src/Dropbox/Dropbox.php @@ -6,6 +6,7 @@ use Kunnu\Dropbox\Models\File; use Kunnu\Dropbox\Models\Account; use Kunnu\Dropbox\Models\Thumbnail; +use Kunnu\Dropbox\Models\ThumbnailList; use Kunnu\Dropbox\Models\AccountList; use Kunnu\Dropbox\Models\ModelFactory; use Kunnu\Dropbox\Models\FileMetadata; @@ -238,30 +239,31 @@ public function getMetadata($path, array $params = []) */ public function postToAPI($endpoint, array $params = [], $accessToken = null) { - return $this->sendRequest("POST", $endpoint, 'api', $params, $accessToken); + return $this->sendRequest("POST", $endpoint, 'api', 'rpc', $params, $accessToken); } /** * Make Request to the API * - * @param string $method HTTP Request Method - * @param string $endpoint API Endpoint to send Request to - * @param string $endpointType Endpoint type ['api'|'content'] - * @param array $params Request Query Params - * @param string $accessToken Access Token to send with the Request - * @param DropboxFile $responseFile Save response to the file + * @param string $method HTTP Request Method + * @param string $endpoint API Endpoint to send Request to + * @param string $endpointType Endpoint type ['api'|'content'] + * @param string $endpointFormat Endpoint format ['rpc'|'content'] + * @param array $params Request Query Params + * @param string $accessToken Access Token to send with the Request + * @param DropboxFile $responseFile Save response to the file * * @return \Kunnu\Dropbox\DropboxResponse * * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException */ - public function sendRequest($method, $endpoint, $endpointType = 'api', array $params = [], $accessToken = null, DropboxFile $responseFile = null) + public function sendRequest($method, $endpoint, $endpointType = 'api', $endpointFormat = 'rpc', array $params = [], $accessToken = null, DropboxFile $responseFile = null) { //Access Token $accessToken = $this->getAccessToken() ? $this->getAccessToken() : $accessToken; //Make a DropboxRequest object - $request = new DropboxRequest($method, $endpoint, $accessToken, $endpointType, $params); + $request = new DropboxRequest($method, $endpoint, $accessToken, $endpointType, $endpointFormat, $params); //Make a DropboxResponse object if a response should be saved to the file $response = $responseFile ? new DropboxResponseToFile($request, $responseFile) : null; @@ -941,7 +943,22 @@ public function startUploadSession($dropboxFile, $chunkSize = -1, $close = false */ public function postToContent($endpoint, array $params = [], $accessToken = null, DropboxFile $responseFile = null) { - return $this->sendRequest("POST", $endpoint, 'content', $params, $accessToken, $responseFile); + return $this->sendRequest("POST", $endpoint, 'content', 'content', $params, $accessToken, $responseFile); + } + + /** + * Make a HTTP POST Request to the Content endpoint type with format as RPC + * + * @param string $endpoint Content Endpoint to send Request to + * @param array $params Request Query Params + * @param string $accessToken Access Token to send with the Request + * @param DropboxFile $responseFile Save response to the file + * + * @return \Kunnu\Dropbox\DropboxResponse + */ + public function postToContentAsRpc($endpoint, array $params = [], $accessToken = null, DropboxFile $responseFile = null) + { + return $this->sendRequest("POST", $endpoint, 'content', 'rpc', $params, $accessToken, $responseFile); } /** @@ -1111,6 +1128,46 @@ public function getThumbnail($path, $size = 'small', $format = 'jpeg') return new Thumbnail($metadata, $contents); } + /** + * Get multiple thumbnails in one call. + * Limited to 25 per batch. + * + * @param string[] $entries Paths of the images to get thumbnails for + * @param string $size Size for the thumbnail image ['thumb','small','medium','large','huge'] + * @param string $format Format for the thumbnail image ['jpeg'|'png'] + * @param string $mode How to resize and crop the image to achieve the desired size. ['strict','bestfit','fitone_bestfit'] + * + * @return \Kunnu\Dropbox\Models\ThumbnailList + * + * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException + * + * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail_batch + * + */ + public function getThumbnailBatch($entries, $size = 'small', $format = 'jpeg', $mode = 'strict') + { + //Invalid Format + if (!in_array($format, ['jpeg', 'png'])) { + throw new DropboxClientException("Invalid format. Must either be 'jpeg' or 'png'."); + } + + $params = [ + 'size' => $this->getThumbnailSize($size), + 'format' => $format, + 'mode' => $mode + ]; + $entries = array_map(function ($entry) use ($params) { + return array_merge(['path' => $entry], $params); + }, $entries); + + //Get Thumbnails + $response = $this->postToContentAsRpc('/files/get_thumbnail_batch', ['entries' => $entries]); + $body = $response->getDecodedBody(); + + //Make and return the model + return new ThumbnailList($body); + } + /** * Get thumbnail size * diff --git a/src/Dropbox/DropboxClient.php b/src/Dropbox/DropboxClient.php index ac75f15..fb6d588 100644 --- a/src/Dropbox/DropboxClient.php +++ b/src/Dropbox/DropboxClient.php @@ -185,7 +185,7 @@ protected function prepareRequest(DropboxRequest $request) $url = $this->buildUrl($request->getEndpoint(), $request->getEndpointType()); //The Endpoint is content - if ($request->getEndpointType() === 'content') { + if ($request->getEndpointFormat() === 'content') { //Dropbox requires the parameters to be passed //through the 'Dropbox-API-Arg' header $request->setHeaders(['Dropbox-API-Arg' => json_encode($request->getParams())]); @@ -218,7 +218,7 @@ protected function prepareRequest(DropboxRequest $request) $this->buildAuthHeader($request->getAccessToken()), $this->buildContentTypeHeader($request->getContentType()), $request->getHeaders() - ); + ); //Return the URL, Headers and Request Body return [$url, $headers, $requestBody]; diff --git a/src/Dropbox/DropboxRequest.php b/src/Dropbox/DropboxRequest.php index 0430209..13ceec7 100644 --- a/src/Dropbox/DropboxRequest.php +++ b/src/Dropbox/DropboxRequest.php @@ -46,6 +46,13 @@ class DropboxRequest */ protected $endpointType = null; + /** + * The Endpoint Format for this request + * + * @var string + */ + protected $endpointFormat = null; + /** * The headers to send with this request * @@ -81,19 +88,29 @@ class DropboxRequest /** * Create a new DropboxRequest instance * - * @param string $method HTTP Method of the Request - * @param string $endpoint API endpoint of the Request - * @param string $accessToken Access Token for the Request - * @param string $endpointType Endpoint type ['api'|'content'] - * @param mixed $params Request Params - * @param array $headers Headers to send along with the Request + * @param string $method HTTP Method of the Request + * @param string $endpoint API endpoint of the Request + * @param string $accessToken Access Token for the Request + * @param string $endpointType Endpoint type ['api'|'content'] + * @param string $endpointFormat Endpoint format ['rpc'|'content'] + * @param mixed $params Request Params + * @param array $headers Headers to send along with the Request */ - public function __construct($method, $endpoint, $accessToken, $endpointType = "api", array $params = [], array $headers = [], $contentType = null) - { + public function __construct( + $method, + $endpoint, + $accessToken, + $endpointType = "api", + $endpointFormat = "rpc", + array $params = [], + array $headers = [], + $contentType = null + ) { $this->setMethod($method); $this->setEndpoint($endpoint); $this->setAccessToken($accessToken); $this->setEndpointType($endpointType); + $this->setEndpointFormat($endpointFormat); $this->setParams($params); $this->setHeaders($headers); @@ -112,7 +129,7 @@ public function getMethod() return $this->method; } - /** + /** * Set the Request Method * * @param string @@ -198,6 +215,30 @@ public function setEndpointType($endpointType) return $this; } + /** + * Get the Endpoint Format of the Request + * + * @return string + */ + public function getEndpointFormat() + { + return $this->endpointFormat; + } + + /** + * Set the Endpoint Format of the Request + * + * @param string + * + * @return \Kunnu\Dropbox\DropboxRequest + */ + public function setEndpointFormat($endpointFormat) + { + $this->endpointFormat = $endpointFormat; + + return $this; + } + /** * Get the Content Type of the Request * diff --git a/src/Dropbox/Models/ThumbnailList.php b/src/Dropbox/Models/ThumbnailList.php new file mode 100644 index 0000000..edfafce --- /dev/null +++ b/src/Dropbox/Models/ThumbnailList.php @@ -0,0 +1,35 @@ +processItems($data['entries']); + parent::__construct($processedItems); + } + + /** + * Process items and cast them + * to Thumbnail Model + * + * @param array $items Unprocessed Items + * + * @return array Array of Thumbnail models + */ + protected function processItems(array $items) + { + $processedItems = []; + + foreach ($items as $entry) { + $processedItems[] = new Thumbnail($entry['metadata'], $entry['thumbnail']); + } + + return $processedItems; + } +}