/
FileQueue.js
162 lines (129 loc) · 5.23 KB
/
FileQueue.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
/*
Copyright 2008-2009 University of Toronto
Copyright 2008-2009 University of California, Berkeley
Copyright 2010-2011 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://source.fluidproject.org/svn/LICENSE.txt
*/
/*global SWFUpload, jQuery, fluid_1_4:true*/
// JSLint options
/*jslint white: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
var fluid_1_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.uploader = fluid.uploader || {};
var filterFiles = function (files, filterFn) {
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (filterFn(file) === true) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
fluid.uploader.fileQueue = function () {
var that = {};
that.files = [];
that.isUploading = false;
/********************
* Queue Operations *
********************/
that.start = function () {
that.setupCurrentBatch();
that.isUploading = true;
that.shouldStop = false;
};
that.startFile = function () {
that.currentBatch.fileIdx++;
that.currentBatch.bytesUploadedForFile = 0;
that.currentBatch.previousBytesUploadedForFile = 0;
};
that.finishFile = function (file) {
that.currentBatch.numFilesCompleted++;
};
that.shouldUploadNextFile = function () {
return !that.shouldStop &&
that.isUploading &&
that.currentBatch.numFilesCompleted < that.currentBatch.files.length;
};
/*****************************
* File manipulation methods *
*****************************/
that.addFile = function (file) {
that.files.push(file);
};
that.removeFile = function (file) {
var idx = $.inArray(file, that.files);
that.files.splice(idx, 1);
};
/**********************
* Queue Info Methods *
**********************/
that.totalBytes = function () {
return fluid.uploader.fileQueue.sizeOfFiles(that.files);
};
that.getReadyFiles = function () {
return filterFiles(that.files, function (file) {
return (file.filestatus === fluid.uploader.fileStatusConstants.QUEUED || file.filestatus === fluid.uploader.fileStatusConstants.CANCELLED);
});
};
that.getErroredFiles = function () {
return filterFiles(that.files, function (file) {
return (file.filestatus === fluid.uploader.fileStatusConstants.ERROR);
});
};
that.sizeOfReadyFiles = function () {
return fluid.uploader.fileQueue.sizeOfFiles(that.getReadyFiles());
};
that.getUploadedFiles = function () {
return filterFiles(that.files, function (file) {
return (file.filestatus === fluid.uploader.fileStatusConstants.COMPLETE);
});
};
that.sizeOfUploadedFiles = function () {
return fluid.uploader.fileQueue.sizeOfFiles(that.getUploadedFiles());
};
/*****************
* Batch Methods *
*****************/
that.setupCurrentBatch = function () {
that.clearCurrentBatch();
that.updateCurrentBatch();
};
that.clearCurrentBatch = function () {
that.currentBatch = {
fileIdx: 0,
files: [],
totalBytes: 0,
numFilesCompleted: 0,
numFilesErrored: 0,
bytesUploadedForFile: 0,
previousBytesUploadedForFile: 0,
totalBytesUploaded: 0
};
};
that.updateCurrentBatch = function () {
var readyFiles = that.getReadyFiles();
that.currentBatch.files = readyFiles;
that.currentBatch.totalBytes = fluid.uploader.fileQueue.sizeOfFiles(readyFiles);
};
that.updateBatchStatus = function (currentBytes) {
var byteIncrement = currentBytes - that.currentBatch.previousBytesUploadedForFile;
that.currentBatch.totalBytesUploaded += byteIncrement;
that.currentBatch.bytesUploadedForFile += byteIncrement;
that.currentBatch.previousBytesUploadedForFile = currentBytes;
};
return that;
};
fluid.uploader.fileQueue.sizeOfFiles = function (files) {
var totalBytes = 0;
for (var i = 0; i < files.length; i++) {
var file = files[i];
totalBytes += file.size;
}
return totalBytes;
};
})(jQuery, fluid_1_4);