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
58
59
60
61
62
63
|
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();
}
|