Pisah server penyimpanan dan update payment gateway
This commit is contained in:
@@ -1,12 +1,85 @@
|
||||
<?php
|
||||
header("Content-Type: application/json");
|
||||
// header("Content-Type: application/json");
|
||||
|
||||
// direktori upload
|
||||
$uploadDir = __DIR__ . "/../img/pesan/";
|
||||
if (!file_exists($uploadDir)) {
|
||||
mkdir($uploadDir, 0777, true);
|
||||
// // 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;
|
||||
@@ -21,13 +94,36 @@ if (!in_array($ext, $allowed)) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$newName = uniqid("img_") . "." . $ext;
|
||||
$target = $uploadDir . $newName;
|
||||
// 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';
|
||||
}
|
||||
|
||||
if (move_uploaded_file($file['tmp_name'], $target)) {
|
||||
$url = "https://manjapro.net/img/pesan/" . $newName;
|
||||
// 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 move file"]);
|
||||
echo json_encode(["status" => "error", "message" => "Failed to upload to MinIO"]);
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user