bypass cors to fix e-z uploads (#697)
This commit is contained in:
parent
11a630a3c6
commit
188fe57d44
2 changed files with 111 additions and 6 deletions
|
|
@ -42,3 +42,74 @@ export async function uploadToNest(
|
|||
return { success: false, error: e instanceof Error ? e.message : "Unknown error" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadToEzHost(
|
||||
_: IpcMainInvokeEvent,
|
||||
fileBuffer: ArrayBuffer,
|
||||
filename: string,
|
||||
key: string
|
||||
): Promise<NativeUploadResult> {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("file", new Blob([fileBuffer]), filename);
|
||||
|
||||
const response = await fetch("https://api.e-z.host/files", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
key
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
return { success: false, error: `Upload failed: ${response.status} ${errorText}` };
|
||||
}
|
||||
|
||||
const data = await response.json() as { success: boolean; error?: string; imageUrl?: string; rawUrl?: string; };
|
||||
|
||||
if (!data || !data.success) {
|
||||
return { success: false, error: data?.error || "Upload failed" };
|
||||
}
|
||||
|
||||
if (data.imageUrl || data.rawUrl) {
|
||||
return { success: true, url: data.imageUrl || data.rawUrl };
|
||||
}
|
||||
|
||||
return { success: false, error: "No URL returned from upload" };
|
||||
} catch (e) {
|
||||
return { success: false, error: e instanceof Error ? e.message : "Unknown error" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchFile(
|
||||
|
||||
_: IpcMainInvokeEvent,
|
||||
|
||||
url: string
|
||||
|
||||
): Promise<{ success: boolean; data?: ArrayBuffer; contentType?: string; error?: string }> {
|
||||
|
||||
try {
|
||||
|
||||
const response = await fetch(url);
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
return { success: false, error: `Fetch failed: ${response.status} ${response.statusText}` };
|
||||
|
||||
}
|
||||
|
||||
const data = await response.arrayBuffer();
|
||||
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
|
||||
return { success: true, data, contentType };
|
||||
|
||||
} catch (e) {
|
||||
|
||||
return { success: false, error: e instanceof Error ? e.message : "Unknown error" };
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,10 +103,25 @@ export function isConfigured(): boolean {
|
|||
}
|
||||
|
||||
async function uploadToEzHost(fileBlob: Blob, filename: string): Promise<string> {
|
||||
const ezHostKey = (settings.store as { ezHostKey?: string }).ezHostKey;
|
||||
const { ezHostKey } = (settings.store as { ezHostKey?: string });
|
||||
|
||||
if (!ezHostKey) throw new Error("E-Z Host API key is required");
|
||||
|
||||
if (Native) {
|
||||
const arrayBuffer = await fileBlob.arrayBuffer();
|
||||
const result = await Native.uploadToEzHost(arrayBuffer, filename, ezHostKey);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error || "Upload failed");
|
||||
}
|
||||
|
||||
if (!result.url) {
|
||||
throw new Error("No URL returned from upload");
|
||||
}
|
||||
|
||||
return result.url;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("file", fileBlob, filename);
|
||||
|
||||
|
|
@ -152,13 +167,32 @@ export async function uploadFile(url: string): Promise<void> {
|
|||
fetchUrl = url.replace("passthrough=false", "passthrough=true");
|
||||
}
|
||||
|
||||
const response = await fetch(fetchUrl);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch file: ${response.status}`);
|
||||
let blob: Blob;
|
||||
let contentType: string;
|
||||
|
||||
if (Native) {
|
||||
const res = await Native.fetchFile(fetchUrl);
|
||||
if (res.success && res.data) {
|
||||
contentType = res.contentType || "";
|
||||
blob = new Blob([res.data], { type: contentType });
|
||||
} else {
|
||||
const response = await fetch(fetchUrl);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch file: ${response.status}`);
|
||||
}
|
||||
contentType = response.headers.get("content-type") || "";
|
||||
blob = await response.blob();
|
||||
}
|
||||
} else {
|
||||
const response = await fetch(fetchUrl);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch file: ${response.status}`);
|
||||
}
|
||||
|
||||
contentType = response.headers.get("content-type") || "";
|
||||
blob = await response.blob();
|
||||
}
|
||||
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
let blob = await response.blob();
|
||||
let ext = await getExtensionFromBytes(blob) || getExtensionFromMime(contentType) || getExtensionFromMime(blob.type) || getUrlExtension(url) || "png";
|
||||
|
||||
if (ext === "apng" && settings.store.apngToGif) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue