File size: 1,262 Bytes
2af15f4 3aca7f0 2af15f4 3aca7f0 2af15f4 3aca7f0 2af15f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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)
}
|