first commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
export async function uploadToOpeninary(file: File, folder: string = "marmarislocal"): Promise<string> {
|
||||
const url = `${process.env.OPENINARY_API_URL}/upload`;
|
||||
const key = process.env.OPENINARY_API_KEY;
|
||||
|
||||
if (!url || !key) {
|
||||
throw new Error("Openinary configuration (OPENINARY_API_URL or OPENINARY_API_KEY) is missing in environment variables.");
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("files", file);
|
||||
formData.append("folder", folder);
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"x-api-key": key,
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errText = await response.text();
|
||||
throw new Error(`Openinary upload failed: ${response.statusText} - ${errText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Openinary standard response is usually an array: [{ url: "...", name: "..." }]
|
||||
if (Array.isArray(data) && data.length > 0) {
|
||||
return data[0].url || data[0].secure_url;
|
||||
} else if (data && typeof data === "object") {
|
||||
return data.url || data.secure_url || (data.files && data.files[0]?.url);
|
||||
}
|
||||
|
||||
throw new Error("Invalid response format from Openinary server.");
|
||||
}
|
||||
Reference in New Issue
Block a user