diff --git a/test_asset_detail.php b/test_asset_detail.php index 960b594..419001c 100644 --- a/test_asset_detail.php +++ b/test_asset_detail.php @@ -21,6 +21,65 @@ $assetId = $_GET['asset_id'] ?? '7f2a881a7448794e4c21100c49a32ff0b87bf259'; $assetDetails = null; $renditions = null; $error = null; +$downloadResult = null; + +// Handle download action +if ($_GET['action'] === 'download' && isset($_GET['endpoint'])) { + $endpoint = $_GET['endpoint']; + $downloadId = $_GET['download_id'] ?? $assetId; + + try { + $apiClient = new ApiClient(); + $apiClient->setBaseUrl($configV3->getBaseUrl()); + + $oauth2Handler = new ReflectionProperty($testRunner, 'oauth2Handler'); + $oauth2Handler->setAccessible(true); + $oauth2HandlerInstance = $oauth2Handler->getValue($testRunner); + if ($oauth2HandlerInstance) { + $apiClient->setHeader('Authorization', $oauth2HandlerInstance->getAuthHeader()); + } + + $request = ['method' => 'GET', 'url' => $endpoint]; + $response = $apiClient->executeRequest($request); + + if ($response['success'] && !empty($response['body'])) { + $jsonCheck = json_decode($response['body'], true); + if (!$jsonCheck || !isset($jsonCheck['exception_body'])) { + // Success! Save file + if (!is_dir('downloads')) { + mkdir('downloads', 0755, true); + } + + $filename = "asset_{$downloadId}.jpg"; + $filepath = "downloads/{$filename}"; + file_put_contents($filepath, $response['body']); + + $downloadResult = [ + 'success' => true, + 'filename' => $filename, + 'filepath' => $filepath, + 'size' => strlen($response['body']), + 'endpoint' => $endpoint + ]; + } else { + $downloadResult = [ + 'success' => false, + 'error' => $jsonCheck['exception_body']['message'] ?? 'Unknown error' + ]; + } + } else { + $downloadResult = [ + 'success' => false, + 'error' => $response['error'] ?? 'Download failed' + ]; + } + } catch (Exception $e) { + $downloadResult = [ + 'success' => false, + 'error' => $e->getMessage() + ]; + } +} if ($_GET['action'] === 'get_asset') { try { @@ -70,6 +129,7 @@ if ($_GET['action'] === 'get_asset') { // Try different download endpoints $downloadAttempts = []; + $successfulDownload = null; // Attempt 1: /contents $attempt1 = [ @@ -77,12 +137,26 @@ if ($_GET['action'] === 'get_asset') { 'url' => "/v6/assets/{$assetId}/contents" ]; $result1 = $apiClient->executeRequest($attempt1); + $isError1 = false; + if ($result1['success'] && !empty($result1['body'])) { + $jsonCheck = json_decode($result1['body'], true); + $isError1 = $jsonCheck && isset($jsonCheck['exception_body']); + + if (!$isError1 && strlen($result1['body']) > 1000) { + $successfulDownload = [ + 'endpoint' => 'contents', + 'url' => $attempt1['url'], + 'data' => $result1['body'], + 'size' => strlen($result1['body']) + ]; + } + } $downloadAttempts['contents'] = [ 'url' => $attempt1['url'], 'http_code' => $result1['http_code'], - 'success' => $result1['success'], + 'success' => $result1['success'] && !$isError1, 'response_size' => strlen($result1['body'] ?? ''), - 'is_json_error' => json_decode($result1['body'] ?? '', true) !== null + 'is_json_error' => $isError1 ]; // Attempt 2: /content (without 's') @@ -91,12 +165,26 @@ if ($_GET['action'] === 'get_asset') { 'url' => "/v6/assets/{$assetId}/content" ]; $result2 = $apiClient->executeRequest($attempt2); + $isError2 = false; + if ($result2['success'] && !empty($result2['body'])) { + $jsonCheck = json_decode($result2['body'], true); + $isError2 = $jsonCheck && isset($jsonCheck['exception_body']); + + if (!$isError2 && strlen($result2['body']) > 1000 && !$successfulDownload) { + $successfulDownload = [ + 'endpoint' => 'content', + 'url' => $attempt2['url'], + 'data' => $result2['body'], + 'size' => strlen($result2['body']) + ]; + } + } $downloadAttempts['content'] = [ 'url' => $attempt2['url'], 'http_code' => $result2['http_code'], - 'success' => $result2['success'], + 'success' => $result2['success'] && !$isError2, 'response_size' => strlen($result2['body'] ?? ''), - 'is_json_error' => json_decode($result2['body'] ?? '', true) !== null + 'is_json_error' => $isError2 ]; // Attempt 3: /original rendition @@ -105,24 +193,27 @@ if ($_GET['action'] === 'get_asset') { 'url' => "/v6/assets/{$assetId}/renditions/ORIGINAL" ]; $result3 = $apiClient->executeRequest($attempt3); - $downloadAttempts['rendition_original'] = [ - 'url' => $attempt3['url'], - 'http_code' => $result3['http_code'], - 'success' => $result3['success'], - 'response_size' => strlen($result3['body'] ?? ''), - 'is_json_error' => json_decode($result3['body'] ?? '', true) !== null - ]; + $isError3 = false; + if ($result3['success'] && !empty($result3['body'])) { + $jsonCheck = json_decode($result3['body'], true); + $isError3 = $jsonCheck && isset($jsonCheck['exception_body']); - // Attempt 4: Direct file download if we have a URL - if (isset($assetDetails['asset']['content_elements']['content_element'][0])) { - $contentElement = $assetDetails['asset']['content_elements']['content_element'][0]; - if (isset($contentElement['download_url'])) { - $downloadAttempts['direct_url'] = [ - 'url' => $contentElement['download_url'], - 'note' => 'Found in content_element download_url' + if (!$isError3 && strlen($result3['body']) > 1000 && !$successfulDownload) { + $successfulDownload = [ + 'endpoint' => 'rendition_original', + 'url' => $attempt3['url'], + 'data' => $result3['body'], + 'size' => strlen($result3['body']) ]; } } + $downloadAttempts['rendition_original'] = [ + 'url' => $attempt3['url'], + 'http_code' => $result3['http_code'], + 'success' => $result3['success'] && !$isError3, + 'response_size' => strlen($result3['body'] ?? ''), + 'is_json_error' => $isError3 + ]; } catch (Exception $e) { $error = $e->getMessage(); @@ -225,7 +316,45 @@ $oauth2Status = $testRunner->getOAuth2Status(); + +
+ + ✅ Download Successful!
+ File:
+ Size: bytes
+ Path:
+ Endpoint: + + ❌ Download Failed: + +
+ + +
+

📥 Quick Download

+

Click a successful endpoint below to download the file:

+
+ $attempt): + if ($attempt['success']): + $hasSuccess = true; + ?> + + 📥 Download via "" ( bytes) + + + +

⚠️ No successful endpoints found. File may not exist in storage.

+ +
+
+

Download Endpoint Tests

diff --git a/test_direct_download.php b/test_direct_download.php new file mode 100644 index 0000000..f9d8fd3 --- /dev/null +++ b/test_direct_download.php @@ -0,0 +1,234 @@ + $configV3->getBaseUrl(), + 'timeout' => $configV3->get('api.timeout'), + 'headers' => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + ] +]; + +$collectionPath = __DIR__ . '/' . $configV3->get('postman_collection'); +$testRunner = new TestRunner($collectionPath, $config); + +$assetId = $_GET['asset_id'] ?? '001aca62191e54abd8e145d688ce962ee98326cb'; +$renditionId = $_GET['rendition_id'] ?? ''; +$results = []; + +if ($_GET['action'] === 'test') { + $apiClient = new ApiClient(); + $apiClient->setBaseUrl($configV3->getBaseUrl()); + + // Get OAuth2 token + $oauth2Handler = new ReflectionProperty($testRunner, 'oauth2Handler'); + $oauth2Handler->setAccessible(true); + $oauth2HandlerInstance = $oauth2Handler->getValue($testRunner); + $authHeader = null; + if ($oauth2HandlerInstance) { + $authHeader = $oauth2HandlerInstance->getAuthHeader(); + $apiClient->setHeader('Authorization', $authHeader); + } + + // Show auth status + echo "
"; + echo "🔑 Authentication Status:
"; + echo "OAuth2 Handler: " . ($oauth2HandlerInstance ? 'Active' : 'Not found') . "
"; + echo "Auth Header: " . ($authHeader ? substr($authHeader, 0, 20) . '...' : 'Not set') . "
"; + echo "Token Info: " . ($oauth2HandlerInstance ? json_encode($oauth2HandlerInstance->getTokenInfo()) : 'N/A'); + echo "
"; + + // Try multiple download endpoints + // Note: V3 Postman collection shows {{baseUrl}}/assets/{id}/contents + // where {{baseUrl}} = https://ppr.dam.ferrero.com/otmmapi + $endpoints = [ + // Standard v6 API format (most likely correct based on errors) + '/v6/assets/' . $assetId . '/contents', + '/v6/assets/' . $assetId . '/content', + '/v6/renditions/' . $assetId, + ]; + + // If rendition ID provided, try it + if ($renditionId) { + array_unshift($endpoints, '/v6/renditions/' . $renditionId); + array_unshift($endpoints, '/renditions/' . $renditionId); + } + + foreach ($endpoints as $endpoint) { + $request = ['method' => 'GET', 'url' => $endpoint]; + $response = $apiClient->executeRequest($request); + + $isError = false; + $errorMsg = null; + + if ($response['success'] && !empty($response['body'])) { + $jsonCheck = json_decode($response['body'], true); + if ($jsonCheck && isset($jsonCheck['exception_body'])) { + $isError = true; + $errorMsg = $jsonCheck['exception_body']['message'] ?? 'Error'; + } + } + + $results[] = [ + 'endpoint' => $endpoint, + 'full_url' => $configV3->getBaseUrl() . $endpoint, + 'http_code' => $response['http_code'], + 'success' => $response['success'], + 'body_size' => strlen($response['body'] ?? ''), + 'is_error' => $isError, + 'error_msg' => $errorMsg, + 'is_binary' => !$isError && $response['success'] && strlen($response['body'] ?? '') > 1000 + ]; + + // If successful, try to save the file + if (!$isError && $response['success'] && strlen($response['body'] ?? '') > 1000) { + $filename = "test_download_" . basename($endpoint) . ".jpg"; + $filepath = "downloads/" . $filename; + + if (!is_dir('downloads')) { + mkdir('downloads', 0755, true); + } + + file_put_contents($filepath, $response['body']); + $results[count($results) - 1]['saved_to'] = $filepath; + $results[count($results) - 1]['saved_size'] = filesize($filepath); + } + } +} + +$oauth2Status = $testRunner->getOAuth2Status(); +?> + + + + + + Direct Download Test + + + +
+

🔍 Direct Download Test

+ + +
+ +
+ + +
+
+ +
+ + + + + +

Download Endpoint Test Results

+
+ + + + + + + + + + + + + + + + + + + + + + +
EndpointFull URLHTTPSizeStatusResult
bytes + + ✅ BINARY FILE + + ❌ ERROR + + ⚠️ UNKNOWN + + + + Saved to: +
( bytes) + + + +
+ +
+ ✅ Look for green rows with "BINARY FILE" - those are successful downloads! +
+ +
+ +