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

99 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-11-11 19:31:32 +01:00
"use server"
2024-12-16 11:55:16 +01:00
2024-11-11 19:31:32 +01:00
import {userAction} from "@/safe-actions";
import {z} from "zod";
import {v4 as uuidv4} from 'uuid';
2024-11-23 16:49:16 +01:00
import {mkdir, writeFile} from "fs/promises";
2024-11-11 19:31:32 +01:00
import path from "path";
import {env} from "@/env.mjs";
2024-11-23 16:49:16 +01:00
import {checkMinioAlive, createPublicBucket, saveFileInBucket} from "@/utils/s3-file-management";
import {prisma} from "@/prisma";
import {UploadedObjectInfo} from "minio/src/internal/type";
import {Settings} from "@prisma/client";
2025-01-03 21:47:22 +01:00
import {getServerUrl} from "@/utils/get-server-url";
2024-11-11 19:31:32 +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(".").slice(-1)[0]
const fileName = uuid + "." + fileFormat
const arrayBuffer = await file.arrayBuffer()
const buffer = Buffer.from(arrayBuffer)
2024-11-23 16:49:16 +01:00
const settings = await prisma.settings.findUnique({
where:{
name: "system"
}
})
2024-11-17 21:01:05 +01:00
2024-11-23 16:49:16 +01:00
let result: void | UploadedObjectInfo;
const bucketName = 'public-image-bucket';
2024-11-17 21:01:05 +01:00
2024-11-23 16:49:16 +01:00
if(settings.storage === "local") {
result = await uploadLocal(fileName,buffer)
}else if (settings.storage === "s3"){
result = await uploadS3Compatible(bucketName,fileName, buffer)
}
const url = getUrl(fileName, settings, bucketName)
console.log(url)
2024-11-17 21:01:05 +01:00
return {
data: {result: result, url: url},
2024-11-11 19:31:32 +01:00
}
});
2024-11-23 16:49:16 +01:00
function getUrl(fileName:string, settings: Settings, bucketName: string):string {
2024-11-17 21:01:05 +01:00
if (env.NODE_ENV === "production") {
2025-01-03 21:47:22 +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-01-03 21:47:22 +01:00
}
2024-11-17 21:01:05 +01:00
} else {
2024-11-23 16:49:16 +01:00
if(settings.storage === "s3"){
return `http://localhost:${env.S3_PORT}/${bucketName}/${fileName}`
}else if(settings.storage === "local"){
2025-01-03 21:47:22 +01:00
const url = getServerUrl()
return `${url}/api/images/${fileName}`
2024-11-23 16:49:16 +01:00
}
2024-11-17 21:01:05 +01: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/images/"
2024-11-17 21:01:05 +01:00
try {
await mkdir(path.join(process.cwd(), localDir), { recursive: true });
2024-11-23 16:49:16 +01:00
return await writeFile(
2024-11-17 21:01:05 +01:00
path.join(process.cwd(), localDir + fileName),
buffer
2024-11-23 16:49:16 +01:00
)
2024-11-17 21:01:05 +01:00
} catch (error) {
console.log("Error occured ", error);
throw new Error('An error occured while importing image');
}
}
2024-11-23 16:49:16 +01:00
async function uploadS3Compatible(bucketName: string, fileName: string, buffer: any){
await createPublicBucket({bucketName});
return await saveFileInBucket({
bucketName,
fileName,
file: buffer,
})
2024-11-17 21:01:05 +01:00
}
export async function checkConnexionToS3(){
return await checkMinioAlive()
}