Files
portabase/src/features/upload/public/upload.action.ts
T

87 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-05-16 15:54:17 +02:00
"use server";
2025-09-23 20:35:48 +02:00
import {userAction} from "@/lib/safe-actions/actions";
2025-11-01 19:38:05 +01:00
import {z} from "zod";
import {v4 as uuidv4} from "uuid";
import {mkdir, writeFile} from "fs/promises";
2024-11-11 19:31:32 +01:00
import path from "path";
2025-11-01 19:38:05 +01:00
import {env} from "@/env.mjs";
import {checkMinioAlive, saveFileInBucket} from "@/utils/s3-file-management";
2025-05-16 15:54:17 +02:00
//@ts-ignore
2025-11-01 19:38:05 +01:00
import {UploadedObjectInfo} from "minio/src/internal/type";
import {getServerUrl} from "@/utils/get-server-url";
import {db} from "@/db";
import {eq} from "drizzle-orm";
import {Setting} from "@/db/schema/01_setting";
2025-07-16 12:36:11 +02:00
import * as drizzleDb from "@/db";
2024-11-11 19:31:32 +01:00
const imageDir = "images/";
2025-05-16 15:54:17 +02:00
2025-11-01 19:38:05 +01:00
export const uploadImageAction = userAction
.schema(z.instanceof(FormData))
.action(async ({parsedInput: formData, ctx}) => {
const file = formData.get("file") as File;
const uuid = uuidv4();
const fileFormat = file.name.split(".").pop();
const arrayBuffer = await file.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const [settings] = await db
.select()
.from(drizzleDb.schemas.setting)
.where(eq(drizzleDb.schemas.setting.name, "system"))
.limit(1);
if (!settings) throw new Error("System settings not found.");
let fileName: string | null = null;
let result: void | UploadedObjectInfo;
if (settings.storage === "local") {
fileName = `${imageDir}${uuid}.${fileFormat}`;
2025-11-01 19:38:05 +01:00
result = await uploadLocal(fileName, buffer);
} else if (settings.storage === "s3") {
fileName = `${imageDir}${uuid}.${fileFormat}`;
2025-11-01 19:38:05 +01:00
result = await uploadS3Compatible(env.S3_BUCKET_NAME ?? "", fileName, buffer);
} else {
throw new Error(`Unsupported storage type: ${settings.storage}`);
}
const url = `${getServerUrl()}/api/${fileName}`
return {data: {result, url}};
});
2025-05-16 15:54:17 +02:00
2024-11-11 19:31:32 +01:00
2024-11-23 16:49:16 +01:00
async function uploadLocal(fileName: string, buffer: any) {
const localDir = "private/uploads/";
2024-11-17 21:01:05 +01:00
try {
2025-11-01 19:38:05 +01:00
await mkdir(path.join(process.cwd(), localDir), {recursive: true});
2025-05-16 15:54:17 +02:00
return await writeFile(path.join(process.cwd(), localDir + fileName), buffer);
2024-11-17 21:01:05 +01:00
} catch (error) {
2025-05-16 15:54:17 +02:00
throw new Error("An error occured while importing image");
2024-11-17 21:01:05 +01:00
}
}
2025-05-16 15:54:17 +02:00
async function uploadS3Compatible(bucketName: string, fileName: string, buffer: any) {
2024-11-23 16:49:16 +01:00
return await saveFileInBucket({
bucketName,
fileName,
file: buffer,
2025-05-16 15:54:17 +02:00
});
2024-11-17 21:01:05 +01:00
}
2025-05-16 15:54:17 +02:00
function getUrl(fileName: string, settings: Setting, bucketName: string): string {
2025-11-01 19:38:05 +01:00
if (settings.storage === "s3") {
return `https://${env.S3_ENDPOINT}/${bucketName}/${fileName}`;
} else if (settings.storage === "local") {
const url = getServerUrl();
return `${url}/api/images/${fileName}`;
2025-05-16 15:54:17 +02:00
}
throw new Error("Invalid storage configuration");
}
2024-11-17 21:01:05 +01:00
2025-05-16 15:54:17 +02:00
export async function checkConnexionToS3() {
return await checkMinioAlive();
}