merge(stg): merge main into stg and resolve storage conflicts

This commit is contained in:
2026-04-23 13:48:26 +08:00
36 changed files with 17763 additions and 233 deletions

View File

@@ -1,27 +1,21 @@
import fs from "fs/promises";
import path from "path";
import fs from "fs/promises";
import sharp from "sharp";
import minio, { MINIO_BUCKET } from "@/lib/minio";
async function img({
name,
UPLOAD_DIR_IMAGE,
ROOT,
size,
}: {
name: string;
UPLOAD_DIR_IMAGE: string;
ROOT: string;
size?: number; // Ukuran opsional (tidak ada default)
size?: number;
}) {
const completeName = path.basename(name); // Nama file lengkap
const ext = path.extname(name).toLowerCase(); // Ekstensi file dalam huruf kecil
// const fileNameWithoutExt = path.basename(name, ext); // Nama file tanpa ekstensi
// Default image jika terjadi kesalahan
const noImage = path.join(ROOT, "public/no-image.jpg");
const ext = path.extname(name).toLowerCase();
// Validasi ekstensi file
if (![".jpg", ".jpeg", ".png"].includes(ext)) {
if (![".jpg", ".jpeg", ".png", ".webp"].includes(ext)) {
console.warn(`Ekstensi file tidak didukung: ${ext}`);
const buffer = await fs.readFile(noImage);
const uint8Array = new Uint8Array(buffer);
@@ -31,33 +25,27 @@ async function img({
}
try {
// Path ke file asli
const filePath = path.join(UPLOAD_DIR_IMAGE, completeName);
const stream = await minio.getObject(MINIO_BUCKET, `image/${name}`);
const chunks: Buffer[] = [];
for await (const chunk of stream) {
chunks.push(Buffer.from(chunk));
}
const buffer = Buffer.concat(chunks);
// Periksa apakah file ada
await fs.stat(filePath);
// Metadata gambar asli
const metadata = await sharp(filePath).metadata();
// Proses resize menggunakan sharp
const resizedImageBuffer = await sharp(filePath)
.resize(size || metadata.width) // Gunakan size jika diberikan, jika tidak gunakan width asli
const metadata = await sharp(buffer).metadata();
const resized = await sharp(buffer)
.resize(size || metadata.width)
.toBuffer();
const uint8Array = new Uint8Array(resizedImageBuffer);
return new Response(new Blob([uint8Array], { type: 'image/jpeg' }), {
return new Response(resized, {
headers: {
"Cache-Control": "public, max-age=3600, stale-while-revalidate=600",
"Content-Type": "image/jpeg",
},
});
} catch (error) {
console.error(`Gagal memproses file: ${name}`, error);
// Jika file tidak ditemukan atau gagal diproses, kembalikan default image
const buffer = await fs.readFile(noImage);
const uint8Array = new Uint8Array(buffer);
return new Response(new Blob([uint8Array], { type: 'image/jpeg' }), {
console.error(`Gagal mengambil file dari MinIO: ${name}`, error);
return new Response(await fs.readFile(noImage), {
headers: { "Content-Type": "image/jpeg" },
});
}