-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy paths3upload.js
237 lines (220 loc) · 7.07 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function S3MultiUpload(file) {
this.PART_SIZE = 5 * 1024 * 1024 * 1024; // Minimum part size defined by aws s3 is 5 MB, maximum 5 GB
this.SERVER_LOC = '?'; // Location of the server
this.completed = false;
this.file = file;
this.fileInfo = {
name: this.file.name,
type: this.file.type,
size: this.file.size,
lastModifiedDate: this.file.lastModifiedDate
};
this.sendBackData = null;
this.uploadXHR = [];
// Progress monitoring
this.byterate = []
this.lastUploadedSize = []
this.lastUploadedTime = []
this.loaded = [];
this.total = [];
}
/**
* Creates the multipart upload
*/
S3MultiUpload.prototype.createMultipartUpload = function() {
var self = this;
$.post(self.SERVER_LOC, {
command: 'create',
fileInfo: self.fileInfo,
}).done(function(data) {
self.sendBackData = data;
self.uploadParts();
}).fail(function(jqXHR, textStatus, errorThrown) {
self.onServerError('create', jqXHR, textStatus, errorThrown);
});
};
/**
* Call this function to start uploading to server
*/
S3MultiUpload.prototype.start = function() {
this.createMultipartUpload();
};
/** private */
S3MultiUpload.prototype.uploadParts = function() {
var blobs = this.blobs = [], promises = [];
var start = 0;
var parts =0;
var end, blob;
var partNum = 0;
while(start < this.file.size) {
end = Math.min(start + this.PART_SIZE, this.file.size);
filePart = this.file.slice(start, end);
// this is to prevent push blob with 0Kb
if (filePart.size > 0)
blobs.push(filePart);
start = this.PART_SIZE * ++partNum;
}
for (var i = 0; i < blobs.length; i++) {
blob = blobs[i];
promises.push(this.uploadXHR[i]=$.post(this.SERVER_LOC, {
command: 'part',
sendBackData: this.sendBackData,
partNumber: i+1,
contentLength: blob.size
}));
}
$.when.apply(null, promises)
.then(this.sendAll.bind(this), this.onServerError)
.done(this.onPrepareCompleted);
};
/**
* Sends all the created upload parts in a loop
*/
S3MultiUpload.prototype.sendAll = function() {
var blobs = this.blobs;
var length = blobs.length;
if (length==1)
this.sendToS3(arguments[0], blobs[0], 0);
else for (var i = 0; i < length; i++) {
this.sendToS3(arguments[i][0], blobs[i], i);
}
};
/**
* Used to send each uploadPart
* @param array data parameters of the part
* @param blob blob data bytes
* @param integer index part index (base zero)
*/
S3MultiUpload.prototype.sendToS3 = function(data, blob, index) {
var self = this;
var url = data['url'];
var size = blob.size;
var request = self.uploadXHR[index] = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4) { // 4 is DONE
// self.uploadXHR[index] = null;
if (request.status !== 200) {
self.updateProgress();
self.onS3UploadError(request);
return;
}
self.updateProgress();
}
};
request.upload.onprogress = function(e) {
if (e.lengthComputable) {
self.total[index] = size;
self.loaded[index] = e.loaded;
if (self.lastUploadedTime[index])
{
var time_diff=(new Date().getTime() - self.lastUploadedTime[index])/1000;
if (time_diff > 0.005) // 5 miliseconds has passed
{
var byterate=(self.loaded[index] - self.lastUploadedSize[index])/time_diff;
self.byterate[index] = byterate;
self.lastUploadedTime[index]=new Date().getTime();
self.lastUploadedSize[index]=self.loaded[index];
}
}
else
{
self.byterate[index] = 0;
self.lastUploadedTime[index]=new Date().getTime();
self.lastUploadedSize[index]=self.loaded[index];
}
// Only send update to user once, regardless of how many
// parallel XHRs we have (unless the first one is over).
if (index==0 || self.total[0]==self.loaded[0])
self.updateProgress();
}
};
request.open('PUT', url, true);
request.send(blob);
};
/**
* Abort multipart upload
*/
S3MultiUpload.prototype.cancel = function() {
var self = this;
for (var i=0; i<this.uploadXHR.length; ++i) {
this.uploadXHR[i].abort();
}
$.post(self.SERVER_LOC, {
command: 'abort',
sendBackData: self.sendBackData
}).done(function(data) {
});
};
/**
* Complete multipart upload
*/
S3MultiUpload.prototype.completeMultipartUpload = function() {
var self = this;
if (this.completed) return;
this.completed=true;
$.post(self.SERVER_LOC, {
command: 'complete',
sendBackData: self.sendBackData
}).done(function(data) {
self.onUploadCompleted(data);
}).fail(function(jqXHR, textStatus, errorThrown) {
self.onServerError('complete', jqXHR, textStatus, errorThrown);
});
};
/**
* Track progress, propagate event, and check for completion
*/
S3MultiUpload.prototype.updateProgress = function() {
var total=0;
var loaded=0;
var byterate=0.0;
var complete=1;
for (var i=0; i<this.total.length; ++i) {
loaded += +this.loaded[i] || 0;
total += this.total[i];
if (this.loaded[i]!=this.total[i])
{
// Only count byterate for active transfers
byterate += +this.byterate[i] || 0;
complete=0;
}
}
if (complete)
this.completeMultipartUpload();
total=this.fileInfo.size;
this.onProgressChanged(loaded, total, byterate);
};
// Overridable events:
/**
* Overrride this function to catch errors occured when communicating to your server
*
* @param {type} command Name of the command which failed,one of 'CreateMultipartUpload', 'SignUploadPart','CompleteMultipartUpload'
* @param {type} jqXHR jQuery XHR
* @param {type} textStatus resonse text status
* @param {type} errorThrown the error thrown by the server
*/
S3MultiUpload.prototype.onServerError = function(command, jqXHR, textStatus, errorThrown) {};
/**
* Overrride this function to catch errors occured when uploading to S3
*
* @param XMLHttpRequest xhr the XMLHttpRequest object
*/
S3MultiUpload.prototype.onS3UploadError = function(xhr) {};
/**
* Override this function to show user update progress
*
* @param {type} uploadedSize is the total uploaded bytes
* @param {type} totalSize the total size of the uploading file
* @param {type} speed bytes per second
*/
S3MultiUpload.prototype.onProgressChanged = function(uploadedSize, totalSize, bitrate) {};
/**
* Override this method to execute something when upload finishes
*
*/
S3MultiUpload.prototype.onUploadCompleted = function(serverData) {};
/**
* Override this method to execute something when part preparation is completed
*
*/
S3MultiUpload.prototype.onPrepareCompleted = function() {};