ferrero-opentext/test_asset_detail.php

414 lines
16 KiB
PHP

<?php
session_start();
require_once 'config_v3.php';
require_once 'src/TestRunner.php';
$configV3 = new ConfigV3();
$config = [
'baseUrl' => $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'] ?? '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 {
// Get asset details with full metadata
$apiClient = new ApiClient();
$apiClient->setBaseUrl($configV3->getBaseUrl());
// Get OAuth2 token
$oauth2Handler = new ReflectionProperty($testRunner, 'oauth2Handler');
$oauth2Handler->setAccessible(true);
$oauth2HandlerInstance = $oauth2Handler->getValue($testRunner);
if ($oauth2HandlerInstance) {
$apiClient->setHeader('Authorization', $oauth2HandlerInstance->getAuthHeader());
}
// Get full asset details
$request = [
'method' => 'GET',
'url' => "/v6/assets/{$assetId}?load_type=full"
];
$response = $apiClient->executeRequest($request);
if ($response['success']) {
$responseData = json_decode($response['body'], true);
// Check if it's an error response
if (isset($responseData['exception_body'])) {
$error = "Asset not found: " . ($responseData['exception_body']['message'] ?? 'Unknown error');
} else {
$assetDetails = $responseData;
}
} else {
$error = "Failed to get asset: " . ($response['error'] ?? 'Unknown error');
}
// Try to get renditions
$renditionRequest = [
'method' => 'GET',
'url' => "/v6/assets/{$assetId}/renditions"
];
$renditionResponse = $apiClient->executeRequest($renditionRequest);
if ($renditionResponse['success']) {
$renditions = json_decode($renditionResponse['body'], true);
}
// Try different download endpoints
$downloadAttempts = [];
$successfulDownload = null;
// Attempt 1: /contents
$attempt1 = [
'method' => 'GET',
'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'] && !$isError1,
'response_size' => strlen($result1['body'] ?? ''),
'is_json_error' => $isError1
];
// Attempt 2: /content (without 's')
$attempt2 = [
'method' => 'GET',
'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'] && !$isError2,
'response_size' => strlen($result2['body'] ?? ''),
'is_json_error' => $isError2
];
// Attempt 3: /original rendition
$attempt3 = [
'method' => 'GET',
'url' => "/v6/assets/{$assetId}/renditions/ORIGINAL"
];
$result3 = $apiClient->executeRequest($attempt3);
$isError3 = false;
if ($result3['success'] && !empty($result3['body'])) {
$jsonCheck = json_decode($result3['body'], true);
$isError3 = $jsonCheck && isset($jsonCheck['exception_body']);
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();
}
}
$oauth2Status = $testRunner->getOAuth2Status();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Asset Download</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
padding: 20px;
background: #f5f5f5;
}
.container {
max-width: 1400px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.alert {
padding: 15px;
border-radius: 6px;
margin: 15px 0;
}
.alert-success { background: #d4edda; color: #155724; }
.alert-error { background: #f8d7da; color: #721c24; }
.btn {
padding: 12px 24px;
background: #667eea;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
text-decoration: none;
display: inline-block;
}
.btn:hover { background: #5568d3; }
pre {
background: #f8f9fa;
padding: 15px;
border-radius: 6px;
overflow-x: auto;
max-height: 400px;
}
.section {
margin: 30px 0;
padding: 20px;
background: #f8f9fa;
border-radius: 8px;
}
table {
width: 100%;
border-collapse: collapse;
margin: 15px 0;
}
th, td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background: #667eea;
color: white;
}
.success { color: #28a745; font-weight: bold; }
.failure { color: #dc3545; font-weight: bold; }
</style>
</head>
<body>
<div class="container">
<h1>🔍 Test Asset Download Endpoints</h1>
<?php if ($oauth2Status && $oauth2Status['enabled']): ?>
<div class="alert alert-<?= ($oauth2Status['has_token'] ?? false) ? 'success' : 'error' ?>">
<?= ($oauth2Status['has_token'] ?? false) ? '✅ OAuth2 Token Active' : '❌ OAuth2 Token Issue' ?>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-error">
<strong>Error:</strong> <?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<form method="GET">
<input type="text" name="asset_id" value="<?= htmlspecialchars($assetId) ?>"
style="width: 500px; padding: 10px; border: 1px solid #ddd; border-radius: 4px;">
<input type="hidden" name="action" value="get_asset">
<button type="submit" class="btn">Get Asset Details</button>
</form>
<?php if ($downloadResult): ?>
<div class="alert alert-<?= $downloadResult['success'] ? 'success' : 'error' ?>">
<?php if ($downloadResult['success']): ?>
<strong>✅ Download Successful!</strong><br>
File: <?= htmlspecialchars($downloadResult['filename']) ?><br>
Size: <?= number_format($downloadResult['size']) ?> bytes<br>
Path: <?= htmlspecialchars($downloadResult['filepath']) ?><br>
Endpoint: <code><?= htmlspecialchars($downloadResult['endpoint']) ?></code>
<?php else: ?>
<strong>❌ Download Failed:</strong> <?= htmlspecialchars($downloadResult['error']) ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if (isset($downloadAttempts)): ?>
<div class="section" style="background: #e8f4f8; border: 2px solid #17a2b8;">
<h2>📥 Quick Download</h2>
<p>Click a successful endpoint below to download the file:</p>
<div style="margin: 15px 0;">
<?php
$hasSuccess = false;
foreach ($downloadAttempts as $name => $attempt):
if ($attempt['success']):
$hasSuccess = true;
?>
<a href="?action=download&endpoint=<?= urlencode($attempt['url']) ?>&download_id=<?= urlencode($assetId) ?>&asset_id=<?= urlencode($assetId) ?>"
style="display: inline-block; padding: 12px 24px; background: #28a745; color: white; text-decoration: none; border-radius: 6px; margin: 5px;">
📥 Download via "<?= htmlspecialchars($name) ?>" (<?= number_format($attempt['response_size']) ?> bytes)
</a>
<?php
endif;
endforeach;
?>
<?php if (!$hasSuccess): ?>
<p style="color: #dc3545; margin: 10px 0;">⚠️ No successful endpoints found. File may not exist in storage.</p>
<?php endif; ?>
</div>
</div>
<div class="section">
<h2>Download Endpoint Tests</h2>
<table>
<thead>
<tr>
<th>Endpoint</th>
<th>URL</th>
<th>HTTP Code</th>
<th>Response Size</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($downloadAttempts as $name => $attempt): ?>
<tr>
<td><strong><?= htmlspecialchars($name) ?></strong></td>
<td><code><?= htmlspecialchars($attempt['url']) ?></code></td>
<td><?= $attempt['http_code'] ?? 'N/A' ?></td>
<td><?= number_format($attempt['response_size'] ?? 0) ?> bytes</td>
<td>
<?php if (isset($attempt['success']) && $attempt['success'] && !($attempt['is_json_error'] ?? false)): ?>
<span class="success">✅ SUCCESS</span>
<?php elseif (isset($attempt['note'])): ?>
<span><?= htmlspecialchars($attempt['note']) ?></span>
<?php else: ?>
<span class="failure">❌ FAILED</span>
<?php if ($attempt['is_json_error'] ?? false): ?>
(JSON Error Response)
<?php endif; ?>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
<?php if ($assetDetails): ?>
<div class="section">
<h2>Asset Details</h2>
<details>
<summary><strong>Full Asset JSON (click to expand)</strong></summary>
<pre><?= htmlspecialchars(json_encode($assetDetails, JSON_PRETTY_PRINT)) ?></pre>
</details>
</div>
<?php endif; ?>
<?php if ($renditions): ?>
<div class="section">
<h2>Available Renditions</h2>
<pre><?= htmlspecialchars(json_encode($renditions, JSON_PRETTY_PRINT)) ?></pre>
</div>
<?php endif; ?>
</div>
</body>
</html>