import * as sfw from 'sfw'; const { Input } = sfw.element.native; class FileUploader { constructor(url) { this.onprogress = () => {} this.ondone = () => {} this.url = url; this.sessions = []; } send(...files) { let count = 0; this.sessions = this.sessions.concat(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", () => { count += 1; resolve(xhr.readyState === 4 && xhr.status === 200); if (count == files.length) { this.ondone(); } }); xhr.open("POST", this.url, true); xhr.setRequestHeader("Content-Type", "application/octet-stream"); xhr.send(file.slice()); }) )); } } export async function upload_to_timeline() { const input = Input.new({ type: 'file', multiple: true, accept: 'image/jpeg', }) input.click(); const uploader = new FileUploader('/api/image/upload'); input.onchange = async () => { uploader.send(...input.files); } } export async function upload_to_profile() { const input = Input.new({ type: 'file', multiple: false, accept: 'image/jpeg', }) input.click(); }