Skip to content

Commit

Permalink
Fix offloading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
selul authored Aug 6, 2024
2 parents 9f3390e + 78bed51 commit 72d96fe
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The second argument of the `init` method is optional. It allows you to pass opti
* `base_domain`: The base domain to connect to Optimole's API. Default is `i.optimole.com`.
* `cache_buster`: A string value that will be appended to the URL of the optimized assets to bust Optimole's cache.
* `dashboard_api_url`: The URL of the dashboard API. Default is `https://dashboard.optimole.com/api`.
* `dashboard_api_key`: The API key to use for the dashboard API.
* `upload_api_credentials`: An array with the credentials to use for the upload API. The array should contain the keys `userKey` and `secret`. The default is empty and the SDK will use the API key provided in the `init` method to fetch them from the dashboard API.
* `upload_api_url`: The URL of the upload API. Default is `https://generateurls-prod.i.optimole.com/upload`.

Expand Down
21 changes: 8 additions & 13 deletions src/Offload/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class Manager
*/
private ClientInterface $httpClient;

/**
* The Optimole API key.
*/
private string $key;

/**
* The manager options.
*/
Expand All @@ -44,7 +39,7 @@ class Manager
/**
* Constructor.
*/
public function __construct(ClientInterface $httpClient, string $key, array $options = [])
public function __construct(ClientInterface $httpClient, array $options = [])
{
if (empty($options['dashboard_api_url'])) {
throw new InvalidArgumentException('Missing "dashboard_api_url" option');
Expand All @@ -53,7 +48,6 @@ public function __construct(ClientInterface $httpClient, string $key, array $opt
}

$this->httpClient = $httpClient;
$this->key = $key;
$this->options = array_merge([
'upload_api_credentials' => [],
], $options);
Expand Down Expand Up @@ -114,7 +108,7 @@ public function getUsage(): OffloadUsage
/**
* Update the metadata of the image with the given ID.
*/
public function updateImageMetadata(string $imageId, int $fileSize = 0, $height = 'auto', $width = 'auto'): void
public function updateImageMetadata(string $imageId, int $fileSize = 0, $height = 'auto', $width = 'auto', $originalUrl = ''): void
{
if ('auto' !== $height && !is_int($height)) {
throw new InvalidArgumentException('Image height must be "auto" or an integer.');
Expand All @@ -127,6 +121,7 @@ public function updateImageMetadata(string $imageId, int $fileSize = 0, $height
'originalFileSize' => $fileSize,
'height' => is_int($height) ? max(0, $height) : $height,
'width' => is_int($width) ? max(0, $width) : $width,
'originalUrl' => $originalUrl,
'updateDynamo' => 'success',
]);
}
Expand Down Expand Up @@ -179,7 +174,7 @@ public function uploadImage(string $filename, string $imageUrl): string
}

try {
$this->httpClient->sendRequest('put', $uploadUrl, $image, [
$this->httpClient->sendRequest('PUT', $uploadUrl, $image, [
'Content-Type' => $fileMimeType,
]);
} catch (BadResponseException $exception) {
Expand All @@ -188,7 +183,7 @@ public function uploadImage(string $filename, string $imageUrl): string

$imagesize = getimagesize($filename);

$this->updateImageMetadata($imageId, filesize($filename) ?: 0, $imagesize && !empty($imagesize[1]) ? $imagesize[1] : 'auto', $imagesize && !empty($imagesize[0]) ? $imagesize[0] : 'auto');
$this->updateImageMetadata($imageId, filesize($filename) ?: 0, $imagesize && !empty($imagesize[1]) ? $imagesize[1] : 'auto', $imagesize && !empty($imagesize[0]) ? $imagesize[0] : 'auto', $imageUrl);

return $imageId;
}
Expand Down Expand Up @@ -237,8 +232,8 @@ private function getUploadApiCredentialsFromDashboardApi(): array
*/
private function requestToDashboardApi(): array
{
return $this->httpClient->sendRequest('post', sprintf('%s/optml/v2/account/details', $this->options['dashboard_api_url']), null, [
'Authorization' => sprintf('Bearer %s', $this->key),
return $this->httpClient->sendRequest('POST', sprintf('%s/optml/v2/account/details', $this->options['dashboard_api_url']), null, [
'Authorization' => sprintf('Bearer %s', $this->options['dashboard_api_key']),
'Content-Type' => 'application/json',
]);
}
Expand All @@ -252,7 +247,7 @@ private function requestToUploadApi(array $body): array
$this->options['upload_api_credentials'] = $this->getUploadApiCredentialsFromDashboardApi();
}

return $this->httpClient->sendRequest('post', $this->options['upload_api_url'], array_merge($this->options['upload_api_credentials'], $body), [
return $this->httpClient->sendRequest('POST', $this->options['upload_api_url'], array_merge($this->options['upload_api_credentials'], $body), [
'Content-Type' => 'application/json',
]);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Optimole.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class Optimole
/**
* The Optimole SDK version.
*/
public const VERSION = '1.2.0';
public const VERSION = '1.2.1';

/**
* The Optimole dashboard API URL.
Expand Down Expand Up @@ -94,6 +94,7 @@ public static function init(string $key, array $options = []): void
$options = array_merge([
'base_domain' => 'i.optimole.com',
'cache_buster' => '',
'dashboard_api_key' => '',
'dashboard_api_url' => self::DASHBOARD_API_URL,
'upload_api_url' => self::UPLOAD_API_URL,
], $options);
Expand Down Expand Up @@ -130,7 +131,7 @@ private function createImage(string $imageUrl, string $cacheBuster = ''): Image
*/
private function createOffload(array $options = []): Manager
{
return new Manager($this->getHttpClient(), $this->key, array_merge($this->options, $options));
return new Manager($this->getHttpClient(), array_merge($this->options, $options));
}

/**
Expand Down
Loading

0 comments on commit 72d96fe

Please sign in to comment.