C
File size: 1,661 Bytes
3aca7f0
2af15f4
 
3aca7f0
 
 
2af15f4
3aca7f0
 
 
2af15f4
3aca7f0
 
2af15f4
3aca7f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { upscaleImage } from "/upscale/image.js"

const $ = selector => document.querySelector(selector)
const p = new URLSearchParams(location.search).get("p")
const model = $("#model")
const scale = $("#scale")
const upload = $("#upload")
const button = $("#upscale")
const input = $("#input")
const output = $("#output")
const status = $(".status")
let inputUrl
let outputUrl

const optionsResponse = await fetch(`/api/upscale/options?${new URLSearchParams({ p })}`)
if (optionsResponse.ok) {
    const options = await optionsResponse.json()
    for (const name of options.models) model.add(new Option(name, name))
    model.value = options.default
} else {
    status.textContent = "Unable to load upscale models"
}

upload.addEventListener("change", () => {
    const file = upload.files[0]
    if (!file) return
    if (inputUrl) URL.revokeObjectURL(inputUrl)
    inputUrl = URL.createObjectURL(file)
    input.src = inputUrl
    input.hidden = false
    button.disabled = !model.value
    status.textContent = ""
})

button.addEventListener("click", async () => {
    const file = upload.files[0]
    if (!file) return
    button.disabled = true
    status.textContent = "Upscaling…"
    try {
        const blob = await upscaleImage({
            file,
            model: model.value,
            scale: scale.value,
        })
        if (outputUrl) URL.revokeObjectURL(outputUrl)
        outputUrl = URL.createObjectURL(blob)
        output.src = outputUrl
        output.hidden = false
        status.textContent = "Complete"
    } catch (error) {
        status.textContent = error.message
    } finally {
        button.disabled = false
    }
})