Skip to content

Commit 72d96fe

Browse files
authored
Fix offloading logic
2 parents 9f3390e + 78bed51 commit 72d96fe

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The second argument of the `init` method is optional. It allows you to pass opti
3535
* `base_domain`: The base domain to connect to Optimole's API. Default is `i.optimole.com`.
3636
* `cache_buster`: A string value that will be appended to the URL of the optimized assets to bust Optimole's cache.
3737
* `dashboard_api_url`: The URL of the dashboard API. Default is `https://dashboard.optimole.com/api`.
38+
* `dashboard_api_key`: The API key to use for the dashboard API.
3839
* `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.
3940
* `upload_api_url`: The URL of the upload API. Default is `https://generateurls-prod.i.optimole.com/upload`.
4041

src/Offload/Manager.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ class Manager
3131
*/
3232
private ClientInterface $httpClient;
3333

34-
/**
35-
* The Optimole API key.
36-
*/
37-
private string $key;
38-
3934
/**
4035
* The manager options.
4136
*/
@@ -44,7 +39,7 @@ class Manager
4439
/**
4540
* Constructor.
4641
*/
47-
public function __construct(ClientInterface $httpClient, string $key, array $options = [])
42+
public function __construct(ClientInterface $httpClient, array $options = [])
4843
{
4944
if (empty($options['dashboard_api_url'])) {
5045
throw new InvalidArgumentException('Missing "dashboard_api_url" option');
@@ -53,7 +48,6 @@ public function __construct(ClientInterface $httpClient, string $key, array $opt
5348
}
5449

5550
$this->httpClient = $httpClient;
56-
$this->key = $key;
5751
$this->options = array_merge([
5852
'upload_api_credentials' => [],
5953
], $options);
@@ -114,7 +108,7 @@ public function getUsage(): OffloadUsage
114108
/**
115109
* Update the metadata of the image with the given ID.
116110
*/
117-
public function updateImageMetadata(string $imageId, int $fileSize = 0, $height = 'auto', $width = 'auto'): void
111+
public function updateImageMetadata(string $imageId, int $fileSize = 0, $height = 'auto', $width = 'auto', $originalUrl = ''): void
118112
{
119113
if ('auto' !== $height && !is_int($height)) {
120114
throw new InvalidArgumentException('Image height must be "auto" or an integer.');
@@ -127,6 +121,7 @@ public function updateImageMetadata(string $imageId, int $fileSize = 0, $height
127121
'originalFileSize' => $fileSize,
128122
'height' => is_int($height) ? max(0, $height) : $height,
129123
'width' => is_int($width) ? max(0, $width) : $width,
124+
'originalUrl' => $originalUrl,
130125
'updateDynamo' => 'success',
131126
]);
132127
}
@@ -179,7 +174,7 @@ public function uploadImage(string $filename, string $imageUrl): string
179174
}
180175

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

189184
$imagesize = getimagesize($filename);
190185

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

193188
return $imageId;
194189
}
@@ -237,8 +232,8 @@ private function getUploadApiCredentialsFromDashboardApi(): array
237232
*/
238233
private function requestToDashboardApi(): array
239234
{
240-
return $this->httpClient->sendRequest('post', sprintf('%s/optml/v2/account/details', $this->options['dashboard_api_url']), null, [
241-
'Authorization' => sprintf('Bearer %s', $this->key),
235+
return $this->httpClient->sendRequest('POST', sprintf('%s/optml/v2/account/details', $this->options['dashboard_api_url']), null, [
236+
'Authorization' => sprintf('Bearer %s', $this->options['dashboard_api_key']),
242237
'Content-Type' => 'application/json',
243238
]);
244239
}
@@ -252,7 +247,7 @@ private function requestToUploadApi(array $body): array
252247
$this->options['upload_api_credentials'] = $this->getUploadApiCredentialsFromDashboardApi();
253248
}
254249

255-
return $this->httpClient->sendRequest('post', $this->options['upload_api_url'], array_merge($this->options['upload_api_credentials'], $body), [
250+
return $this->httpClient->sendRequest('POST', $this->options['upload_api_url'], array_merge($this->options['upload_api_credentials'], $body), [
256251
'Content-Type' => 'application/json',
257252
]);
258253
}

src/Optimole.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class Optimole
3333
/**
3434
* The Optimole SDK version.
3535
*/
36-
public const VERSION = '1.2.0';
36+
public const VERSION = '1.2.1';
3737

3838
/**
3939
* The Optimole dashboard API URL.
@@ -94,6 +94,7 @@ public static function init(string $key, array $options = []): void
9494
$options = array_merge([
9595
'base_domain' => 'i.optimole.com',
9696
'cache_buster' => '',
97+
'dashboard_api_key' => '',
9798
'dashboard_api_url' => self::DASHBOARD_API_URL,
9899
'upload_api_url' => self::UPLOAD_API_URL,
99100
], $options);
@@ -130,7 +131,7 @@ private function createImage(string $imageUrl, string $cacheBuster = ''): Image
130131
*/
131132
private function createOffload(array $options = []): Manager
132133
{
133-
return new Manager($this->getHttpClient(), $this->key, array_merge($this->options, $options));
134+
return new Manager($this->getHttpClient(), array_merge($this->options, $options));
134135
}
135136

136137
/**

0 commit comments

Comments
 (0)