-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathActiveStorageUpload.js
81 lines (65 loc) · 2.04 KB
/
ActiveStorageUpload.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable @bigbinary/neeto/file-name-and-export-name-standards */
import { BasePlugin } from "@uppy/core";
import { isEmpty } from "ramda";
import DirectUpload from "utils/DirectUpload";
class ActiveStorageUpload extends BasePlugin {
constructor(uppy, opts) {
super(uppy, opts);
this.type = "uploader";
this.id = opts.id || "ActiveStorageUpload";
this.title = opts.title || "Active Storage Upload";
}
install() {
this.uppy.addUploader(this.upload);
}
uninstall() {
this.uppy.removeUploader(this.upload);
}
upload = fileIDs => {
if (isEmpty(fileIDs)) return Promise.resolve();
const promises = fileIDs.map(id => this.uploadFile(this.uppy.getFile(id)));
return Promise.all(promises);
};
uploadFile = async file => {
const handleProgress = xhr => {
if (xhr.lengthComputable) {
this.uppy.emit("upload-progress", file, {
uploader: this,
bytesUploaded: xhr.loaded,
bytesTotal: xhr.total,
progress: Math.round((xhr.loaded / xhr.total) * 100),
});
}
};
this.uppy.on("file-removed", removedFile => {
if (!(removedFile.id === file.id)) return;
upload.abort();
if (isEmpty(this.uppy.getFiles())) {
this.uppy.emit("complete");
}
});
this.uppy.on("upload-cancel", fileID => {
if (fileID === file.id) {
upload.abort();
}
});
this.uppy.on("cancel-all", () => upload.abort());
const upload = new DirectUpload({
file: file.data,
url: this.opts.directUploadUrl,
progress: handleProgress,
});
try {
const blob = await upload.create();
this.uppy.setFileState(file.id, { response: { status: "success" } });
this.uppy.emit("upload-success", file, blob);
return file;
} catch (error) {
if (!this.uppy.getFile(file.id)) return null;
this.uppy.setFileState(file.id, { response: { status: "error" } });
this.uppy.emit("upload-error", file, error);
throw error;
}
};
}
export default ActiveStorageUpload;