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

Add function for getting thumbnail batches. #164

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
77 changes: 67 additions & 10 deletions src/Dropbox/Dropbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions src/Dropbox/DropboxClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())]);
Expand Down Expand Up @@ -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];
Expand Down
59 changes: 50 additions & 9 deletions src/Dropbox/DropboxRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);

Expand All @@ -112,7 +129,7 @@ public function getMethod()
return $this->method;
}

/**
/**
* Set the Request Method
*
* @param string
Expand Down Expand Up @@ -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
*
Expand Down
35 changes: 35 additions & 0 deletions src/Dropbox/Models/ThumbnailList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Kunnu\Dropbox\Models;

class ThumbnailList extends ModelCollection
{
/**
* Create a new Metadata Collection
*
* @param array $data Collection Data
*/
public function __construct(array $data)
{
$processedItems = $this->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;
}
}