-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
213 lines (188 loc) · 7.2 KB
/
index.html
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
<!DOCTYPE html>
<html ng-app>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Video Uploader</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="/socket.io/socket.io.js"></script>
<style type="text/css">
#dropzone{
height: 300px;
width: 300px;
border: 10px dotted black;
}
#dropzone:hover{
background: yellow;
}
</style>
</head>
<body>
<div ng-controller="UploadController">
<div id="dropzone">
<input type="file" id="browsezone"/>
</div>
<div>items</div>
<table>
<tr>
<th>title</th>
<th>progress</th>
<th>token</th>
<th>type</th>
<th>status</th>
</tr>
<tr ng-repeat="item in uploader.getItems()">
<td>{{ item.title }}</td>
<td>{{ item.progress }}</td>
<td>{{ item.token }}</td>
<td>{{ item.type }}</td>
<td>{{ item.status }}</td>
</tr>
</table>
</div>
<script type="text/javascript" charset="utf-8">
var BramCo = {};
BramCo.Uploader = {
location : 'localhost:1337',
socket : io.connect(location),
items : {},
getItems: function(){
return this.items;
},
activeItem : null,
sliceReader : new FileReader(),
dropzoneId : 'dropzone',
browsezoneId : 'browsezone',
init : function(){
this.dropzone = document.getElementById(this.dropzoneId)
this.dropzone.addEventListener("dragenter", this.eventHole, false);
this.dropzone.addEventListener("dragexit", this.eventHole, false);
this.dropzone.addEventListener("dragover", this.eventHole, false);
this.dropzone.addEventListener("drop", this.filesDropped, false);
this.browsezone = document.getElementById(this.browsezoneId)
this.browsezone.addEventListener('change', this.filesBrowsed);
//when a new slice is read it's send over the socket with the token of the active file
this.sliceReader.onload = function(event){
var item = BramCo.Uploader.activeItem;
BramCo.Uploader.angularScope.$apply(function () {
item.progress = Math.round((item.slicePointer / item.file.size) * 100);
});
BramCo.Uploader.socket.emit('SendSlice', { 'token' : item.token, 'slice' : event.target.result });
}
this.socket.on('WantSlice', function (sliceRequest){
var item = BramCo.Uploader.items[sliceRequest['token']];
BramCo.Uploader.activeItem = item;
item.slicePointer = sliceRequest['pointer'];
var sliceEnd = (item.slicePointer + sliceRequest['size']);
if( sliceEnd > item.file.size ) {
sliceEnd = item.file.size;
}
console.log("sending slice from byte: " + item.slicePointer + " to " + sliceEnd);
var slice = item.file.slice(item.slicePointer, sliceEnd);
BramCo.Uploader.sliceReader.readAsBinaryString(slice);
});
this.socket.on('FileCompleted', function (data){
var item = BramCo.Uploader.items[data['token']];
BramCo.Uploader.angularScope.$apply(function () {
item.status = 1;
item.progress = 100;
BramCo.Uploader.activeItem = false;
});
BramCo.Uploader.sendNextFile();
});
},
sendNextFile : function () {
var token, item;
for (token in this.items) {
item = this.items[token];
if(!this.activeItem && item.status === -1){
this.sendNewFile(item);
}
}
},
sendNewFile: function(item){
this.activeItem = item;
this.socket.emit('NewFile', {
'checksum' : item.title,
'size' : item.file.size,
'token' : item.token,
'type' : item.type
});
},
filesDropped: function(event){
event.stopPropagation();
event.preventDefault();
var files = event.dataTransfer.files,
fileCounter = files.length;
while(fileCounter--){
BramCo.Uploader.addItem(files[fileCounter]);
}
},
filesBrowsed: function(event){
var files = event.target.files,
fileCounter = files.length;
while(fileCounter--){
BramCo.Uploader.addItem(files[fileCounter]);
}
},
addItem:function(file){
var item = {
file: file,
type: file.name.split('.').pop(),
// cut the extension off of the the filename
title: file.name.slice(0, file.name.lastIndexOf('.')),
token: (file.name.slice(0, file.name.lastIndexOf('.')) + file.size),//this.canIhasToken(),
//checksum: this.canIhasChecksum(),
// guess the type of the file on the client side, should be verified on the server side
slicePointer: 0,
progress : 0,
// -1: idle, 0: uploading, 1: complete
status : -1
};
this.angularScope.$apply(function () {
BramCo.Uploader.items[item.token] = item;
});
if(!this.activeItem){
this.sendNewFile(item);
}
},
// drop your unwanted events in this hole
eventHole: function(event){
event.stopPropagation();
event.preventDefault();
},
// THIS IS NOT A REAL CHECKSUM
// I'm just returning the whole file or the bytes at the middle of the file encoded as utf-8
// this is used to check the uniqueness of a local file and prevent double uploads
canIhasChecksum:function(file){
var bytez,
slicer = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
reader = new FileReader(),
begin = 0,
end = file.size;
//if the file is bigger then half a megabyte, use its middle part
if(end > 524288){
begin = Math.round((end / 2) - 262144);
end = begin + 524288;
}
bytez = slicer.call(file, begin, end);
// read as text, should default to utf-8
return reader.readAsText(bytez);
}
};
window.addEventListener("load", Ready);
function Ready(){
if(window.File && window.FileReader){
BramCo.Uploader.init();
}
else
{
alert("Your Browser Doesn't Support The File API Please Update Your Browser");
}
};
function UploadController($scope){
$scope.uploader = BramCo.Uploader;
BramCo.Uploader.angularScope = $scope;
};
</script>
</body>
</html>