Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Share to community button for images #8737

Merged
merged 20 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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