migration to rustfs
This commit is contained in:
+37
-12
@@ -1,29 +1,42 @@
|
||||
import crypto from "crypto";
|
||||
import sharp from "sharp";
|
||||
import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
|
||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||
|
||||
import { AppError } from "@/lib/action-result";
|
||||
import { getMinioClient } from "@/lib/minio";
|
||||
import { BUCKET, getInternalClient, getPublicClient } from "@/lib/storage";
|
||||
|
||||
const BUCKET = "uploads";
|
||||
const MAX_FILE_SIZE = 5 * 1024 * 1024;
|
||||
const ALLOWED_INPUT_FORMATS = new Set(["jpeg", "png", "webp"]);
|
||||
|
||||
export async function uploadImage(file: File, directory: string, options: { maxWidth?: number; maxHeight?: number; fit?: "inside" | "cover" }): Promise<string> {
|
||||
const DIRECTORY_RE = /^[a-z0-9][a-z0-9_-]*(\/[a-z0-9][a-z0-9_-]*)*$/;
|
||||
const KEY_RE = /^[a-z0-9][a-z0-9_-]*(\/[a-z0-9][a-z0-9_-]*)*\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.webp$/;
|
||||
|
||||
const URL_WINDOW_SEC = 60 * 60;
|
||||
const URL_EXPIRES_SEC = URL_WINDOW_SEC * 2;
|
||||
|
||||
export async function uploadImage(
|
||||
file: File,
|
||||
directory: string,
|
||||
options: { maxWidth?: number; maxHeight?: number; fit?: "inside" | "cover" } = {},
|
||||
): Promise<string> {
|
||||
if (!DIRECTORY_RE.test(directory)) throw new Error("Invalid upload directory");
|
||||
if (file.size === 0) throw new AppError("A file is required");
|
||||
if (file.size > MAX_FILE_SIZE) throw new AppError("Image must be smaller than 5MB");
|
||||
|
||||
const input = Buffer.from(await file.arrayBuffer());
|
||||
if (input.length > MAX_FILE_SIZE) throw new AppError("Image must be smaller than 5MB");
|
||||
|
||||
const image = sharp(input, {
|
||||
failOn: "error",
|
||||
limitInputPixels: 30_000_000,
|
||||
sequentialRead: true,
|
||||
});
|
||||
|
||||
const metadata = await image.metadata();
|
||||
if (!metadata.format || !ALLOWED_INPUT_FORMATS.has(metadata.format)) {
|
||||
throw new AppError("Only JPEG, PNG, or WEBP images are allowed");
|
||||
}
|
||||
|
||||
if (!metadata.width || !metadata.height || metadata.width < 100 || metadata.height < 100) {
|
||||
throw new AppError("Image is too small");
|
||||
}
|
||||
@@ -41,19 +54,31 @@ export async function uploadImage(file: File, directory: string, options: { maxW
|
||||
.webp({ quality: 80, effort: 4 })
|
||||
.toBuffer();
|
||||
|
||||
const key = `${directory}/${crypto.randomUUID()}.webp`;
|
||||
await getMinioClient().putObject(BUCKET, key, output, output.length, { "Content-Type": "image/webp" });
|
||||
const key = `${directory}/${crypto.randomUUID()}.webp`;
|
||||
await getInternalClient().send(
|
||||
new PutObjectCommand({
|
||||
Bucket: BUCKET,
|
||||
Key: key,
|
||||
Body: output,
|
||||
ContentLength: output.length,
|
||||
ContentType: "image/webp",
|
||||
CacheControl: "public, max-age=31536000, immutable",
|
||||
}),
|
||||
);
|
||||
return key;
|
||||
}
|
||||
|
||||
export async function removeImage(key: string) {
|
||||
await getMinioClient().removeObject(BUCKET, key)
|
||||
if (!KEY_RE.test(key)) throw new Error("Invalid image key");
|
||||
await getInternalClient().send(new DeleteObjectCommand({ Bucket: BUCKET, Key: key }));
|
||||
}
|
||||
|
||||
export async function getImageUrl(key: string): Promise<string> {
|
||||
return getMinioClient().presignedGetObject(
|
||||
BUCKET,
|
||||
key,
|
||||
60 * 5
|
||||
);
|
||||
if (!KEY_RE.test(key)) throw new Error("Invalid image key");
|
||||
const windowMs = URL_WINDOW_SEC * 1000;
|
||||
const signingDate = new Date(Math.floor(Date.now() / windowMs) * windowMs);
|
||||
return getSignedUrl(getPublicClient(), new GetObjectCommand({ Bucket: BUCKET, Key: key }), {
|
||||
expiresIn: URL_EXPIRES_SEC,
|
||||
signingDate,
|
||||
});
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { Client } from "minio";
|
||||
|
||||
let _client: Client | null = null;
|
||||
|
||||
export function getMinioClient(): Client {
|
||||
if (!_client) {
|
||||
const url = new URL(process.env.MINIO_PUBLIC_URL!);
|
||||
_client = new Client({
|
||||
endPoint: url.hostname,
|
||||
port: url.port ? Number(url.port) : 443,
|
||||
useSSL: url.protocol === "https:",
|
||||
accessKey: process.env.MINIO_ACCESS_KEY!,
|
||||
secretKey: process.env.MINIO_SECRET_KEY!,
|
||||
});
|
||||
}
|
||||
return _client;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import "server-only";
|
||||
import { S3Client } from "@aws-sdk/client-s3";
|
||||
|
||||
function env(name: string): string {
|
||||
const value = process.env[name];
|
||||
if (!value) throw new Error(`Missing environment variable: ${name}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
export const BUCKET = process.env.RUSTFS_BUCKET ?? "uploads";
|
||||
const REGION = process.env.RUSTFS_REGION ?? "us-east-1";
|
||||
|
||||
let internalClient: S3Client | null = null;
|
||||
let publicClient: S3Client | null = null;
|
||||
|
||||
const credentials = () => ({
|
||||
accessKeyId: env("RUSTFS_ACCESS_KEY"),
|
||||
secretAccessKey: env("RUSTFS_SECRET_KEY"),
|
||||
});
|
||||
|
||||
|
||||
export function getInternalClient(): S3Client {
|
||||
internalClient ??= new S3Client({
|
||||
endpoint: `http://${env("RUSTFS_INTERNAL_HOST")}:${process.env.RUSTFS_INTERNAL_PORT ?? 9000}`,
|
||||
region: REGION,
|
||||
credentials: credentials(),
|
||||
forcePathStyle: true,
|
||||
requestChecksumCalculation: "WHEN_REQUIRED",
|
||||
responseChecksumValidation: "WHEN_REQUIRED",
|
||||
maxAttempts: 3,
|
||||
requestHandler: { connectionTimeout: 3_000, requestTimeout: 15_000 },
|
||||
});
|
||||
return internalClient;
|
||||
}
|
||||
|
||||
export function getPublicClient(): S3Client {
|
||||
publicClient ??= new S3Client({
|
||||
endpoint: new URL(env("RUSTFS_PUBLIC_URL")).origin,
|
||||
region: REGION,
|
||||
credentials: credentials(),
|
||||
forcePathStyle: true,
|
||||
requestChecksumCalculation: "WHEN_REQUIRED",
|
||||
responseChecksumValidation: "WHEN_REQUIRED",
|
||||
});
|
||||
return publicClient;
|
||||
}
|
||||
Reference in New Issue
Block a user