AssetUploaderSimple - exact copy of standalone logic: - Only 5 metadata fields (not 17) - Same field order and structure - Same cURL options - Produces ~1200 byte payload like standalone Test upload now uses AssetUploaderSimple for exact match. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
205 lines
7.9 KiB
PHP
205 lines
7.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Simplified AssetUploader - copies EXACT approach from working standalone script
|
|
*/
|
|
class AssetUploaderSimple
|
|
{
|
|
private $apiClient;
|
|
|
|
public function __construct($apiClient)
|
|
{
|
|
$this->apiClient = $apiClient;
|
|
}
|
|
|
|
public function uploadFile($filePath, $folderId)
|
|
{
|
|
if (!file_exists($filePath)) {
|
|
return [
|
|
'success' => false,
|
|
'error' => 'File not found',
|
|
'filename' => basename($filePath)
|
|
];
|
|
}
|
|
|
|
$filename = basename($filePath);
|
|
$mimeType = mime_content_type($filePath);
|
|
|
|
// Get base URL
|
|
$baseUrlReflection = new ReflectionProperty($this->apiClient, 'baseUrl');
|
|
$baseUrlReflection->setAccessible(true);
|
|
$baseUrl = $baseUrlReflection->getValue($this->apiClient);
|
|
$url = rtrim($baseUrl, '/') . '/v6/assets';
|
|
|
|
// Get auth header
|
|
$headersReflection = new ReflectionProperty($this->apiClient, 'headers');
|
|
$headersReflection->setAccessible(true);
|
|
$headers = $headersReflection->getValue($this->apiClient);
|
|
$authHeader = $headers['Authorization'] ?? '';
|
|
|
|
// EXACT structure from working standalone (SIMPLIFIED - only 5 fields)
|
|
$assetRep = [
|
|
'asset_resource' => [
|
|
'asset' => [
|
|
'metadata' => [
|
|
'metadata_element_list' => [
|
|
[
|
|
'id' => 'FERRERO.FIELD.MKTG.ASSET TYPE',
|
|
'type' => 'com.artesia.metadata.MetadataField',
|
|
'value' => [
|
|
'cascading_domain_value' => false,
|
|
'domain_value' => true,
|
|
'value' => [
|
|
'type' => 'string',
|
|
'value' => 'heroimage'
|
|
]
|
|
]
|
|
],
|
|
[
|
|
'id' => 'FERRERO.FIELD.FISCAL YEAR',
|
|
'type' => 'com.artesia.metadata.MetadataField',
|
|
'value' => [
|
|
'cascading_domain_value' => false,
|
|
'domain_value' => true,
|
|
'value' => [
|
|
'type' => 'string',
|
|
'value' => '2025/2026'
|
|
]
|
|
]
|
|
],
|
|
[
|
|
'id' => 'MAIN_LANGUAGES',
|
|
'parent_table_id' => 'FERRERO.TABULAR.FIELD.MAIN LANGUAGES',
|
|
'type' => 'com.artesia.metadata.MetadataTableField',
|
|
'values' => [
|
|
[
|
|
'cascading_domain_value' => false,
|
|
'domain_value' => true,
|
|
'value' => [
|
|
'field_value' => [
|
|
'type' => 'string',
|
|
'value' => 'IT'
|
|
],
|
|
'type' => 'com.artesia.metadata.DomainValue'
|
|
]
|
|
]
|
|
]
|
|
],
|
|
[
|
|
'id' => 'ARTESIA.FIELD.ASSET NAME',
|
|
'type' => 'com.artesia.metadata.MetadataField',
|
|
'value' => [
|
|
'cascading_domain_value' => false,
|
|
'domain_value' => true,
|
|
'value' => [
|
|
'type' => 'string',
|
|
'value' => $filename
|
|
]
|
|
]
|
|
],
|
|
[
|
|
'id' => 'FERRERO.FIELD.STATE',
|
|
'type' => 'com.artesia.metadata.MetadataField',
|
|
'value' => [
|
|
'cascading_domain_value' => false,
|
|
'domain_value' => true,
|
|
'value' => [
|
|
'type' => 'string',
|
|
'value' => 'Local'
|
|
]
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'metadata_model_id' => 'ECOMMERCE',
|
|
'security_policy_list' => [
|
|
['id' => 1594]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
$uploadManifest = [
|
|
'upload_manifest' => [
|
|
'master_files' => [
|
|
[
|
|
'file' => [
|
|
'file_name' => $filename,
|
|
'file_type' => $mimeType
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
$postFields = [
|
|
'asset_representation' => json_encode($assetRep),
|
|
'parent_folder_id' => $folderId,
|
|
'manifest' => json_encode($uploadManifest),
|
|
'files' => new \CURLFile($filePath, $mimeType, $filename)
|
|
];
|
|
|
|
error_log("SIMPLE UPLOADER: Asset Rep size: " . strlen(json_encode($assetRep)));
|
|
error_log("SIMPLE UPLOADER: Manifest size: " . strlen(json_encode($uploadManifest)));
|
|
|
|
$cookieFile = sys_get_temp_dir() . '/ferrero_upload_cookies.txt';
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $postFields,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Accept: application/json',
|
|
'Authorization: Bearer ' . str_replace('Bearer ', '', $authHeader)
|
|
],
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_TIMEOUT => 60,
|
|
CURLOPT_COOKIEJAR => $cookieFile,
|
|
CURLOPT_COOKIEFILE => $cookieFile
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curlError = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($curlError) {
|
|
return [
|
|
'success' => false,
|
|
'filename' => $filename,
|
|
'error' => 'CURL error: ' . $curlError,
|
|
'http_code' => 0
|
|
];
|
|
}
|
|
|
|
if ($httpCode == 201 || $httpCode == 202) {
|
|
$responseData = json_decode($response, true);
|
|
return [
|
|
'success' => true,
|
|
'asset_id' => $responseData['asset_resource_list']['asset_resource'][0]['asset']['asset_id'] ??
|
|
$responseData['job_handle']['job_id'] ?? null,
|
|
'filename' => $filename,
|
|
'http_code' => $httpCode
|
|
];
|
|
}
|
|
|
|
// Extract error
|
|
$errorMsg = 'Upload failed';
|
|
if (!empty($response)) {
|
|
$responseData = json_decode($response, true);
|
|
if ($responseData && isset($responseData['exception_body'])) {
|
|
$errorMsg = $responseData['exception_body']['message'] ?? 'API Error';
|
|
}
|
|
}
|
|
|
|
return [
|
|
'success' => false,
|
|
'filename' => $filename,
|
|
'error' => $errorMsg,
|
|
'http_code' => $httpCode,
|
|
'response_body' => substr($response ?? '', 0, 1000)
|
|
];
|
|
}
|
|
}
|