Skip to content

Commit

Permalink
Fix Share to community button for images (#8737)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahblair committed Jul 11, 2024
1 parent 0482453 commit 31a876d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/ready-roses-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@gradio/image": patch
"@gradio/utils": patch
"gradio": patch
---

fix:Fix `Share to community` button for images
2 changes: 1 addition & 1 deletion js/image/shared/ImagePreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
on:error
formatter={async (value) => {
if (!value) return "";
let url = await uploadToHuggingFace(value, "base64");
let url = await uploadToHuggingFace(value, "url");
return `<img src="${url}" />`;
}}
{value}
Expand Down
30 changes: 25 additions & 5 deletions js/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ShareError extends Error {
}

export async function uploadToHuggingFace(
data: string,
data: string | { url?: string; path?: string },
type: "base64" | "url"
): Promise<string> {
if (window.__gradio_space__ == null) {
Expand All @@ -41,14 +41,34 @@ export async function uploadToHuggingFace(
let contentType: string;
let filename: string;
if (type === "url") {
const response = await fetch(data);
let url: string;

if (typeof data === "object" && data.url) {
url = data.url;
} else if (typeof data === "string") {
url = data;
} else {
throw new Error("Invalid data format for URL type");
}

const response = await fetch(url);
blob = await response.blob();
contentType = response.headers.get("content-type") || "";
filename = response.headers.get("content-disposition") || "";
} else {
blob = dataURLtoBlob(data);
contentType = data.split(";")[0].split(":")[1];
filename = "file" + contentType.split("/")[1];
let dataurl: string;

if (typeof data === "object" && data.path) {
dataurl = data.path;
} else if (typeof data === "string") {
dataurl = data;
} else {
throw new Error("Invalid data format for base64 type");
}

blob = dataURLtoBlob(dataurl);
contentType = dataurl.split(";")[0].split(":")[1];
filename = "file." + contentType.split("/")[1];
}

const file = new File([blob], filename, { type: contentType });
Expand Down

0 comments on commit 31a876d

Please sign in to comment.