aboutsummaryrefslogtreecommitdiff
path: root/static/api
diff options
context:
space:
mode:
Diffstat (limited to 'static/api')
-rw-r--r--static/api/images.js35
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);
+}