Skip to content

Commit bb93a26

Browse files
feat(media): support sku/code and attribute fields in media uploads
1 parent 846f506 commit bb93a26

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/Api/MediaFileApi.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,38 @@
77
class MediaFileApi extends AbstractApi
88
{
99
/**
10-
* Upload a product media file and return the server response.
10+
* Upload a product media file alongside the required UnoPim form fields.
1111
*
12+
* @param string $filePath
13+
* @param string $sku
14+
* @param string $attribute
1215
* @return array<string, mixed>
1316
*/
14-
public function uploadProductMedia(string $filePath): array
17+
public function uploadProductMedia(string $filePath, string $sku = '', string $attribute = ''): array
1518
{
16-
return $this->client->uploadFile('/api/v1/rest/media-files/product', $filePath);
19+
$fields = array_filter([
20+
'sku' => $sku,
21+
'attribute' => $attribute,
22+
], static fn ($v) => $v !== '');
23+
24+
return $this->client->uploadFile('/api/v1/rest/media-files/product', $filePath, $fields);
1725
}
1826

1927
/**
20-
* Upload a category media file and return the server response.
28+
* Upload a category media file alongside the required UnoPim form fields.
2129
*
30+
* @param string $filePath
31+
* @param string $code
32+
* @param string $attribute
2233
* @return array<string, mixed>
2334
*/
24-
public function uploadCategoryMedia(string $filePath): array
35+
public function uploadCategoryMedia(string $filePath, string $code = '', string $attribute = ''): array
2536
{
26-
return $this->client->uploadFile('/api/v1/rest/media-files/category', $filePath);
37+
$fields = array_filter([
38+
'code' => $code,
39+
'attribute' => $attribute,
40+
], static fn ($v) => $v !== '');
41+
42+
return $this->client->uploadFile('/api/v1/rest/media-files/category', $filePath, $fields);
2743
}
2844
}

src/UnoPimClient.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,12 @@ public function getBaseUrl(): string
332332
* directly to cURL's native CURLFile support, which is the most reliable
333333
* approach for binary uploads.
334334
*
335+
* @param string $endpoint
336+
* @param string $filePath
337+
* @param array $fields
335338
* @return array<string, mixed>
336339
*/
337-
public function uploadFile(string $endpoint, string $filePath): array
340+
public function uploadFile(string $endpoint, string $filePath, array $fields = []): array
338341
{
339342
if (! file_exists($filePath)) {
340343
throw new \InvalidArgumentException("File not found: {$filePath}");
@@ -343,12 +346,15 @@ public function uploadFile(string $endpoint, string $filePath): array
343346
$token = $this->getAccessToken();
344347
$url = $this->baseUrl . $endpoint;
345348

349+
$postFields = $fields;
350+
$postFields['file'] = new \CURLFile($filePath);
351+
346352
$ch = curl_init();
347353
curl_setopt_array($ch, [
348354
CURLOPT_URL => $url,
349355
CURLOPT_RETURNTRANSFER => true,
350356
CURLOPT_POST => true,
351-
CURLOPT_POSTFIELDS => ['file' => new \CURLFile($filePath)],
357+
CURLOPT_POSTFIELDS => $postFields,
352358
CURLOPT_HTTPHEADER => [
353359
'Accept: application/json',
354360
'Authorization: Bearer ' . $token,

0 commit comments

Comments
 (0)