Fix Openinary upload ENAMETOOLONG and undefined slug issues, update api endpoint and auth header
This commit is contained in:
+36
-7
@@ -1,19 +1,46 @@
|
||||
import crypto from 'crypto'
|
||||
|
||||
export async function uploadToOpeninary(file: File, folder: string = "marmarislocal"): Promise<string> {
|
||||
const url = `${process.env.OPENINARY_API_URL}/upload`;
|
||||
const baseUrl = process.env.OPENINARY_API_URL?.replace(/\/+$/, '');
|
||||
const url = `${baseUrl}/api/upload`;
|
||||
const key = process.env.OPENINARY_API_KEY;
|
||||
|
||||
if (!url || !key) {
|
||||
if (!baseUrl || !key) {
|
||||
throw new Error("Openinary configuration (OPENINARY_API_URL or OPENINARY_API_KEY) is missing in environment variables.");
|
||||
}
|
||||
|
||||
// 1. ENAMETOOLONG Fix: Shorten very long folder parts (e.g. slugs)
|
||||
const safeFolder = folder
|
||||
.split('/')
|
||||
.filter(Boolean) // Avoid double slashes in folder path
|
||||
.map(part => {
|
||||
if (part.length > 50) {
|
||||
const hash = crypto.createHash('md5').update(part).digest('hex').substring(0, 8);
|
||||
return `${part.substring(0, 40)}-${hash}`;
|
||||
}
|
||||
return part;
|
||||
})
|
||||
.join('/');
|
||||
|
||||
// 2. ENAMETOOLONG Fix: Shorten very long filenames
|
||||
const lastDotIndex = file.name.lastIndexOf('.');
|
||||
const ext = lastDotIndex !== -1 ? file.name.substring(lastDotIndex) : '';
|
||||
let nameWithoutExt = lastDotIndex !== -1 ? file.name.substring(0, lastDotIndex) : file.name;
|
||||
|
||||
if (nameWithoutExt.length > 50) {
|
||||
const hash = crypto.createHash('md5').update(nameWithoutExt).digest('hex').substring(0, 8);
|
||||
nameWithoutExt = `${nameWithoutExt.substring(0, 40)}-${hash}`;
|
||||
}
|
||||
const safeFileName = `${nameWithoutExt}${ext}`;
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("files", file);
|
||||
formData.append("folder", folder);
|
||||
formData.append("files", file, safeFileName);
|
||||
formData.append("folder", safeFolder);
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"x-api-key": key,
|
||||
"Authorization": `Bearer ${key}`,
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
@@ -27,9 +54,11 @@ export async function uploadToOpeninary(file: File, folder: string = "marmarislo
|
||||
|
||||
// Openinary standard response is usually an array: [{ url: "...", name: "..." }]
|
||||
if (Array.isArray(data) && data.length > 0) {
|
||||
return data[0].url || data[0].secure_url;
|
||||
const fileUrl = data[0].url || data[0].secure_url;
|
||||
return fileUrl.startsWith('/') ? `${baseUrl}${fileUrl}` : fileUrl;
|
||||
} else if (data && typeof data === "object") {
|
||||
return data.url || data.secure_url || (data.files && data.files[0]?.url);
|
||||
const fileUrl = data.url || data.secure_url || (data.files && data.files[0]?.url);
|
||||
return fileUrl.startsWith('/') ? `${baseUrl}${fileUrl}` : fileUrl;
|
||||
}
|
||||
|
||||
throw new Error("Invalid response format from Openinary server.");
|
||||
|
||||
Reference in New Issue
Block a user