This commit is contained in:
Ivan Rezchikov
2026-07-11 22:14:18 +03:00
parent 81576a0e37
commit ea852cd22f
2 changed files with 18 additions and 11 deletions
+4 -4
View File
@@ -2,7 +2,7 @@ import crypto from "crypto";
import sharp from "sharp"; import sharp from "sharp";
import { AppError } from "@/lib/action-result"; import { AppError } from "@/lib/action-result";
import { minioClient } from "@/lib/minio"; import { getMinioClient } from "@/lib/minio";
const BUCKET = "uploads"; const BUCKET = "uploads";
const MAX_FILE_SIZE = 5 * 1024 * 1024; const MAX_FILE_SIZE = 5 * 1024 * 1024;
@@ -42,16 +42,16 @@ export async function uploadImage(file: File, directory: string, options: { maxW
.toBuffer(); .toBuffer();
const key = `${directory}/${crypto.randomUUID()}.webp`; 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; return key;
} }
export async function removeImage(key: string) { export async function removeImage(key: string) {
await minioClient.removeObject(BUCKET, key) await getMinioClient().removeObject(BUCKET, key)
} }
export async function getImageUrl(key: string): Promise<string> { export async function getImageUrl(key: string): Promise<string> {
return minioClient.presignedGetObject( return getMinioClient().presignedGetObject(
BUCKET, BUCKET,
key, key,
60 * 5 60 * 5
+9 -2
View File
@@ -1,9 +1,16 @@
import { Client } from "minio"; import { Client } from "minio";
export const minioClient = new Client({ let _client: Client | null = null;
export function getMinioClient(): Client {
if (!_client) {
_client = new Client({
endPoint: process.env.MINIO_URL!, endPoint: process.env.MINIO_URL!,
port: Number(process.env.MINIO_PORT), port: Number(process.env.MINIO_PORT),
useSSL: false, useSSL: false,
accessKey: process.env.MINIO_ACCESS_KEY!, accessKey: process.env.MINIO_ACCESS_KEY!,
secretKey: process.env.MINIO_SECRET_KEY! secretKey: process.env.MINIO_SECRET_KEY!
}) });
}
return _client;
}