-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupload.js
269 lines (243 loc) · 8.57 KB
/
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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* S3MultiUpload Object
* Create a new instance with new S3MultiUpload(file, otherInfo)
* To start uploading, call start()
* You can pause with pause()
* Resume with resume()
* Cancel with cancel()
*
* You can override the following functions (no event emitter :( , description below on the function definition, at the end of the file)
* onServerError = function(command, jqXHR, textStatus, errorThrown) {}
* onS3UploadError = function(xhr) {}
* onProgressChanged = function(uploadingSize, uploadedSize, totalSize) {}
* onUploadCompleted = function() {}
*
* @param {type} file
* @param {type} otheInfo
* @returns {MultiUpload}
*/
function S3MultiUpload(file, otheInfo) {
this.PART_SIZE = 5 * 1024 * 1024; //minimum part size defined by aws s3
this.SERVER_LOC = 'server.php'; //location of the server
this.RETRY_WAIT_SEC = 30; //wait before retrying again on upload failure
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.isPaused = false;
this.uploadXHR = null;
this.otherInfo = otheInfo;
this.uploadedSize = 0;
this.uploadingSize = 0;
this.curUploadInfo = {
blob: null,
partNum: 0
};
this.progress = [];
if (console && console.log) {
this.log = console.log;
} else {
this.log = function() {
};
}
}
/** private */
S3MultiUpload.prototype.createMultipartUpload = function() {
var self = this;
$.get(self.SERVER_LOC, {
command: 'CreateMultipartUpload',
fileInfo: self.fileInfo,
otherInfo: self.otherInfo
}).done(function(data) {
self.sendBackData = data;
self.uploadPart(1);
}).fail(function(jqXHR, textStatus, errorThrown) {
self.onServerError('CreateMultipartUpload', jqXHR, textStatus, errorThrown);
});
};
/**
* Call this function to start uploading to server
*
*/
S3MultiUpload.prototype.start = function() {
this.uploadPart(0);
};
/** private */
S3MultiUpload.prototype.uploadPart = function(partNum) {
var blobs = this.blobs = [], promises = [];
var start = 0;
var end, blob;
this.curUploadInfo.partNum = partNum;
if (this.curUploadInfo.partNum === 0) {
this.createMultipartUpload();
return;
}
if (start > this.file.size) {
this.completeMultipartUpload();
return;
}
while(start < this.file.size) {
start = this.PART_SIZE * this.curUploadInfo.partNum++;
end = Math.min(start + this.PART_SIZE, this.file.size);
blobs.push(this.file.slice(start, end));
}
for (var i = 0; i < blobs.length; i++) {
blob = blobs[i];
promises.push($.get(this.SERVER_LOC, {
command: 'SignUploadPart',
sendBackData: this.sendBackData,
partNumber: this.curUploadInfo.partNum,
contentLength: blob.size
}));
}
// we need to pass $.when an array of arguments
// so we are using .apply()
$.when.apply(null, promises)
.then(this.sendAll.bind(this), this.onServerError);
};
S3MultiUpload.prototype.sendAll = function() {
var blobs = this.blobs;
var length = blobs.length;
for (var i = 0; i < length; i++) {
// sendToS3( XHRresponse, blob);
this.sendToS3(arguments[i][0], blobs[i], i);
}
};
/** private */
S3MultiUpload.prototype.sendToS3 = function(data, blob, index) {
var self = this;
var url = data['url'];
var size = blob.size;
var authHeader = data['authHeader'];
var dateHeader = data['dateHeader'];
var request = self.uploadXHR = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4) {
self.uploadXHR = null;
self.progress[index] = 100;
if (request.status !== 200) {
self.updateProgressBar();
if (!self.isPaused)
self.onS3UploadError(request);
return;
}
self.uploadedSize += blob.size;
self.updateProgressBar();
}
};
request.upload.onprogress = function(e) {
if (e.lengthComputable) {
self.progress[index] = e.loaded / size;
self.updateProgressBar();
}
};
request.open('PUT', url, true);
request.setRequestHeader("x-amz-date", dateHeader);
request.setRequestHeader("Authorization", authHeader);
request.send(blob);
};
/**
* Pause the upload
* Remember, the current progressing part will fail,
* that part will start from beginning (< 5MB of uplaod is wasted)
*/
S3MultiUpload.prototype.pause = function() {
this.isPaused = true;
if (this.uploadXHR !== null) {
this.uploadXHR.abort();
}
};
/**
* Resumes the upload
*
*/
S3MultiUpload.prototype.resume = function() {
this.isPaused = false;
this.uploadPart(this.curUploadInfo.partNum);
};
S3MultiUpload.prototype.cancel = function() {
var self = this;
self.pause();
$.get(self.SERVER_LOC, {
command: 'AbortMultipartUpload',
sendBackData: self.sendBackData
}).done(function(data) {
});
};
S3MultiUpload.prototype.waitRetry = function() {
var self = this;
window.setTimeout(function() {
self.retry();
}, this.RETRY_WAIT_SEC * 1000);
};
S3MultiUpload.prototype.retry = function() {
if (!this.isPaused && self.uploadXHR === null) {
this.uploadPart(this.curUploadInfo.partNum);
}
};
/** private */
S3MultiUpload.prototype.completeMultipartUpload = function() {
var self = this;
$.get(self.SERVER_LOC, {
command: 'CompleteMultipartUpload',
sendBackData: self.sendBackData
}).done(function(data) {
self.onUploadCompleted(data);
}).fail(function(jqXHR, textStatus, errorThrown) {
self.onServerError('CompleteMultipartUpload', jqXHR, textStatus, errorThrown);
});
};
/** private */
S3MultiUpload.prototype.updateProgressBar = function() {
var progress = this.progress;
var length = progress.length;
var total = 0;
for (var i = 0; i < progress.length; i++) {
total = total + progress[i];
}
total = total / length;
this.onProgressChanged(this.uploadingSize, total, this.file.size);
};
/**
* Overrride this function to catch errors occured when communicating to your server
* If this occurs, the program stops, you can retry by retry() or wait and retry by waitRetry()
*
* @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
* If this occurs, we retry upload after RETRY_WAIT_SEC seconds
* Most of the time you don't need to override this, except for informing user that upload of a part failed
*
* @param XMLHttpRequest xhr the XMLHttpRequest object
*/
S3MultiUpload.prototype.onS3UploadError = function(xhr) {
self.waitRetry();
};
/**
* Override this function to show user update progress
*
* @param {type} uploadingSize is the current upload part
* @param {type} uploadedSize is already uploaded part
* @param {type} totalSize the total size of the uploading file
*/
S3MultiUpload.prototype.onProgressChanged = function(uploadingSize, uploadedSize, totalSize) {
this.log("uploadedSize = " + uploadedSize);
this.log("uploadingSize = " + uploadingSize);
this.log("totalSize = " + totalSize);
};
/**
* Override this method to execute something when upload finishes
*
*/
S3MultiUpload.prototype.onUploadCompleted = function(serverData) {
};