forked from adambiggs/jquery-upchunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.upchunk.coffee
231 lines (203 loc) · 6.36 KB
/
jquery.upchunk.coffee
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
# Project: Upchunk
# Description: An HTML5 file uploader with chunk and fallback support
# Author: Chris Bolton
# License: WTFPL
# the semi-colon before function invocation is a safety net against concatenated
# scripts and/or other plugins which may not be closed properly.
``
(($, window, document) ->
pluginName = 'upchunk'
defaults =
url: '',
chunk: false,
fallback_id: '',
chunk_size: 1024,
file_param: 'file',
name_param: 'file_name',
headers: {},
max_file_size: 0,
queue_size: 2,
processNextImmediately: false,
data: {},
drop: ->,
dragEnter: ->,
dragOver: ->,
dragLeave: ->,
docEnter: ->,
docOver: ->,
docLeave: ->,
beforeEach: ->,
afterAll: ->
rename: (file) -> file.name,
error: (err) -> alert err,
fileAdded: ->,
uploadStarted: ->,
uploadFinished: ->,
progressUpdated: ->
class Plugin
constructor: (@element, options) ->
@opts = $.extend {}, defaults, options
@_defaults = defaults
@_name = pluginName
@errors =
notSupported: 'BrowserNotSupported',
tooLarge: 'FileTooLarge'
uploadHalted: 'UploadHalted'
@hash = (s) ->
hash = 0
len = s.length
return hash if len == 0
for i in [0..len]
char = s.charCodeAt(i)
test = ((hash<<5)-hash)+char
hash = test & test unless isNaN(test)
Math.abs(hash)
@init()
init: ->
@processQ = []
@todoQ = []
$(@element).on('drop', @drop).on('dragenter', @dragEnter).on('dragover', @dragOver).on('dragleave', @dragLeave)
$(document).on('drop', @docDrop).on('dragenter', @docEnter).on('dragover', @docOver).on('dragleave', @docLeave)
$('#' + @opts.fallback_id).change( @drop )
process: (file) =>
next_file = =>
# Remove file element from list
@processQ.splice(@processQ.indexOf(file),1)
# ... and go to the next
next = @todoQ.shift()
if next
next_hash = (@hash(next.name) + next.size).toString()
@opts.uploadStarted(next, next_hash)
@processQ.push(next)
@process(next)
progress = (e) =>
old = 0 if !old?
if n?
pchunk = chunk_size * 100 / file.size
percentage = Math.floor(((e.loaded * 100) / file.size) + (n - 1) * pchunk)
else
percentage = Math.floor(e.loaded * 100 / e.total)
percentage = 100 if percentage > 100
if percentage > old
old = percentage
hash = (@hash(file.name) + file.size).toString()
@opts.progressUpdated(file, hash, percentage)
next_file() if percentage == 100 && @opts.processNextImmediately
next_chunk = =>
start = chunk_size * n
end = chunk_size * (n + 1)
n += 1
if file.slice
chunk = file.slice(start, end)
else if file.mozSlice
chunk = file.mozSlice(start, end)
else if file.webkitSlice
chunk = file.webkitSlice(start, end)
else
chunk = file
chunks = 1
send(chunk, @opts.url)
send = (chunk, url) =>
hash = (@hash(file.name) + file.size).toString()
if @opts.beforeEach() is false
@opts.error(@errors.uploadHalted)
next_file()
return false
fd = new FormData
fd.append(@opts.file_param, chunk)
fd.append(@opts.name_param, @opts.rename(file))
fd.append('hash', hash)
for name, value of @opts.data
if typeof value == 'function'
fd.append(name, value())
else
fd.append(name, value)
if n == 1
fd.append('first', true)
else
fd.append('first', false)
if n == chunks
fd.append('last', true)
else
fd.append('last', false)
xhr = new XMLHttpRequest
xhr.open('POST', url, true)
for key,value of @opts.headers
xhr.setRequestHeader(key, value)
xhr.upload.addEventListener('progress', progress, false)
xhr.send(fd)
xhr.onload = =>
if chunks? && n < chunks
next_chunk()
else
try response = $.parseJSON(xhr.responseText)
if response?
@opts.uploadFinished(file, hash, response)
else
@opts.uploadFinished(file, hash)
next_file() unless @opts.processNextImmediately
@opts.afterAll() if @processQ.length == 0
if @opts.max_file_size > 0 && file.size > 1048576 * @opts.max_file_size
@opts.error(@errors.tooLarge)
next_file()
return false
if @opts.chunk == false
send(file, @opts.url)
else
chunk_size = 1024 * @opts.chunk_size
chunks = Math.ceil(file.size / chunk_size)
n = 0
next_chunk()
drop: (e) =>
@docLeave(e)
@opts.drop(e)
@files = if e.originalEvent.dataTransfer? then e.originalEvent.dataTransfer.files else e.target.files
unless @files
@opts.error(@errors.notSupported)
false
@todoQ.push(file) for file in @files
while @todoQ.length and @processQ.length < @opts.queue_size
file = @todoQ.shift()
if file
hash = (@hash(file.name) + file.size).toString()
@opts.uploadStarted(file, hash)
@processQ.push(file)
@process(file)
e.preventDefault()
false
dragEnter: (e) =>
clearTimeout(@timer)
e.preventDefault()
@opts.dragEnter(e)
dragOver: (e) =>
clearTimeout(@timer)
e.preventDefault()
@opts.docOver(e)
@opts.dragOver(e)
dragLeave: (e) =>
clearTimeout(@timer)
@opts.dragLeave(e)
e.stopPropagation()
docDrop: (e) =>
e.preventDefault()
@opts.docLeave(e)
false
docEnter: (e) =>
clearTimeout(@timer)
e.preventDefault()
@opts.docEnter(e)
false
docOver: (e) =>
clearTimeout(@timer)
e.preventDefault()
@opts.docOver(e)
false
docLeave: (e) =>
@timer = setTimeout((=> @opts.docLeave(e)), 200)
# A really lightweight plugin wrapper around the constructor,
# preventing against multiple instantiations
$.fn[pluginName] = (options) ->
this.each ->
if !$.data(this, "plugin_#{pluginName}")
$.data(this, "plugin_#{pluginName}", new Plugin(this, options))
)(jQuery, window, document)