Skip to content

Commit 8f7f9b4

Browse files
authored
Merge pull request #33 from Portabase/feat/avatar-upload
fix: Upload avatar image error for s3 storage
2 parents 8211407 + 4c0d68e commit 8f7f9b4

File tree

3 files changed

+5
-82
lines changed

3 files changed

+5
-82
lines changed

app/api/images/[fileName]/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export async function GET(
6464
const result = await dispatchStorage(input, undefined, settings.storageChannel.id);
6565

6666
if (!result.file || !Buffer.isBuffer(result.file)) {
67+
console.error(`An error occurred while getting file :`, result);
6768
return NextResponse.json(
6869
{error: "Invalid file payload"},
6970
{status: 500}

src/features/storages/providers/s3.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Minio from "minio";
22
import {StorageDeleteInput, StorageGetInput, StorageResult, StorageUploadInput} from "../types";
3+
import {getServerUrl} from "@/utils/get-server-url";
34

45
type S3Config = {
56
endPointUrl: string;
@@ -44,19 +45,19 @@ export async function uploadS3(config: S3Config, input: { data: StorageUploadInp
4445
}
4546

4647
await client.putObject(config.bucketName, key, input.data.file as Buffer);
48+
const baseUrl = getServerUrl();
4749

4850
return {
4951
success: true,
50-
provider: "s3",
52+
provider: 's3',
53+
url: `${baseUrl}/api/${input.data.path}`,
5154
};
5255
}
5356

5457
export async function getS3(config: S3Config, input: { data: StorageGetInput }): Promise<StorageResult> {
5558
const client = await getS3Client(config);
5659

57-
// const key = input.data.path;
5860
const key = `${BASE_DIR}${input.data.path}`;
59-
6061
try {
6162
await client.statObject(config.bucketName, key);
6263
} catch {

src/features/upload/public/upload.action.ts

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,13 @@
22
import {userAction} from "@/lib/safe-actions/actions";
33
import {z} from "zod";
44
import {v4 as uuidv4} from "uuid";
5-
import {mkdir, writeFile} from "fs/promises";
6-
import path from "path";
7-
import {env} from "@/env.mjs";
8-
import {checkMinioAlive, saveFileInBucket} from "@/utils/s3-file-management";
9-
//@ts-ignore
10-
import {getServerUrl} from "@/utils/get-server-url";
115
import {db} from "@/db";
126
import {eq} from "drizzle-orm";
13-
import {Setting} from "@/db/schema/01_setting";
147
import * as drizzleDb from "@/db";
158
import {ServerActionResult} from "@/types/action-type";
169
import {dispatchStorage} from "@/features/storages/dispatch";
1710
import {StorageInput} from "@/features/storages/types";
1811

19-
// const imageDir = "images/";
20-
2112

2213
export const uploadUserImageAction = userAction.schema(
2314
z.instanceof(FormData)
@@ -59,7 +50,6 @@ export const uploadUserImageAction = userAction.schema(
5950
}
6051

6152
const result = await dispatchStorage(input, undefined, settings.storageChannel.id);
62-
6353
if (!result.success) {
6454
return {
6555
success: false,
@@ -90,74 +80,5 @@ export const uploadUserImageAction = userAction.schema(
9080
}
9181
});
9282

93-
//
94-
// export const uploadImageAction = userAction
95-
// .schema(z.instanceof(FormData))
96-
// .action(async ({parsedInput: formData, ctx}) => {
97-
// const file = formData.get("file") as File;
98-
// const uuid = uuidv4();
99-
// const fileFormat = file.name.split(".").pop();
100-
// const arrayBuffer = await file.arrayBuffer();
101-
// const buffer = Buffer.from(arrayBuffer);
102-
//
103-
//
104-
// const settings = await db.query.setting.findFirst({
105-
// where: eq(drizzleDb.schemas.setting.name, "system"),
106-
// with: {
107-
// storageChannel: true
108-
// }
109-
// });
110-
//
111-
//
112-
// if (!settings) throw new Error("System settings not found.");
113-
//
114-
// let fileName: string | null = null;
115-
// let result: void | UploadedObjectInfo;
116-
//
117-
// if (settings.storage === "local") {
118-
// fileName = `${imageDir}${uuid}.${fileFormat}`;
119-
// result = await uploadLocal(fileName, buffer);
120-
// } else if (settings.storage === "s3") {
121-
// fileName = `${imageDir}${uuid}.${fileFormat}`;
122-
// result = await uploadS3Compatible(env.S3_BUCKET_NAME ?? "", fileName, buffer);
123-
// } else {
124-
// throw new Error(`Unsupported storage type: ${settings.storage}`);
125-
// }
126-
//
127-
//
128-
// const url = `${getServerUrl()}/api/${fileName}`
129-
// return {data: {result, url}};
130-
// });
131-
//
132-
133-
async function uploadLocal(fileName: string, buffer: any) {
134-
const localDir = "private/uploads/";
135-
try {
136-
await mkdir(path.join(process.cwd(), localDir), {recursive: true});
137-
return await writeFile(path.join(process.cwd(), localDir + fileName), buffer);
138-
} catch (error) {
139-
throw new Error("An error occured while importing image");
140-
}
141-
}
142-
143-
async function uploadS3Compatible(bucketName: string, fileName: string, buffer: any) {
144-
return await saveFileInBucket({
145-
bucketName,
146-
fileName,
147-
file: buffer,
148-
});
149-
}
15083

151-
function getUrl(fileName: string, settings: Setting, bucketName: string): string {
152-
if (settings.storage === "s3") {
153-
return `https://${env.S3_ENDPOINT}/${bucketName}/${fileName}`;
154-
} else if (settings.storage === "local") {
155-
const url = getServerUrl();
156-
return `${url}/api/images/${fileName}`;
157-
}
158-
throw new Error("Invalid storage configuration");
159-
}
16084

161-
export async function checkConnexionToS3() {
162-
return await checkMinioAlive();
163-
}

0 commit comments

Comments
 (0)