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,15 +1,8 @@
import fs from "fs/promises";
import path from "path";
import minio, { MINIO_BUCKET } from "@/lib/minio";
async function imgDel({
name,
UPLOAD_DIR_IMAGE,
}: {
name: string;
UPLOAD_DIR_IMAGE: string;
}) {
async function imgDel({ name }: { name: string }) {
try {
await fs.unlink(path.join(UPLOAD_DIR_IMAGE, name));
await minio.removeObject(MINIO_BUCKET, `image/${name}`);
return "ok";
} catch (error) {
console.log(error);

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" },
});
}

View File

@@ -1,30 +1,30 @@
import fs from "fs/promises";
import minio, { MINIO_BUCKET } from "@/lib/minio";
async function imgs({
search = "",
page = 1,
count = 20,
UPLOAD_DIR_IMAGE,
}: {
search?: string;
page?: number;
count?: number;
UPLOAD_DIR_IMAGE: string;
}) {
const files = await fs.readdir(UPLOAD_DIR_IMAGE);
const objects: { name: string; url: string }[] = [];
return files
.filter(
(file) =>
file.endsWith(".jpg") || file.endsWith(".png") || file.endsWith(".jpeg")
)
.filter((file) => file.includes(search))
const stream = minio.listObjects(MINIO_BUCKET, "image/", true);
for await (const obj of stream) {
if (!obj.name) continue;
const fileName = obj.name.replace("image/", "");
if (!fileName) continue;
if (search && !fileName.includes(search)) continue;
objects.push({ name: fileName, url: `/api/img/${fileName}` });
}
const total = objects.length;
return objects
.slice((page - 1) * count, page * count)
.map((file) => ({
name: file,
url: `/api/img/${file}`,
total: files.length,
}));
.map((o) => ({ ...o, total }));
}
export default imgs;

View File

@@ -1,30 +1,31 @@
import { nanoid } from "nanoid";
import fs from "fs/promises";
import path from "path";
import _ from "lodash";
import minio, { MINIO_BUCKET } from "@/lib/minio";
export async function uplImgSingle({
fileName,
file,
UPLOAD_DIR_IMAGE,
}: {
fileName: string;
file: File;
UPLOAD_DIR_IMAGE: string;
}) {
if (!fileName || typeof fileName !== "string" || fileName.trim() === "") {
console.warn(`Nama file tidak valid: ${fileName}`);
fileName = nanoid() + ".jpg";
}
const ext = path.extname(fileName).toLowerCase();
const fileNameWithoutExt = path.basename(fileName, ext);
const fileNameKebabCase = _.kebabCase(fileNameWithoutExt) + ext;
const objectName = `image/${fileNameKebabCase}`;
try {
const buffer = Buffer.from(await file.arrayBuffer());
const filePath = path.join(UPLOAD_DIR_IMAGE, fileNameKebabCase);
await fs.writeFile(filePath, buffer);
return filePath;
await minio.putObject(MINIO_BUCKET, objectName, buffer, buffer.length, {
"Content-Type": file.type || "image/jpeg",
});
return objectName;
} catch (error) {
console.log(error);
return "error";

View File

@@ -1,15 +1,11 @@
import path from "path";
import fs from "fs/promises";
import { nanoid } from "nanoid";
import minio, { MINIO_BUCKET } from "@/lib/minio";
async function uplImg({
files,
UPLOAD_DIR_IMAGE,
}: {
files: File[];
UPLOAD_DIR_IMAGE: string;
}) {
// Validasi input
function sanitizeFileName(fileName: string): string {
return fileName.replace(/[^a-zA-Z0-9._\-]/g, "_");
}
async function uplImg({ files }: { files: File[] }) {
if (!Array.isArray(files) || files.length === 0) {
throw new Error("Tidak ada file yang diunggah");
}
@@ -17,24 +13,20 @@ async function uplImg({
for (const file of files) {
let fileName = file.name;
// Validasi nama file
if (!fileName || typeof fileName !== "string" || fileName.trim() === "") {
console.warn(`Nama file tidak valid: ${fileName}`);
fileName = nanoid() + ".jpg";
}
// Sanitasi nama file untuk mencegah path traversal
const sanitizedFileName = sanitizeFileName(fileName);
const sanitized = sanitizeFileName(fileName);
const objectName = `image/${sanitized}`;
try {
// Konversi file ke buffer
const buffer = Buffer.from(await file.arrayBuffer());
// Tulis file ke direktori uploads
const filePath = path.join(UPLOAD_DIR_IMAGE, sanitizedFileName);
await fs.writeFile(filePath, buffer);
console.log(`File berhasil diunggah: ${sanitizedFileName}`);
await minio.putObject(MINIO_BUCKET, objectName, buffer, buffer.length, {
"Content-Type": file.type || "image/jpeg",
});
console.log(`File berhasil diunggah ke MinIO: ${objectName}`);
} catch (error) {
console.error(`Gagal mengunggah file ${fileName}:`, error);
throw new Error(`Gagal mengunggah file: ${fileName}`);
@@ -44,9 +36,4 @@ async function uplImg({
return "ok";
}
// Fungsi untuk membersihkan nama file dari karakter yang tidak aman
function sanitizeFileName(fileName: string): string {
return fileName.replace(/[^a-zA-Z0-9._\-]/g, "_");
}
export default uplImg;