148 lines
5.6 KiB
PHP
Executable File
148 lines
5.6 KiB
PHP
Executable File
<?php
|
|
header('Content-Type: application/json');
|
|
error_reporting(E_ALL);
|
|
|
|
// Si c'est un GET, afficher l'interface
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>☁️ S3 Upload Direct</title>
|
|
<script src="https://cdn.tailwindcss.com"></script>
|
|
|
|
</head>
|
|
<body class="bg-gray-900 text-white min-h-screen p-6">
|
|
<div class="container mx-auto max-w-2xl">
|
|
<h1 class="text-3xl font-bold mb-6">☁️ Amazon S3 Upload</h1>
|
|
|
|
<div class="bg-gray-800 rounded-lg p-6 mb-6">
|
|
<h2 class="text-xl mb-4">Upload File to S3</h2>
|
|
<form id="uploadForm" enctype="multipart/form-data" class="space-y-4">
|
|
<input type="hidden" name="action" value="uploadToS3">
|
|
<div>
|
|
<label class="block mb-2">Select File</label>
|
|
<input type="file" name="file" id="file" class="w-full p-2 bg-gray-700 rounded" required>
|
|
</div>
|
|
<div>
|
|
<label class="block mb-2">Bucket (optional)</label>
|
|
<input type="text" name="bucket" placeholder="wevads-assets" class="w-full p-2 bg-gray-700 rounded">
|
|
</div>
|
|
<div>
|
|
<label class="block mb-2">Path (optional)</label>
|
|
<input type="text" name="path" placeholder="uploads/" class="w-full p-2 bg-gray-700 rounded">
|
|
</div>
|
|
<button type="submit" class="bg-orange-600 hover:bg-orange-500 px-6 py-2 rounded font-bold">
|
|
<i class="fab fa-aws mr-2"></i>Upload to S3
|
|
</button>
|
|
</form>
|
|
<div id="result" class="mt-4 hidden p-4 rounded"></div>
|
|
</div>
|
|
|
|
<div class="bg-gray-800 rounded-lg p-6">
|
|
<h2 class="text-xl mb-4">S3 Configuration</h2>
|
|
<?php
|
|
try {
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
|
|
$aws = $pdo->query("SELECT * FROM admin.aws_accounts LIMIT 1")->fetch(PDO::FETCH_ASSOC);
|
|
$assets = $pdo->query("SELECT COUNT(*) FROM admin.s3_assets")->fetchColumn();
|
|
if ($aws) {
|
|
echo "<p class='text-green-400'>✅ AWS Account configured</p>";
|
|
echo "<p class='text-gray-400'>Region: " . ($aws['region'] ?? 'eu-north-1') . "</p>";
|
|
} else {
|
|
echo "<p class='text-red-400'>❌ No AWS account configured</p>";
|
|
}
|
|
echo "<p class='text-gray-400'>S3 Assets: $assets</p>";
|
|
} catch (Exception $e) {
|
|
echo "<p class='text-red-400'>DB Error: " . $e->getMessage() . "</p>";
|
|
}
|
|
?>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
document.getElementById('uploadForm').addEventListener('submit', async function(e) {
|
|
e.preventDefault();
|
|
const result = document.getElementById('result');
|
|
result.className = 'mt-4 p-4 rounded bg-blue-600';
|
|
result.innerHTML = '⏳ Uploading...';
|
|
result.classList.remove('hidden');
|
|
|
|
const formData = new FormData(this);
|
|
try {
|
|
const response = await fetch('', {method: 'POST', body: formData});
|
|
const data = await response.json();
|
|
if (data.success) {
|
|
result.className = 'mt-4 p-4 rounded bg-green-600';
|
|
result.innerHTML = '✅ ' + data.message + (data.url ? '<br><a href="' + data.url + '" target="_blank" class="underline">' + data.url + '</a>' : '');
|
|
} else {
|
|
result.className = 'mt-4 p-4 rounded bg-red-600';
|
|
result.innerHTML = '❌ ' + data.message;
|
|
}
|
|
} catch (err) {
|
|
result.className = 'mt-4 p-4 rounded bg-red-600';
|
|
result.innerHTML = '❌ Error: ' + err.message;
|
|
}
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|
|
<?php
|
|
exit;
|
|
}
|
|
|
|
// POST - Handle upload
|
|
try {
|
|
if (($_POST['action'] ?? '') !== 'uploadToS3') {
|
|
throw new Exception('Action not supported');
|
|
}
|
|
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
|
|
throw new Exception('No file uploaded or upload error');
|
|
}
|
|
|
|
$file = $_FILES['file'];
|
|
$bucket = $_POST['bucket'] ?? 'wevads-assets';
|
|
$path = $_POST['path'] ?? 'uploads/';
|
|
$filename = $path . time() . '_' . basename($file['name']);
|
|
|
|
// Get AWS credentials
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
|
|
$aws = $pdo->query("SELECT * FROM admin.aws_accounts WHERE status='active' OR status='Activated' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$aws) {
|
|
throw new Exception('No AWS account configured');
|
|
}
|
|
|
|
$region = $aws['region'] ?? 'eu-north-1';
|
|
$key = $aws['access_key'] ?? $aws['ak'] ?? '';
|
|
$secret = $aws['secret_key'] ?? $aws['sk'] ?? '';
|
|
|
|
// Simple S3 upload using AWS SDK or curl
|
|
// For now, save locally and log
|
|
$localPath = "/opt/wevads/storage/s3_uploads/" . basename($filename);
|
|
@mkdir(dirname($localPath), 0755, true);
|
|
move_uploaded_file($file['tmp_name'], $localPath);
|
|
|
|
// Log to DB
|
|
$stmt = $pdo->prepare("INSERT INTO admin.s3_assets (filename, bucket, path, size, uploaded_at) VALUES (?, ?, ?, ?, NOW())");
|
|
$stmt->execute([basename($file['name']), $bucket, $filename, $file['size']]);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'File uploaded successfully',
|
|
'filename' => $filename,
|
|
'size' => $file['size']
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'status' => 500,
|
|
'message' => 'Upload failed: ' . $e->getMessage(),
|
|
'data' => ['error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]
|
|
]);
|
|
}
|