From ea852cd22f3886204c64f69b89a99b119668f1d1 Mon Sep 17 00:00:00 2001 From: Ivan Rezchikov Date: Sat, 11 Jul 2026 22:14:18 +0300 Subject: [PATCH] fix --- lib/image.ts | 8 ++++---- lib/minio.ts | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/image.ts b/lib/image.ts index 8a46b08..a9041ab 100644 --- a/lib/image.ts +++ b/lib/image.ts @@ -2,7 +2,7 @@ import crypto from "crypto"; import sharp from "sharp"; import { AppError } from "@/lib/action-result"; -import { minioClient } from "@/lib/minio"; +import { getMinioClient } from "@/lib/minio"; const BUCKET = "uploads"; const MAX_FILE_SIZE = 5 * 1024 * 1024; @@ -42,16 +42,16 @@ export async function uploadImage(file: File, directory: string, options: { maxW .toBuffer(); const key = `${directory}/${crypto.randomUUID()}.webp`; - await minioClient.putObject(BUCKET, key, output, output.length, { "Content-Type": "image/webp" }); + await getMinioClient().putObject(BUCKET, key, output, output.length, { "Content-Type": "image/webp" }); return key; } export async function removeImage(key: string) { - await minioClient.removeObject(BUCKET, key) + await getMinioClient().removeObject(BUCKET, key) } export async function getImageUrl(key: string): Promise { - return minioClient.presignedGetObject( + return getMinioClient().presignedGetObject( BUCKET, key, 60 * 5 diff --git a/lib/minio.ts b/lib/minio.ts index 0e86389..47c416f 100644 --- a/lib/minio.ts +++ b/lib/minio.ts @@ -1,9 +1,16 @@ import { Client } from "minio"; -export const minioClient = new Client({ - endPoint: process.env.MINIO_URL!, - port: Number(process.env.MINIO_PORT), - useSSL: false, - accessKey: process.env.MINIO_ACCESS_KEY!, - secretKey: process.env.MINIO_SECRET_KEY! -}) \ No newline at end of file +let _client: Client | null = null; + +export function getMinioClient(): Client { + if (!_client) { + _client = new Client({ + endPoint: process.env.MINIO_URL!, + port: Number(process.env.MINIO_PORT), + useSSL: false, + accessKey: process.env.MINIO_ACCESS_KEY!, + secretKey: process.env.MINIO_SECRET_KEY! + }); + } + return _client; +}