Files
manjapro_v6/controller/upload_image_pesan_siaran.php

130 lines
3.5 KiB
PHP

<?php
// header("Content-Type: application/json");
// // direktori upload
// $uploadDir = __DIR__ . "/../img/pesan/";
// if (!file_exists($uploadDir)) {
// mkdir($uploadDir, 0777, true);
// }
// if (!isset($_FILES['file'])) {
// echo json_encode(["status" => "error", "message" => "No file uploaded"]);
// exit;
// }
// $file = $_FILES['file'];
// $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
// $allowed = ['jpg', 'jpeg', 'png', 'gif'];
// if (!in_array($ext, $allowed)) {
// echo json_encode(["status" => "error", "message" => "Invalid file type"]);
// exit;
// }
// $newName = uniqid("img_") . "." . $ext;
// $target = $uploadDir . $newName;
// if (move_uploaded_file($file['tmp_name'], $target)) {
// $url = "https://manjapro.net/img/pesan/" . $newName;
// echo json_encode(["status" => "success", "url" => $url]);
// } else {
// echo json_encode(["status" => "error", "message" => "Failed to move file"]);
// }
header("Content-Type: application/json");
require_once __DIR__ . '/../minio/vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
/**
* Upload file langsung ke MinIO storage
*/
function uploadToMinioStorageRawPesan($fileData, $objectPath, $contentType)
{
$minio = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => 'https://storage.manjapro.net',
'use_path_style_endpoint' => true,
'credentials' => [
'key' => 'admin', // Ganti sesuai kredensial kamu
'secret' => '@P4ssw0rd', // Ganti sesuai kredensial kamu
],
'suppress_php_deprecation_warning' => true
]);
$bucket = 'files';
try {
$result = $minio->putObject([
'Bucket' => $bucket,
'Key' => $objectPath,
'Body' => $fileData,
'ContentType' => $contentType,
'ACL' => 'public-read',
]);
return $result['ObjectURL'];
} catch (AwsException $e) {
error_log('Upload MinIO gagal: ' . $e->getMessage());
return false;
} catch (Exception $e) {
error_log('Kesalahan umum: ' . $e->getMessage());
return false;
}
}
// ------------------------------------------------------------
// PROSES UPLOAD
// ------------------------------------------------------------
if (!isset($_FILES['file'])) {
echo json_encode(["status" => "error", "message" => "No file uploaded"]);
exit;
}
$file = $_FILES['file'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowed = ['jpg', 'jpeg', 'png', 'gif'];
if (!in_array($ext, $allowed)) {
echo json_encode(["status" => "error", "message" => "Invalid file type"]);
exit;
}
// Tentukan Content-Type
switch ($ext) {
case 'jpg':
case 'jpeg':
$contentType = 'image/jpeg';
break;
case 'png':
$contentType = 'image/png';
break;
case 'gif':
$contentType = 'image/gif';
break;
default:
$contentType = 'application/octet-stream';
}
// Buat nama baru unik
$newName = uniqid("img_") . "." . $ext;
$objectPath = "pesan/" . $newName;
// Baca isi file dari temporary upload
$fileData = file_get_contents($file['tmp_name']);
// Upload langsung ke MinIO
$url = uploadToMinioStorageRawPesan($fileData, $objectPath, $contentType);
// Hasil respon
if ($url) {
echo json_encode(["status" => "success", "url" => $url]);
} else {
echo json_encode(["status" => "error", "message" => "Failed to upload to MinIO"]);
}
?>