forked from closeio/s3upload-coffee-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3upload.js
135 lines (121 loc) · 4.52 KB
/
s3upload.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(function() {
window.S3Upload = (function() {
S3Upload.prototype.s3_sign_put_url = '/signS3put';
S3Upload.prototype.file_dom_selector = '#file_upload';
S3Upload.prototype.onFinishS3Put = function(public_url, file) {
return console.log('base.onFinishS3Put()', public_url, file);
};
S3Upload.prototype.onProgress = function(percent, status, public_url, file) {
return console.log('base.onProgress()', percent, status, public_url, file);
};
S3Upload.prototype.onError = function(status, file) {
return console.log('base.onError()', status, file);
};
function S3Upload(options) {
if (options == null) {
options = {};
}
_.extend(this, options);
if (this.file_dom_selector) {
this.handleFileSelect($(this.file_dom_selector).get(0));
}
}
S3Upload.prototype.handleFileSelect = function(file_element) {
var f, files, output, _i, _len, _results;
this.onProgress(0, 'Upload started.');
files = file_element.files;
output = [];
_results = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
f = files[_i];
_results.push(this.uploadFile(f));
}
return _results;
};
S3Upload.prototype.createCORSRequest = function(method, url) {
var xhr;
xhr = new XMLHttpRequest();
if (xhr.withCredentials != null) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest !== "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
};
S3Upload.prototype.executeOnSignedUrl = function(file, callback, opts) {
var name, this_s3upload, type, xhr;
this_s3upload = this;
xhr = new XMLHttpRequest();
type = opts && opts.type || file.type || "application/octet-stream";
name = opts && opts.name || file.name;
xhr.open('GET', this.s3_sign_put_url + '?s3_object_type=' + type + '&s3_object_name=' + encodeURIComponent(name), true);
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.onreadystatechange = function(e) {
var error, result;
if (this.readyState === 4 && this.status === 200) {
try {
result = JSON.parse(this.responseText);
} catch (_error) {
error = _error;
this_s3upload.onError('Signing server returned some ugly/empty JSON: "' + this.responseText + '"');
return false;
}
return callback(result.signed_request, result.url);
} else if (this.readyState === 4 && this.status !== 200) {
return this_s3upload.onError('Could not contact request signing server. Status = ' + this.status);
}
};
return xhr.send();
};
S3Upload.prototype.uploadToS3 = function(file, url, public_url, opts) {
var this_s3upload, type, xhr;
this_s3upload = this;
type = opts && opts.type || file.type || "application/octet-stream";
xhr = this.createCORSRequest('PUT', url);
if (!xhr) {
this.onError('CORS not supported');
} else {
xhr.onload = function() {
if (xhr.status === 200) {
this_s3upload.onProgress(100, 'Upload completed.', public_url, file);
return this_s3upload.onFinishS3Put(public_url, file);
} else {
return this_s3upload.onError('Upload error: ' + xhr.status, file);
}
};
xhr.onerror = function() {
return this_s3upload.onError('XHR error.', file);
};
xhr.upload.onprogress = function(e) {
var percentLoaded;
if (e.lengthComputable) {
percentLoaded = Math.round((e.loaded / e.total) * 100);
return this_s3upload.onProgress(percentLoaded, (percentLoaded === 100 ? 'Finalizing.' : 'Uploading.'), public_url, file);
}
};
}
xhr.setRequestHeader('Content-Type', type);
xhr.setRequestHeader('x-amz-acl', 'public-read');
return xhr.send(file);
};
S3Upload.prototype.validate = function(file) {
return null;
};
S3Upload.prototype.uploadFile = function(file, opts) {
var error, this_s3upload;
error = this.validate(file);
if (error) {
this.onError(error, file);
return null;
}
this_s3upload = this;
return this.executeOnSignedUrl(file, function(signedURL, publicURL) {
return this_s3upload.uploadToS3(file, signedURL, publicURL, opts);
}, opts);
};
return S3Upload;
})();
}).call(this);