Working on S3 compatibility.

This commit is contained in:
charles-gauthereau
2024-11-17 21:01:05 +01:00
parent de4242f4ca
commit 9b38d68d51
16 changed files with 179 additions and 51 deletions
+51 -19
View File
@@ -5,6 +5,7 @@ import {v4 as uuidv4} from 'uuid';
import { writeFile, access, mkdir } from "fs/promises";
import path from "path";
import {env} from "@/env.mjs";
import {checkMinioAlive} from "@/utils/s3-file-management";
export const uploadImageAction = userAction
@@ -18,29 +19,60 @@ export const uploadImageAction = userAction
const fileName = uuid + "." + fileFormat
const arrayBuffer = await file.arrayBuffer()
const buffer = Buffer.from(arrayBuffer)
const localDir = "public/uploads/"
try {
await mkdir(path.join(process.cwd(), localDir), { recursive: true });
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/uploads/${fileName}`
}
return {
data: {result: result, url: url},
}
} catch (error) {
console.log("Error occured ", error);
throw new Error('An error occured while importing image');
// await uploadS3Compatible()
const result = await uploadLocal(fileName,buffer)
const url = getUrl(fileName)
return {
data: {result: result, url: url},
}
});
function getUrl(fileName:string):string {
let url: string = "";
if (env.NODE_ENV === "production") {
// url = `https://${env.S3_ENDPOINT}/${bucketName}/${fileName}`
return url
} else {
url = `http://localhost:8887/uploads/${fileName}`
return url
}
}
async function uploadLocal(fileName: string, buffer: any): Promise<{ data: { result: void; url: string } }> {
const localDir = "public/uploads/"
try {
await mkdir(path.join(process.cwd(), localDir), { recursive: true });
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/uploads/${fileName}`
}
return {
data: {result: result, url: url},
}
} catch (error) {
console.log("Error occured ", error);
throw new Error('An error occured while importing image');
}
}
async function uploadS3Compatible() {
await checkMinioAlive()
}
export async function checkConnexionToS3(){
return await checkMinioAlive()
}