|
2 | 2 | import {userAction} from "@/lib/safe-actions/actions"; |
3 | 3 | import {z} from "zod"; |
4 | 4 | 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"; |
11 | 5 | import {db} from "@/db"; |
12 | 6 | import {eq} from "drizzle-orm"; |
13 | | -import {Setting} from "@/db/schema/01_setting"; |
14 | 7 | import * as drizzleDb from "@/db"; |
15 | 8 | import {ServerActionResult} from "@/types/action-type"; |
16 | 9 | import {dispatchStorage} from "@/features/storages/dispatch"; |
17 | 10 | import {StorageInput} from "@/features/storages/types"; |
18 | 11 |
|
19 | | -// const imageDir = "images/"; |
20 | | - |
21 | 12 |
|
22 | 13 | export const uploadUserImageAction = userAction.schema( |
23 | 14 | z.instanceof(FormData) |
@@ -59,7 +50,6 @@ export const uploadUserImageAction = userAction.schema( |
59 | 50 | } |
60 | 51 |
|
61 | 52 | const result = await dispatchStorage(input, undefined, settings.storageChannel.id); |
62 | | - |
63 | 53 | if (!result.success) { |
64 | 54 | return { |
65 | 55 | success: false, |
@@ -90,74 +80,5 @@ export const uploadUserImageAction = userAction.schema( |
90 | 80 | } |
91 | 81 | }); |
92 | 82 |
|
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 | | -} |
150 | 83 |
|
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 | | -} |
160 | 84 |
|
161 | | -export async function checkConnexionToS3() { |
162 | | - return await checkMinioAlive(); |
163 | | -} |
0 commit comments