diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-17 09:57:09 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-17 09:57:09 +0100 |
| commit | e95cf5c7b6a08eb560763d5167fbddc1c2117bcc (patch) | |
| tree | 2f7815c9f39328fcaced2113de727f63e4837fa3 /static/api/images.js | |
| parent | 0016aaa197697ec5ff38dfb3f63ac8b6f74b48e0 (diff) | |
add file uploading and multi-threading
Diffstat (limited to 'static/api/images.js')
| -rw-r--r-- | static/api/images.js | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/static/api/images.js b/static/api/images.js index 4a41a3c..4bdac1f 100644 --- a/static/api/images.js +++ b/static/api/images.js @@ -1,31 +1,29 @@ import * as sfw from 'sfw'; const { Input } = sfw.element.native; +import * as rest from './rest.js'; class FileUploader { - constructor(url) { + constructor(url, files) { this.onprogress = () => {} this.ondone = () => {} this.url = url; this.sessions = []; + this.files = files; } - send(...files) { + send() { let count = 0; - this.sessions = this.sessions.concat(files.map( - file => new Promise((resolve) => { + this.sessions = this.sessions.concat(this.files.map( + (file) => new Promise((resolve) => { const xhr = new XMLHttpRequest(); - xhr.upload.addEventListener("progress", (event) => { - if (event.lengthComputable) { - this.onprogress(file, event.loaded, event.total) - } - }); - - xhr.addEventListener("loadend", () => { + xhr.addEventListener("loadend", (event) => { count += 1; + this.onprogress(count, this.files.length) + resolve(xhr.readyState === 4 && xhr.status === 200); - if (count == files.length) { + if (count == this.files.length) { this.ondone(); } }); @@ -46,11 +44,12 @@ export async function upload_to_timeline() { }) input.click(); - const uploader = new FileUploader('/api/image/upload'); - input.onchange = async () => { - uploader.send(...input.files); - } + return new Promise((resolve) => { + input.onchange = () => { + resolve(new FileUploader('/api/image/upload', [...input.files])); + } + }) } export async function upload_to_profile() { @@ -61,3 +60,7 @@ export async function upload_to_profile() { }) input.click(); } + +export function list() { + return rest.get('/api/image/list').then(r => r.images); +} |