forked from rails/rails
/
direct_upload.js
50 lines (44 loc) · 1.3 KB
/
direct_upload.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
import { FileChecksum } from "./file_checksum"
import { BlobRecord } from "./blob_record"
import { BlobUpload } from "./blob_upload"
let id = 0
export class DirectUpload {
constructor(file, url, serviceName, attachmentName, delegate) {
this.id = ++id
this.file = file
this.url = url
this.serviceName = serviceName
this.attachmentName = attachmentName
this.delegate = delegate
}
create(callback) {
FileChecksum.create(this.file, (error, checksum) => {
if (error) {
callback(error)
return
}
const blob = new BlobRecord(this.file, checksum, this.url, this.serviceName, this.attachmentName)
notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr)
blob.create(error => {
if (error) {
callback(error)
} else {
const upload = new BlobUpload(blob)
notify(this.delegate, "directUploadWillStoreFileWithXHR", upload.xhr)
upload.create(error => {
if (error) {
callback(error)
} else {
callback(null, blob.toJSON())
}
})
}
})
})
}
}
function notify(object, methodName, ...messages) {
if (object && typeof object[methodName] == "function") {
return object[methodName](...messages)
}
}