-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunkedfile.js
126 lines (111 loc) · 2.92 KB
/
chunkedfile.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
(function(root, factory) {
if(typeof define === 'function' && define.amd){
define(factory);
} else if(typeof exports === 'object') {
module.exports = factory();
} else {
root.chunkedfile = factory;
}
}(this, function() {
//config: {"filename": string, "metadata": {"type": string}}
function ChunkedFile(config, success, error) {
var self = this;
self.filename = config.filename;
config.metadata = config.metadata || {type: "text/plain"};
self.chunkIndices = []; //array of chunk indexes
self.chunks = [];
self.size = function() {
return self.chunks.length;
}
self.addChunk = function(chunk, success, error, index) {
if(!index) index = self.size();
self.chunks[index] = chunk;
if(success) success(index);
}
self.getChunk = function(index, success, error) {
var chunk = self.chunks[index];
if(chunk) {
if(success) success(chunk);
} else {
var e = new Error("Chunk with index " + index + " not found");
if(error) error(e); else throw e;
}
self.store.get(index, function(chunk) {
if(success) success(chunk);
}, error);
}
self.getFile = function(success, error, indices) {
var chunks = self.chunks;
if(indices) {
chunks = [];
for(i in indices) {
var chunk = self.chunks[i];
if(chunk) {
chunks.push(chunk);
} else {
var e = new Error("Chunk with index " + index + " not found");
if(error) error(e); else throw e;
return;
}
}
}
var file;
try {
file = new File(chunks, config.filename, config.metadata);
} catch (e) {
file = new Blob(chunks, config.metadata);
}
if(success) success(file);
}
self.close = function() {
self.store.close();
}
}
var splitWorker = URL.createObjectURL(new Blob(['(',function() {
onmessage = function(event) {
var message = event.data;
var blob = message.file;
var chunksize = message.chunksize;
var start = 0;
while(start < blob.size) {
var end = start + chunksize;
postMessage({
'chunk': blob.slice(start, end, blob.type),
'type': 'chunk'
});
start = end;
}
postMessage({
'type': 'success'
})
}
}.toString(),')()'], {type: 'application/javascript'}));
ChunkedFile.fromFile = function(args, success, error) {
//args can be the just file as well, check typeof
if(!args.file) {
var e = new Error("File is not defined");
if(error) error(e); else throw e;
}
var file = args.file;
args.chunksize = args.chunksize || 1024*1024;
var chunkedFile = new ChunkedFile({
"filename": file.name,
"metadata": {type: file.type}
});
var worker = new Worker(splitWorker);
worker.onmessage = function(event) {
var message = event.data;
switch(message.type) {
case 'chunk':
chunkedFile.addChunk(message.chunk);
break;
case 'success':
worker.terminate();
if(success) success(chunkedFile);
break;
}
}
worker.postMessage(args);
}
return ChunkedFile;
}));