C / upscale.js
q6's picture
Expand standalone upscale interface
3aca7f0
Raw History Blame Contribute Delete
1.26 kB
const DOWNLOAD_LIFETIME = 30000
export async function upscaleImage({ file, path, model, scale = 1 }) {
const query = new URLSearchParams({
p: new URLSearchParams(location.search).get("p"),
scale,
})
if (path !== undefined) query.set("path", path)
if (model !== undefined) query.set("model", model)
const response = await fetch(`/api/upscale?${query}`, {
method: "POST",
headers: { "Content-Type": "application/octet-stream" },
body: file,
})
if (!response.ok) {
if (response.headers.get("Content-Type")?.includes("application/json")) {
const error = await response.json()
throw new Error(error.detail)
}
throw new Error(`Upscale failed (${response.status})`)
}
return response.blob()
}
export async function downloadUpscale({ file, path, name, model, scale }) {
const blob = await upscaleImage({ file, path, model, scale })
const url = URL.createObjectURL(blob)
const link = document.createElement("a")
link.href = url
link.download = `${name.replace(/\.[^.]+$/, "")}-upscaled.png`
document.body.append(link)
link.click()
link.remove()
setTimeout(() => URL.revokeObjectURL(url), DOWNLOAD_LIFETIME)
}