-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitStreamerClient.js
More file actions
executable file
·471 lines (400 loc) · 15.3 KB
/
BitStreamerClient.js
File metadata and controls
executable file
·471 lines (400 loc) · 15.3 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/****************************
BitStreamer Client - A JS library for uploading Bulk & Large Files.
Copyright (C) 2007-2018 Ashley Johnson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
----------------------------
BitStreamer
----------------------------
The main class for BitStreamer
client.
Author: Ashley Johnson
Email: runashrunner@gmail.com
Info: Whilst looking for uploading solutions I found none that I thought would suit
me and also, were either missing client side or server side script with the download
so created one.
Its probably no where near advanced or probably has a few bugs but I have built
something that in principle works and would like to share it with the community in
the hope, if nothing else that it will help people to understand how to upload
large files as chunks and put them back together on the server,
License: GPL v3
Credits: Ashley Johnson
Usage: If you stumble upon this script, feel free to use it. All I ask is that
you retain myself in the credits and that if you improve it let me know so
that others can benefit from it.
Bugs: Just let me know or if you can fix it do so and let me know so I can
add it in for the community. :)
****************************/
;(function( $ ) {
"use strict";
/****************************
formInputs
----------------------------
This stores the form
content to be processed.
*****************************/
var formInputs = [];
/****************************
formFileInputs
----------------------------
This stores the form
input values from all
except file uploaders
*****************************/
var formFileInputs = [];
/****************************
formFileInputFiles
----------------------------
This stores the form
input values from all
file uploaders
*****************************/
var formFileInputFiles = [];
/****************************
Current File Variables.
----------------------------
Used to specify the current
file for processing. Set
after each title has been
processed,
*****************************/
var currentFileID = 0; // Unique Identifier
var currentFilename; // Filename as String
var currentFileSize = 0; // Filesize of current file.
var currentFileAsBlob; // A variable to store the current file as a Blob
var currentFileChunk = 0; // The current chunk to be sent to the server.
var currentFileOffset = 0; // The current position of the chunk that needs uploading.
var currentFileTotalChunks = 0; // Total chunks required in order to upload the file.
var currentFileChunksSent = 0; // How many chunks have been sent
var currentFileChunksTransfered = 0; // How many chunks have been recieved.
var currentFileProgress = 0; // The progress of the current file.
/****************************
uploadsArePaused
----------------------------
(Bool) Lets the script know
if the user has invoked the
pause button.
****************************/
var uploadsArePaused = false;
/****************************
chunksForAllFiles
----------------------------
How many chunks in total
for the whole form.
Used for Overal Progress
****************************/
var chunksForAllFiles = 0;
/****************************
chunksForAllFilesProcessed
----------------------------
How many chunks have been
processed in total.
Used for Overal Progress
****************************/
var chunksForAllFilesProcessed = 0;
/****************************
this.submitUploadForm
----------------------------
The main process that begins
to upload the files.
****************************/
var defaults = {
maxChunkSize: 400000,
uploadHandler: null, // Url to send chunks
successCallback: null, // Url to submit to after sucessfull uploads.
pauseButtonID: null,
resumeButtonID: null,
retryOnError: true,
waitAfterChunk: 500
}
var options;
$.fn.BitStreamer = function(options) {
return this.each(function() {
new BitStreamer(this, options);
});
};
var BitStreamer = function(form, options) {
this.options = $.extend( true, {}, defaults, options );
this.form = form;
this.events();
}
BitStreamer.prototype = {
events : function(e) {
var self = this;
$(this.form).submit(function(e) {
e.preventDefault();
formInputs = $(self.form).find("input").not("[type=file]");
formFileInputs = $(self.form).find("[type=file]");
self.submitUploadForm(e);
});
},
submitUploadForm : function() {
$('#progress-box').show(); // Show progress bar. Improvement needed.
console.log("Preparing file upload...");
console.log("----- Uploading -----")
$('#bsfilesbox').html(""); // Reset the files box. Improvement needed. Very Basic.
/****************************
Loop
----------------------------
The loop go through the files
to upload and assigns a progress
bar in the #filesbox as well as
tallying up how many chunks will
need to be sent in total.
Again, I will improve on this soon.
****************************/
for(var f=0; f<formFileInputs.length; f++) {
var filesToUpload = formFileInputs[f].files;
for(var u=0; u<filesToUpload.length; u++) {
formFileInputFiles.push(filesToUpload[u]);
$('#bsfilesbox').append("<p style='text-align: left; text-transform: uppercase; margin-bottom: 2px; padding-right:0; font-family: arial;'><span id='file"+u+"'>"+filesToUpload[u].name+"</span><span style='float: right;' id='progress-status"+u+"'></span></p><div id='bar_container' style='width: 100%; background-color: #bbb; height: auto; margin-top: 5px; margin-bottom: 10px;'><div id='bar"+u+"' style='height: 5px; font-size: 80%; background-color: #00a300; transition:600ms linear; color:#ffffff; font-family: sans-serif; width: 0px;''></div></div>");
chunksForAllFiles = chunksForAllFiles + Math.ceil(filesToUpload[u].size/this.options.maxChunkSize, this.options.maxChunkSize);
console.log(filesToUpload[u].name);
}
}
console.log("----- Uploading -----");
/****************************
Loop
----------------------------
Just for debugging purposes.
Loops though other data fields
and if name is not "" prints
them to the JS console.
****************************/
console.log("----- DATA to send with files -----");
for(var fi=0; fi<formInputs.length; fi++) {
if(formInputs[fi].name != "") {
console.log(formInputs[fi].name);
}
}
console.log("----- DATA to send with files -----");
$('#pause').show(); // Show pause button, Improvement Needed
this.form.hide(); // Hide form: this unfortunatley assums its the only form on the page.
this.processFile(); // Start proccessing the first file.
},
/****************************
this.processFile
----------------------------
Starts the process of uploading
the current file.
****************************/
processFile : function() {
if(currentFileID > (formFileInputFiles.length-1)) return false; // If there are no more files... end.
$("#file"+currentFileID).css("font-weight", "bold"); // Set the current filename to be bold in the files box.
console.log("Setting filename to "+formFileInputFiles[currentFileID].name);
// Set the filename
currentFilename = formFileInputFiles[currentFileID].name;
console.log("Setting fileSize to "+formFileInputFiles[currentFileID].size);
// Set the file size.
currentFileSize = formFileInputFiles[currentFileID].size;
// Set total chunks for this file.
currentFileTotalChunks = Math.ceil(formFileInputFiles[currentFileID].size/this.options.maxChunkSize, this.options.maxChunkSize)-1;
console.log("totalChunks to send are "+(currentFileTotalChunks+1));
// Store file as Blob.
currentFileAsBlob = formFileInputFiles[currentFileID];
//Start processing the individual chunks.
this.processChunk();
},
/****************************
this.pauseFileUpload
----------------------------
Pauses the file upload process
until the user resumes it
****************************/
pauseFileUpload : function() {
console.log("Paused at chunk: "+currentFileChunk);
// Set the uploadsArePaused flag to true
uploadsArePaused = true;
},
/****************************
this.resumeFileUpload
----------------------------
Resumes the file upload process
from the next chunk.
P.S the pause process only starts
after chunk was uploaded so resume
calls next chunk...
****************************/
resumeFileUpload : function() {
console.log("Resuming at chunk: "+currentFileChunk);
// Set the uploadsArePaused flag to false
uploadsArePaused = false;
// Hide the resume button and show the pause button again.
$('#resume').hide();
$('#pause').show();
//Process the next chunk.
this.processNextChunk();
},
/****************************
this.processNextFile
----------------------------
After a file is finished
uploading this is called to
upload the next file (if any)
****************************/
processNextFile : function() {
if(!uploadsArePaused) {
// Uploads are not paused so continue...
$("#file"+currentFileID).attr("style", "font-weight: normal;");
console.log("Looking for next file...");
currentFileID++;
var filesToProcess = formFileInputFiles.length-1;
console.log(currentFileID+"/"+filesToProcess);
if(currentFileID > filesToProcess) {
$('#bar').css('width', "100%");
$('#progress-status').html("100%");
// No More files to process. So upload finished.
$('#resume').hide();
$('#pause').hide();
$('#bar').css('background-color', '#00a300');
console.log("No more files found...");
return;
} else {
// More files to process.
$('#bar').css('width', 0+"%");
console.log("Next file found, Uploading...");
currentFileChunk = 0;
this.processFile();
}
} else {
// Uploads are paused so wait.
$('#resume').show();
$('#pause').hide();
}
},
/****************************
this.waitACottonPickingMinuteLetTheAjaxHaveABeer
----------------------------
LOLZ, I had the trouble that smaller files were
making the server give a 403 Forbidden as when
uploading say 50+ images under a MB or so it
was pinging the server every few seconds. So to
stop the server returning forbidden, I used a
script I had found somewhere online and modified it.
I thought the function name (Credit: Me [Ashley Johnson]) was appropriate.
Maybe this can be the first uploader with a laugh to.
****************************/
waitACottonPickingMinuteLetTheAjaxHaveABeer : function(ms){
console.log("Giving the Ajax a Beer!");
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
},
/****************************
this.processNextChunk
----------------------------
After a chunk has finished
uploading this is called to
upload the next chunk (if any)
****************************/
processNextChunk : function() {
if(!uploadsArePaused) {
// Uploads are not paused so continue...
console.log("Looking for next chunk...");
currentFileChunk++;
chunksForAllFilesProcessed++;
if(currentFileChunk > currentFileTotalChunks) {
// No More chunks to process so call processNextFile().
console.log("No more chunks found...");
this.processNextFile();
} else {
// More chunks to process.
console.log("Next Chunk Found, Sending...");
this.processChunk();
}
} else {
// Uploads are paused so wait.
$('#resume').show();
$('#pause').hide();
}
},
/****************************
this.processChunk
----------------------------
This is the function that
communicates with the server.
It will send the chunk to the
server with information on which
file it is, current chunk number,
total chunks for file etc...
****************************/
processChunk : function() {
// If there are no more chunks... end.
if(currentFileChunk > currentFileTotalChunks) return false;
currentFileOffset = currentFileChunk * this.options.maxChunkSize;
var currentBlob = currentFileAsBlob.slice(currentFileOffset, this.options.maxChunkSize+currentFileOffset);
var chunkEndPoint = this.options.maxChunkSize+currentFileOffset;
var totalProgress;
console.log(chunksForAllFiles);
if (chunksForAllFiles != 0) {
totalProgress = Math.ceil((100/chunksForAllFiles) * chunksForAllFilesProcessed);
console.log("Progress By Chunk: "+totalProgress);
console.log("File ID: "+currentFileID);
console.log(chunksForAllFilesProcessed);
} else {
totalProgress = Math.ceil((100/formFileInputFiles.length) * currentFileID);
console.log("Progress By Files Completed: "+totalProgress);
console.log("File ID: "+currentFileID);
}
if (currentFileTotalChunks == 0) {
currentFileProgress = 100;
} else {
currentFileProgress = Math.ceil((100/currentFileTotalChunks)*currentFileChunk);
}
$('#bar'+currentFileID).css('width', currentFileProgress+"%");
$('#bar').css('width', totalProgress+"%");
$('#progress-status').html(totalProgress+"%");
$('#progress-status'+currentFileID).html(currentFileProgress+"%");
console.log("Sending Chunk "+(currentFileChunk + 1)+"/"+(currentFileTotalChunks+1));
console.log("Chunk Start: "+currentFileOffset);
console.log("Chunk End: "+chunkEndPoint);
// Create new FormData() Obj.
var fData = new FormData();
/****************************
Loop
----------------------------
Go through non file inputs and
appends the FormData to the
FormData() obj.
****************************/
for(var i=0; i<formInputs.length; i++) {
fData.append(formInputs[i].name, formInputs[i].value);
}
fData.append('filename', currentFilename); // the filename of the current file.
fData.append('chunk_as_binary', currentBlob); // The chunk being uploaded as a Blob.
fData.append('chunk_id', currentFileChunk); // Information on current chunks for current file.
fData.append('chunk_tally', currentFileTotalChunks); // Information on total chunks for current file.
$.ajax({
context: this,
type: 'POST',
url: this.options.uploadHandler, // URL to BS server PHP (Your file Handler)
data: fData, // Form data.
processData: false, // Keep as false. we dont want Ajax to process the data.
contentType: false, // Dont set content type.
error: function() {
//If an error occurs try to process again.
if(this.options.retryOnError === true) {
this.processChunk();
} else {
alert("There was an error and the flag retryOnError is false... Aborting...");
}
},
success: function() {
//Success - Call this.processNextChunk()
this.waitACottonPickingMinuteLetTheAjaxHaveABeer(500);
this.processNextChunk();
}
});
}
}
})( jQuery );