fine_uploader 3.1.1 → 3.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,115 +1,187 @@
1
1
  /**
2
2
  * Class for uploading files, uploading itself is handled by child classes
3
3
  */
4
- qq.UploadHandlerAbstract = function(o){
4
+ /*globals qq*/
5
+ qq.UploadHandler = function(o) {
6
+ "use strict";
7
+
8
+ var queue = [],
9
+ options, log, dequeue, handlerImpl;
10
+
5
11
  // Default options, can be overridden by the user
6
- this._options = {
12
+ options = {
7
13
  debug: false,
8
- endpoint: '/upload.php',
14
+ forceMultipart: true,
9
15
  paramsInBody: false,
10
- // maximum number of concurrent uploads
11
- maxConnections: 999,
16
+ paramsStore: {},
17
+ endpointStore: {},
18
+ cors: {
19
+ expected: false,
20
+ sendCredentials: false
21
+ },
22
+ maxConnections: 3, // maximum number of concurrent uploads
23
+ uuidParamName: 'qquuid',
24
+ totalFileSizeParamName: 'qqtotalfilesize',
25
+ chunking: {
26
+ enabled: false,
27
+ partSize: 2000000, //bytes
28
+ paramNames: {
29
+ partIndex: 'qqpartindex',
30
+ partByteOffset: 'qqpartbyteoffset',
31
+ chunkSize: 'qqchunksize',
32
+ totalParts: 'qqtotalparts',
33
+ filename: 'qqfilename'
34
+ }
35
+ },
36
+ resume: {
37
+ enabled: false,
38
+ id: null,
39
+ cookiesExpireIn: 7, //days
40
+ paramNames: {
41
+ resuming: "qqresume"
42
+ }
43
+ },
44
+ blobs: {
45
+ paramNames: {
46
+ name: 'qqblobname'
47
+ }
48
+ },
12
49
  log: function(str, level) {},
13
50
  onProgress: function(id, fileName, loaded, total){},
14
51
  onComplete: function(id, fileName, response, xhr){},
15
52
  onCancel: function(id, fileName){},
16
- onUpload: function(id, fileName, xhr){},
17
- onAutoRetry: function(id, fileName, response, xhr){}
53
+ onUpload: function(id, fileName){},
54
+ onUploadChunk: function(id, fileName, chunkData){},
55
+ onAutoRetry: function(id, fileName, response, xhr){},
56
+ onResume: function(id, fileName, chunkData){}
18
57
 
19
58
  };
20
- qq.extend(this._options, o);
59
+ qq.extend(options, o);
21
60
 
22
- this._queue = [];
61
+ log = options.log;
23
62
 
24
- this.log = this._options.log;
25
- };
26
- qq.UploadHandlerAbstract.prototype = {
27
63
  /**
28
- * Adds file or file input to the queue
29
- * @returns id
30
- **/
31
- add: function(file){},
32
- /**
33
- * Sends the file identified by id
64
+ * Removes element from queue, starts upload of next
34
65
  */
35
- upload: function(id){
36
- var len = this._queue.push(id);
66
+ dequeue = function(id) {
67
+ var i = qq.indexOf(queue, id),
68
+ max = options.maxConnections,
69
+ nextId;
37
70
 
38
- // if too many active uploads, wait...
39
- if (len <= this._options.maxConnections){
40
- this._upload(id);
41
- }
42
- },
43
- retry: function(id) {
44
- var i = qq.indexOf(this._queue, id);
45
71
  if (i >= 0) {
46
- this._upload(id);
47
- }
48
- else {
49
- this.upload(id);
50
- }
51
- },
52
- /**
53
- * Cancels file upload by id
54
- */
55
- cancel: function(id){
56
- this.log('Cancelling ' + id);
57
- this._options.paramsStore.remove(id);
58
- this._cancel(id);
59
- this._dequeue(id);
60
- },
61
- /**
62
- * Cancells all uploads
63
- */
64
- cancelAll: function(){
65
- for (var i=0; i<this._queue.length; i++){
66
- this._cancel(this._queue[i]);
72
+ queue.splice(i, 1);
73
+
74
+ if (queue.length >= max && i < max){
75
+ nextId = queue[max-1];
76
+ handlerImpl.upload(nextId);
77
+ }
67
78
  }
68
- this._queue = [];
69
- },
70
- /**
71
- * Returns name of the file identified by id
72
- */
73
- getName: function(id){},
74
- /**
75
- * Returns size of the file identified by id
76
- */
77
- getSize: function(id){},
78
- /**
79
- * Returns id of files being uploaded or
80
- * waiting for their turn
81
- */
82
- getQueue: function(){
83
- return this._queue;
84
- },
85
- reset: function() {
86
- this.log('Resetting upload handler');
87
- this._queue = [];
88
- },
89
- /**
90
- * Actual upload method
91
- */
92
- _upload: function(id){},
93
- /**
94
- * Actual cancel method
95
- */
96
- _cancel: function(id){},
97
- /**
98
- * Removes element from queue, starts upload of next
99
- */
100
- _dequeue: function(id){
101
- var i = qq.indexOf(this._queue, id);
102
- this._queue.splice(i, 1);
79
+ };
80
+
81
+ if (qq.isXhrUploadSupported()) {
82
+ handlerImpl = new qq.UploadHandlerXhr(options, dequeue, log);
83
+ }
84
+ else {
85
+ handlerImpl = new qq.UploadHandlerForm(options, dequeue, log);
86
+ }
87
+
88
+
89
+ return {
90
+ /**
91
+ * Adds file or file input to the queue
92
+ * @returns id
93
+ **/
94
+ add: function(file){
95
+ return handlerImpl.add(file);
96
+ },
97
+ /**
98
+ * Sends the file identified by id
99
+ */
100
+ upload: function(id){
101
+ var len = queue.push(id);
103
102
 
104
- var max = this._options.maxConnections;
103
+ // if too many active uploads, wait...
104
+ if (len <= options.maxConnections){
105
+ return handlerImpl.upload(id);
106
+ }
107
+ },
108
+ retry: function(id) {
109
+ var i = qq.indexOf(queue, id);
110
+ if (i >= 0) {
111
+ return handlerImpl.upload(id, true);
112
+ }
113
+ else {
114
+ return this.upload(id);
115
+ }
116
+ },
117
+ /**
118
+ * Cancels file upload by id
119
+ */
120
+ cancel: function(id) {
121
+ log('Cancelling ' + id);
122
+ options.paramsStore.remove(id);
123
+ handlerImpl.cancel(id);
124
+ dequeue(id);
125
+ },
126
+ /**
127
+ * Cancels all queued or in-progress uploads
128
+ */
129
+ cancelAll: function() {
130
+ var self = this,
131
+ queueCopy = [];
105
132
 
106
- if (this._queue.length >= max && i < max){
107
- var nextId = this._queue[max-1];
108
- this._upload(nextId);
133
+ qq.extend(queueCopy, queue);
134
+ qq.each(queueCopy, function(idx, fileId) {
135
+ self.cancel(fileId);
136
+ });
137
+
138
+ queue = [];
139
+ },
140
+ /**
141
+ * Returns name of the file identified by id
142
+ */
143
+ getName: function(id){
144
+ return handlerImpl.getName(id);
145
+ },
146
+ /**
147
+ * Returns size of the file identified by id
148
+ */
149
+ getSize: function(id){
150
+ if (handlerImpl.getSize) {
151
+ return handlerImpl.getSize(id);
152
+ }
153
+ },
154
+ getFile: function(id) {
155
+ if (handlerImpl.getFile) {
156
+ return handlerImpl.getFile(id);
157
+ }
158
+ },
159
+ /**
160
+ * Returns id of files being uploaded or
161
+ * waiting for their turn
162
+ */
163
+ getQueue: function(){
164
+ return queue;
165
+ },
166
+ reset: function() {
167
+ log('Resetting upload handler');
168
+ queue = [];
169
+ handlerImpl.reset();
170
+ },
171
+ getUuid: function(id) {
172
+ return handlerImpl.getUuid(id);
173
+ },
174
+ /**
175
+ * Determine if the file exists.
176
+ */
177
+ isValid: function(id) {
178
+ return handlerImpl.isValid(id);
179
+ },
180
+ getResumableFilesData: function() {
181
+ if (handlerImpl.getResumableFilesData) {
182
+ return handlerImpl.getResumableFilesData();
183
+ }
184
+ return [];
109
185
  }
110
- },
111
- /**
112
- * Determine if the file exists.
113
- */
114
- isValid: function(id) {}
186
+ };
115
187
  };