- Moved uploads from Lucee webroot to /opt/payfrit-api/uploads/ - Updated nginx on both dev and biz to alias /uploads/ to new path - Replaced luceeWebroot() with uploadsRoot() helper - Temp files now use /opt/payfrit-api/temp/ - No more /opt/lucee or /var/www/biz.payfrit.com references Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
133 lines
4 KiB
PHP
133 lines
4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../helpers.php';
|
|
runAuth();
|
|
|
|
/**
|
|
* Download Business Images
|
|
*
|
|
* Downloads logo and header images from external URLs.
|
|
*
|
|
* POST JSON:
|
|
* {
|
|
* "businessID": 123,
|
|
* "logoUrl": "https://example.com/logo.png",
|
|
* "headerUrl": "https://example.com/header.jpg"
|
|
* }
|
|
*/
|
|
|
|
$response = ['OK' => false, 'downloaded' => []];
|
|
|
|
try {
|
|
$data = readJsonBody();
|
|
|
|
$businessID = (int)($data['businessID'] ?? 0);
|
|
if ($businessID === 0) {
|
|
throw new Exception('businessID is required');
|
|
}
|
|
|
|
$uploadsPath = uploadsRoot();
|
|
|
|
$logosPath = "$uploadsPath/logos";
|
|
$headersPath = "$uploadsPath/headers";
|
|
|
|
if (!is_dir($logosPath)) mkdir($logosPath, 0755, true);
|
|
if (!is_dir($headersPath)) mkdir($headersPath, 0755, true);
|
|
|
|
// Download logo
|
|
if (!empty($data['logoUrl'])) {
|
|
$logoUrl = $data['logoUrl'];
|
|
$ext = '.png';
|
|
if (stripos($logoUrl, '.jpg') !== false || stripos($logoUrl, '.jpeg') !== false) $ext = '.jpg';
|
|
elseif (stripos($logoUrl, '.gif') !== false) $ext = '.gif';
|
|
elseif (stripos($logoUrl, '.webp') !== false) $ext = '.webp';
|
|
|
|
$logoFile = "$logosPath/$businessID$ext";
|
|
|
|
try {
|
|
$ch = curl_init($logoUrl);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
]);
|
|
$content = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode === 200 && $content !== false) {
|
|
file_put_contents($logoFile, $content);
|
|
$response['downloaded'][] = [
|
|
'type' => 'logo',
|
|
'url' => $logoUrl,
|
|
'savedTo' => "/uploads/logos/$businessID$ext",
|
|
'size' => strlen($content),
|
|
];
|
|
} else {
|
|
$response['downloaded'][] = [
|
|
'type' => 'logo',
|
|
'url' => $logoUrl,
|
|
'error' => "HTTP $httpCode",
|
|
];
|
|
}
|
|
} catch (Exception $e) {
|
|
$response['downloaded'][] = [
|
|
'type' => 'logo',
|
|
'url' => $logoUrl,
|
|
'error' => $e->getMessage(),
|
|
];
|
|
}
|
|
}
|
|
|
|
// Download header
|
|
if (!empty($data['headerUrl'])) {
|
|
$headerUrl = $data['headerUrl'];
|
|
$ext = '.jpg';
|
|
if (stripos($headerUrl, '.png') !== false) $ext = '.png';
|
|
elseif (stripos($headerUrl, '.gif') !== false) $ext = '.gif';
|
|
elseif (stripos($headerUrl, '.webp') !== false) $ext = '.webp';
|
|
|
|
$headerFile = "$headersPath/$businessID$ext";
|
|
|
|
try {
|
|
$ch = curl_init($headerUrl);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
]);
|
|
$content = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode === 200 && $content !== false) {
|
|
file_put_contents($headerFile, $content);
|
|
$response['downloaded'][] = [
|
|
'type' => 'header',
|
|
'url' => $headerUrl,
|
|
'savedTo' => "/uploads/headers/$businessID$ext",
|
|
'size' => strlen($content),
|
|
];
|
|
} else {
|
|
$response['downloaded'][] = [
|
|
'type' => 'header',
|
|
'url' => $headerUrl,
|
|
'error' => "HTTP $httpCode",
|
|
];
|
|
}
|
|
} catch (Exception $e) {
|
|
$response['downloaded'][] = [
|
|
'type' => 'header',
|
|
'url' => $headerUrl,
|
|
'error' => $e->getMessage(),
|
|
];
|
|
}
|
|
}
|
|
|
|
$response['OK'] = true;
|
|
$response['businessID'] = $businessID;
|
|
|
|
} catch (Exception $e) {
|
|
$response['error'] = $e->getMessage();
|
|
}
|
|
|
|
jsonResponse($response);
|