forked from rico345100/socket.io-file-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.io-file-client.js
298 lines (257 loc) · 7.26 KB
/
socket.io-file-client.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
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
"use strict";
(function() {
var instanceId = 0;
function getInstanceId() {
return instanceId++;
}
// note that this function invoked from call/apply, which has "this" binded
function _upload(file, options) {
options = options || {};
var self = this;
var socket = this.socket;
var chunkSize = this.chunkSize;
var transmissionDelay = this.transmissionDelay;
var uploadId = file.uploadId;
var uploadTo = options.uploadTo || '';
var data = options.data || {};
var fileInfo = {
id: uploadId,
name: file.name,
size: file.size,
chunkSize: chunkSize,
sent: 0,
data: data
};
uploadTo && (fileInfo.uploadTo = uploadTo);
// read file
var fileReader = new FileReader();
fileReader.onloadend = function() {
var buffer = fileReader.result;
// check file mime type if exists
if(self.accepts && self.accepts.length > 0) {
var found = false;
for(var i = 0; i < self.accepts.length; i++) {
var accept = self.accepts[i];
if(file.type === accept) {
found = true;
break;
}
}
if(!found) {
return self.emit('error',
new Error('Not Acceptable file type ' + file.type + ' of ' + file.name + '. Type must be one of these: ' + self.accepts.join(', ')),
{
uploadId: fileInfo.id,
name: fileInfo.name,
size: fileInfo.size,
type: file.type,
uploadTo: uploadTo,
data: data
});
}
}
// check file size
if(self.maxFileSize && self.maxFileSize > 0) {
if(file.size > +self.maxFileSize) {
return self.emit('error',
new Error('Max Uploading File size must be under ' + self.maxFileSize + ' byte(s).'),
{
uploadId: fileInfo.id,
name: fileInfo.name,
size: fileInfo.size,
uploadTo: uploadTo,
data: data
});
}
}
// put into uploadingFiles list
self.uploadingFiles[uploadId] = fileInfo;
// request the server to make a file
self.emit('start', {
uploadId: fileInfo.id,
name: fileInfo.name,
size: fileInfo.size,
uploadTo: uploadTo,
data: data
});
socket.emit('socket.io-file::createFile', fileInfo);
function sendChunk() {
if(fileInfo.aborted) {
return;
}
if(fileInfo.sent >= buffer.byteLength) {
socket.emit('socket.io-file::done::' + uploadId);
return;
}
var chunk = buffer.slice(fileInfo.sent, fileInfo.sent + chunkSize);
self.emit('stream', {
uploadId: fileInfo.id,
name: fileInfo.name,
size: fileInfo.size,
sent: fileInfo.sent,
uploadTo: uploadTo,
data: data
});
socket.once('socket.io-file::request::' + uploadId, sendChunk);
socket.emit('socket.io-file::stream::' + uploadId, chunk);
fileInfo.sent += chunk.byteLength;
self.uploadingFiles[uploadId] = fileInfo;
}
socket.once('socket.io-file::request::' + uploadId, sendChunk);
socket.on('socket.io-file::complete::' + uploadId, function(info) {
info.uploadId = fileInfo.id;
info.data = fileInfo.data;
self.emit('complete', info);
socket.removeAllListeners('socket.io-file::abort::' + uploadId);
socket.removeAllListeners('socket.io-file::error::' + uploadId);
socket.removeAllListeners('socket.io-file::complete::' + uploadId);
// remove from uploadingFiles list
delete self.uploadingFiles[uploadId];
});
socket.on('socket.io-file::abort::' + uploadId, function(info) {
fileInfo.aborted = true;
self.emit('abort', {
uploadId: fileInfo.id,
name: fileInfo.name,
size: fileInfo.size,
sent: fileInfo.sent,
wrote: info.wrote,
uploadTo: uploadTo,
data: data
});
});
socket.on('socket.io-file::error::' + uploadId, function(err) {
self.emit('error',
new Error(err.message),
{
uploadId: fileInfo.id,
name: fileInfo.name,
size: fileInfo.size,
uploadTo: uploadTo,
data: data
});
});
};
fileReader.readAsArrayBuffer(file);
}
function SocketIOFileClient(socket, options) {
if(!socket) {
return this.emit('error', new Error('SocketIOFile requires Socket.'));
}
this.instanceId = getInstanceId(); // using for identifying multiple file upload from SocketIOFileClient objects
this.uploadId = 0; // using for identifying each uploading
this.ev = {}; // event handlers
this.options = options || {};
this.accepts = [];
this.maxFileSize = undefined;
this.socket = socket;
this.uploadingFiles = {};
var self = this;
socket.once('socket.io-file::recvSync', function(settings) {
self.maxFileSize = settings.maxFileSize || undefined;
self.accepts = settings.accepts || [];
self.chunkSize = settings.chunkSize || 10240;
self.transmissionDelay = settings.transmissionDelay || 0;
self.emit('ready');
});
socket.emit('socket.io-file::reqSync');
}
SocketIOFileClient.prototype.getUploadId = function() {
return 'u_' + this.uploadId++;
}
SocketIOFileClient.prototype.upload = function(fileEl, options) {
if(!fileEl ||
(fileEl.files && fileEl.files.length <= 0) ||
fileEl.length <= 0
) {
this.emit('error', new Error('No file(s) to upload.'));
return [];
}
var self = this;
var uploadIds = [];
var files = fileEl.files ? fileEl.files : fileEl;
var loaded = 0;
for(var i = 0; i < files.length; i++) {
var file = files[i];
var uploadId = this.getUploadId();
uploadIds.push(uploadId);
file.uploadId = uploadId;
_upload.call(self, file, options);
}
return uploadIds;
};
SocketIOFileClient.prototype.on = function(evName, fn) {
if(!this.ev[evName]) {
this.ev[evName] = [];
}
this.ev[evName].push(fn);
return this;
};
SocketIOFileClient.prototype.off = function(evName, fn) {
if(typeof evName === 'undefined') {
this.ev = [];
}
else if(typeof fn === 'undefined') {
if(this.ev[evName]) {
delete this.ev[evName];
}
}
else {
var evList = this.ev[evName] || [];
for(var i = 0; i < evList.length; i++) {
if(evList[i] === fn) {
evList = evList.splice(i, 1);
break;
}
}
}
return this;
};
SocketIOFileClient.prototype.emit = function(evName) {
var evList = this.ev[evName] || [];
var args = Array.from(arguments);
args.splice(0, 1); // Don't pass evName to the event handler.
for(var i = 0; i < evList.length; i++) {
evList[i].apply(null, args);
}
return this;
};
SocketIOFileClient.prototype.abort = function(id) {
var socket = this.socket;
socket.emit('socket.io-file::abort::' + id);
};
SocketIOFileClient.prototype.destroy = function() {
var uploadingFiles = this.uploadingFiles;
for(var key in uploadingFiles) {
this.abort(key);
}
this.socket = null;
this.uploadingFiles = null;
this.ev = null;
};
SocketIOFileClient.prototype.getUploadInfo = function() {
return JSON.parse(JSON.stringify(this.uploadingFiles));
};
// module export
// CommonJS
if (typeof exports === "object" && typeof module !== "undefined") {
module.exports = SocketIOFileClient;
}
// RequireJS
else if (typeof define === "function" && define.amd) {
define(['SocketIOFileClient'], SocketIOFileClient);
}
else {
var g;
if (typeof window !== "undefined") {
g = window;
}
else if (typeof global !== "undefined") {
g = global;
}
else if (typeof self !== "undefined") {
g = self;
}
g.SocketIOFileClient = SocketIOFileClient;
}
})();