Working on Avatar upload image.

This commit is contained in:
charles-gauthereau
2024-11-11 19:31:32 +01:00
parent 55c5a35918
commit ec8cb3871b
4 changed files with 135 additions and 16 deletions
+50
View File
@@ -0,0 +1,50 @@
"use server"
import {userAction} from "@/safe-actions";
import {z} from "zod";
import {v4 as uuidv4} from 'uuid';
import { writeFile } from "fs/promises";
import path from "path";
import {env} from "@/env.mjs";
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)
const localDir = "public/uploads/"
try {
const result = await writeFile(
path.join(process.cwd(), localDir + fileName),
buffer
);
let url: string = "";
if (env.NODE_ENV === "production") {
// url = `https://${env.S3_ENDPOINT}/${bucketName}/${fileName}`
} else {
url = `http://localhost:8887/${localDir}${fileName}`
}
return {
data: {result: result, url: url},
}
} catch (error) {
console.log("Error occured ", error);
throw new Error('An error occured while importing image');
}
});