tus-server 2.2.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +181 -85
- data/README.md +152 -300
- data/lib/tus/checksum.rb +4 -28
- data/lib/tus/errors.rb +2 -0
- data/lib/tus/info.rb +23 -14
- data/lib/tus/input.rb +53 -8
- data/lib/tus/locker.rb +108 -0
- data/lib/tus/response.rb +1 -1
- data/lib/tus/server.rb +225 -159
- data/lib/tus/storage/filesystem.rb +25 -8
- data/lib/tus/storage/s3.rb +106 -125
- data/tus-server.gemspec +6 -7
- metadata +13 -33
- data/lib/tus/input/unicorn.rb +0 -16
- data/lib/tus/storage/gridfs.rb +0 -278
data/lib/tus/server.rb
CHANGED
|
@@ -4,6 +4,7 @@ require "roda"
|
|
|
4
4
|
require "rack/rewindable_input"
|
|
5
5
|
|
|
6
6
|
require "tus/storage/filesystem"
|
|
7
|
+
require "tus/locker"
|
|
7
8
|
require "tus/info"
|
|
8
9
|
require "tus/input"
|
|
9
10
|
require "tus/checksum"
|
|
@@ -18,7 +19,7 @@ module Tus
|
|
|
18
19
|
class Server < Roda
|
|
19
20
|
SUPPORTED_VERSIONS = ["1.0.0"]
|
|
20
21
|
SUPPORTED_EXTENSIONS = [
|
|
21
|
-
"creation", "creation-defer-length",
|
|
22
|
+
"creation", "creation-defer-length", "creation-with-upload",
|
|
22
23
|
"termination",
|
|
23
24
|
"expiration",
|
|
24
25
|
"concatenation",
|
|
@@ -33,6 +34,8 @@ module Tus
|
|
|
33
34
|
opts[:disposition] = "inline"
|
|
34
35
|
opts[:redirect_download] = nil
|
|
35
36
|
opts[:hooks] = {}
|
|
37
|
+
opts[:locker] = nil
|
|
38
|
+
opts[:lock_timeout] = 20
|
|
36
39
|
|
|
37
40
|
plugin :all_verbs
|
|
38
41
|
plugin :default_headers, {"Content-Type" => ""}
|
|
@@ -64,20 +67,12 @@ module Tus
|
|
|
64
67
|
|
|
65
68
|
r.is ['', true] do
|
|
66
69
|
# OPTIONS /
|
|
67
|
-
r.options
|
|
68
|
-
response.headers.update(
|
|
69
|
-
"Tus-Version" => SUPPORTED_VERSIONS.join(","),
|
|
70
|
-
"Tus-Extension" => SUPPORTED_EXTENSIONS.join(","),
|
|
71
|
-
"Tus-Max-Size" => max_size.to_s,
|
|
72
|
-
"Tus-Checksum-Algorithm" => SUPPORTED_CHECKSUM_ALGORITHMS.join(","),
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
no_content!
|
|
76
|
-
end
|
|
70
|
+
r.options { server_info! }
|
|
77
71
|
|
|
78
72
|
# POST /
|
|
79
73
|
r.post do
|
|
80
|
-
|
|
74
|
+
validate_upload_defer_length! if request.headers["Upload-Defer-Length"]
|
|
75
|
+
validate_upload_length! unless request.headers["Upload-Concat"].to_s.start_with?("final") || request.headers["Upload-Defer-Length"]
|
|
81
76
|
validate_upload_metadata! if request.headers["Upload-Metadata"]
|
|
82
77
|
validate_upload_concat! if request.headers["Upload-Concat"]
|
|
83
78
|
|
|
@@ -93,10 +88,20 @@ module Tus
|
|
|
93
88
|
|
|
94
89
|
before_create(uid, info)
|
|
95
90
|
|
|
91
|
+
# creation-with-upload: the request body holds the first chunk of data
|
|
92
|
+
upload_data = request.content_type == RESUMABLE_CONTENT_TYPE &&
|
|
93
|
+
request.content_length != "0" &&
|
|
94
|
+
info.length != 0
|
|
95
|
+
|
|
96
|
+
if upload_data
|
|
97
|
+
error!(400, "Cannot upload data to a final upload") if info.final?
|
|
98
|
+
validate_content_length!(request.content_length.to_i, info) if request.content_length
|
|
99
|
+
end
|
|
100
|
+
|
|
96
101
|
if info.final?
|
|
97
|
-
validate_partial_uploads!(info.partial_uploads)
|
|
102
|
+
length = validate_partial_uploads!(info.partial_uploads)
|
|
98
103
|
|
|
99
|
-
|
|
104
|
+
storage.concatenate(uid, info.partial_uploads, info.to_h)
|
|
100
105
|
info["Upload-Length"] = length.to_s
|
|
101
106
|
info["Upload-Offset"] = length.to_s
|
|
102
107
|
else
|
|
@@ -105,132 +110,191 @@ module Tus
|
|
|
105
110
|
|
|
106
111
|
after_create(uid, info)
|
|
107
112
|
|
|
113
|
+
# an empty upload is complete as soon as it's created
|
|
114
|
+
finish_upload!(uid, info) if info.length == 0 && !info.final?
|
|
115
|
+
|
|
108
116
|
storage.update_info(uid, info.to_h)
|
|
117
|
+
|
|
118
|
+
if upload_data
|
|
119
|
+
uploaded = false
|
|
120
|
+
|
|
121
|
+
begin
|
|
122
|
+
upload_data!(uid, info)
|
|
123
|
+
uploaded = true
|
|
124
|
+
ensure
|
|
125
|
+
# When the data is rejected the client doesn't get the upload URL,
|
|
126
|
+
# so we remove the upload instead of leaving it behind.
|
|
127
|
+
unless uploaded
|
|
128
|
+
storage.delete_file(uid, info.to_h)
|
|
129
|
+
after_terminate(uid, info)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
storage.update_info(uid, info.to_h)
|
|
134
|
+
end
|
|
135
|
+
|
|
109
136
|
response.headers.update(info.headers)
|
|
110
137
|
|
|
111
|
-
|
|
138
|
+
# without the query string, which Rack::Request#url includes
|
|
139
|
+
file_url = "#{request.base_url}#{request.path.chomp("/")}/#{uid}"
|
|
112
140
|
created!(file_url)
|
|
113
141
|
end
|
|
114
142
|
end
|
|
115
143
|
|
|
116
|
-
|
|
144
|
+
# Upload ids are generated by the server, so anything that isn't made of
|
|
145
|
+
# word characters (e.g. "..") cannot be an upload.
|
|
146
|
+
r.is(/(\w+)/) do |uid|
|
|
117
147
|
# OPTIONS /{uid}
|
|
118
|
-
r.options
|
|
119
|
-
response.headers.update(
|
|
120
|
-
"Tus-Version" => SUPPORTED_VERSIONS.join(","),
|
|
121
|
-
"Tus-Extension" => SUPPORTED_EXTENSIONS.join(","),
|
|
122
|
-
"Tus-Max-Size" => max_size.to_s,
|
|
123
|
-
"Tus-Checksum-Algorithm" => SUPPORTED_CHECKSUM_ALGORITHMS.join(","),
|
|
124
|
-
)
|
|
148
|
+
r.options { server_info! }
|
|
125
149
|
|
|
126
|
-
|
|
127
|
-
|
|
150
|
+
# GET /{uid}
|
|
151
|
+
r.get do
|
|
152
|
+
info = read_info!(uid)
|
|
128
153
|
|
|
129
|
-
|
|
130
|
-
info = Tus::Info.new(storage.read_info(uid))
|
|
131
|
-
rescue Tus::NotFound
|
|
132
|
-
error!(404, "Upload Not Found")
|
|
133
|
-
end
|
|
154
|
+
validate_upload_finished!(info)
|
|
134
155
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
156
|
+
if redirect_download
|
|
157
|
+
redirect_url = instance_exec(uid, info.to_h,
|
|
158
|
+
content_type: info.type,
|
|
159
|
+
content_disposition: ContentDisposition.(disposition: opts[:disposition], filename: info.name),
|
|
160
|
+
&redirect_download)
|
|
161
|
+
|
|
162
|
+
r.redirect redirect_url
|
|
163
|
+
else
|
|
164
|
+
range = handle_range_request!(info.length)
|
|
139
165
|
|
|
140
|
-
|
|
166
|
+
response.headers["Content-Disposition"] = ContentDisposition.(disposition: opts[:disposition], filename: info.name)
|
|
167
|
+
response.headers["Content-Type"] = info.type if info.type
|
|
168
|
+
response.headers["ETag"] = %(W/"#{uid}")
|
|
169
|
+
|
|
170
|
+
body = storage.get_file(uid, info.to_h, range: range)
|
|
171
|
+
|
|
172
|
+
r.halt response.finish_with_body(body)
|
|
173
|
+
end
|
|
141
174
|
end
|
|
142
175
|
|
|
143
|
-
# PATCH
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
176
|
+
# HEAD, PATCH and DELETE run while holding the upload's lock, and read
|
|
177
|
+
# the upload info only once they have it.
|
|
178
|
+
lock_upload(uid) do
|
|
179
|
+
info = read_info!(uid)
|
|
180
|
+
|
|
181
|
+
# HEAD /{uid}
|
|
182
|
+
r.head do
|
|
183
|
+
response.headers.update(info.headers)
|
|
184
|
+
response.headers["Cache-Control"] = "no-store"
|
|
147
185
|
|
|
148
|
-
|
|
149
|
-
info["Upload-Defer-Length"] = nil
|
|
186
|
+
no_content!
|
|
150
187
|
end
|
|
151
188
|
|
|
152
|
-
|
|
189
|
+
# PATCH /{uid}
|
|
190
|
+
r.patch do
|
|
191
|
+
error!(403, "Cannot modify a final upload") if info.final?
|
|
153
192
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
validate_content_length!(request.content_length.to_i, info) if request.content_length
|
|
193
|
+
validate_content_type!
|
|
194
|
+
validate_upload_offset!(info)
|
|
157
195
|
|
|
158
|
-
|
|
159
|
-
if request.headers["Upload-Checksum"]
|
|
160
|
-
input = Rack::RewindableInput.new(input) unless input.rewindable?
|
|
161
|
-
validate_upload_checksum!(input)
|
|
162
|
-
end
|
|
196
|
+
error!(403, "Cannot modify completed upload") if info.offset == info.length
|
|
163
197
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
198
|
+
# The length of a deferred-length upload can also be set by a request
|
|
199
|
+
# without data, once the upload already has all of its data.
|
|
200
|
+
if info.defer_length? && request.headers["Upload-Length"]
|
|
201
|
+
validate_upload_length!
|
|
202
|
+
|
|
203
|
+
info["Upload-Length"] = request.headers["Upload-Length"]
|
|
204
|
+
info["Upload-Defer-Length"] = nil
|
|
205
|
+
end
|
|
168
206
|
|
|
169
|
-
|
|
170
|
-
info["Upload-Expires"] = (Time.now + expiration_time).httpdate
|
|
207
|
+
upload_data!(uid, info)
|
|
171
208
|
|
|
172
|
-
|
|
173
|
-
|
|
209
|
+
storage.update_info(uid, info.to_h)
|
|
210
|
+
response.headers.update(info.headers)
|
|
174
211
|
|
|
175
|
-
|
|
212
|
+
no_content!
|
|
176
213
|
end
|
|
177
214
|
|
|
178
|
-
|
|
179
|
-
|
|
215
|
+
# DELETE /{uid}
|
|
216
|
+
r.delete do
|
|
217
|
+
storage.delete_file(uid, info.to_h)
|
|
218
|
+
|
|
219
|
+
after_terminate(uid, info)
|
|
180
220
|
|
|
181
|
-
|
|
221
|
+
no_content!
|
|
222
|
+
end
|
|
182
223
|
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
183
226
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
227
|
+
# Yields while holding the lock for the given upload. If another request in
|
|
228
|
+
# this process holds the lock, it's asked to stop reading its request body
|
|
229
|
+
# and save what it received. Responds with 423 Locked if the lock isn't
|
|
230
|
+
# released within the lock timeout.
|
|
231
|
+
def lock_upload(uid, &block)
|
|
232
|
+
locker.lock(uid, timeout: lock_timeout, release: method(:interrupt_upload), &block)
|
|
233
|
+
rescue Tus::Locked
|
|
234
|
+
response.headers["Retry-After"] = "1"
|
|
235
|
+
error!(423, "Upload is locked by another request")
|
|
236
|
+
end
|
|
187
237
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
238
|
+
# Called when another request wants the lock this request holds. Makes the
|
|
239
|
+
# request body appear finished, so that the data received so far is saved
|
|
240
|
+
# and the lock is released.
|
|
241
|
+
def interrupt_upload
|
|
242
|
+
@upload_interrupted = true
|
|
243
|
+
@upload_input.interrupt! if @upload_input
|
|
244
|
+
end
|
|
191
245
|
|
|
192
|
-
|
|
193
|
-
|
|
246
|
+
def read_info!(uid)
|
|
247
|
+
Tus::Info.new(storage.read_info(uid))
|
|
248
|
+
rescue Tus::NotFound
|
|
249
|
+
error!(404, "Upload Not Found")
|
|
250
|
+
end
|
|
194
251
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
252
|
+
# Writes the request body to the upload at its current offset, then updates
|
|
253
|
+
# the offset and expiration, and finalizes the upload if it's complete.
|
|
254
|
+
# Shared by PATCH and creation-with-upload POST requests.
|
|
255
|
+
def upload_data!(uid, info)
|
|
256
|
+
input = get_input(info)
|
|
200
257
|
|
|
201
|
-
|
|
202
|
-
else
|
|
203
|
-
range = handle_range_request!(info.length)
|
|
258
|
+
validate_content_length!(request.content_length.to_i, info) if request.content_length
|
|
204
259
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
response.headers["Content-Type"] = content_type
|
|
260
|
+
begin
|
|
261
|
+
body = input
|
|
208
262
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
end
|
|
263
|
+
if request.headers["Upload-Checksum"]
|
|
264
|
+
body = Rack::RewindableInput.new(input) unless input.rewindable?
|
|
265
|
+
validate_upload_checksum!(body)
|
|
213
266
|
end
|
|
214
267
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
268
|
+
bytes_uploaded = storage.patch_file(uid, body, info.to_h)
|
|
269
|
+
rescue Tus::MaxSizeExceeded
|
|
270
|
+
validate_content_length!(input.pos, info)
|
|
271
|
+
rescue Tus::NotFound
|
|
272
|
+
error!(404, "Upload Not Found")
|
|
273
|
+
end
|
|
218
274
|
|
|
219
|
-
|
|
275
|
+
info["Upload-Offset"] = (info.offset + bytes_uploaded).to_s
|
|
276
|
+
info["Upload-Expires"] = (Time.now + expiration_time).httpdate
|
|
220
277
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
278
|
+
finish_upload!(uid, info) if info.offset == info.length # last chunk
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def finish_upload!(uid, info)
|
|
282
|
+
storage.finalize_file(uid, info.to_h) if storage.respond_to?(:finalize_file)
|
|
283
|
+
|
|
284
|
+
after_finish(uid, info)
|
|
224
285
|
end
|
|
225
286
|
|
|
226
287
|
# Wraps the Rack input (request body) into a Tus::Input object, applying a
|
|
227
|
-
# size limit if one exists.
|
|
288
|
+
# size limit if one exists. Rack 3 allows a request without a body to have
|
|
289
|
+
# no Rack input (e.g. over HTTP/2 on Falcon).
|
|
228
290
|
def get_input(info)
|
|
229
291
|
offset = info.offset
|
|
230
292
|
total = info.length || max_size
|
|
231
293
|
limit = total - offset if total
|
|
232
294
|
|
|
233
|
-
Tus::Input.new(request.body, limit: limit)
|
|
295
|
+
@upload_input = Tus::Input.new(request.body || StringIO.new, limit: limit)
|
|
296
|
+
@upload_input.interrupt! if @upload_interrupted
|
|
297
|
+
@upload_input
|
|
234
298
|
end
|
|
235
299
|
|
|
236
300
|
def validate_content_type!
|
|
@@ -250,8 +314,7 @@ module Tus
|
|
|
250
314
|
upload_length = request.headers["Upload-Length"]
|
|
251
315
|
|
|
252
316
|
error!(400, "Missing Upload-Length header") if upload_length.to_s == ""
|
|
253
|
-
error!(400, "Invalid Upload-Length header") if upload_length =~ /\D/
|
|
254
|
-
error!(400, "Invalid Upload-Length header") if upload_length.to_i < 0
|
|
317
|
+
error!(400, "Invalid Upload-Length header") if upload_length.to_s =~ /\D/
|
|
255
318
|
|
|
256
319
|
if max_size && upload_length.to_i > max_size
|
|
257
320
|
error!(413, "Upload-Length header too large")
|
|
@@ -263,7 +326,6 @@ module Tus
|
|
|
263
326
|
|
|
264
327
|
error!(400, "Missing Upload-Offset header") if upload_offset.to_s == ""
|
|
265
328
|
error!(400, "Invalid Upload-Offset header") if upload_offset =~ /\D/
|
|
266
|
-
error!(400, "Invalid Upload-Offset header") if upload_offset.to_i < 0
|
|
267
329
|
|
|
268
330
|
if upload_offset.to_i != info.offset
|
|
269
331
|
error!(409, "Upload-Offset header doesn't match current offset")
|
|
@@ -272,7 +334,6 @@ module Tus
|
|
|
272
334
|
|
|
273
335
|
def validate_content_length!(size, info)
|
|
274
336
|
if info.length
|
|
275
|
-
error!(403, "Cannot modify completed upload") if info.offset == info.length
|
|
276
337
|
error!(413, "Size of this chunk surpasses Upload-Length") if info.offset + size > info.length
|
|
277
338
|
elsif max_size
|
|
278
339
|
error!(413, "Size of this chunk surpasses Tus-Max-Size") if info.offset + size > max_size
|
|
@@ -286,14 +347,27 @@ module Tus
|
|
|
286
347
|
def validate_upload_metadata!
|
|
287
348
|
upload_metadata = request.headers["Upload-Metadata"]
|
|
288
349
|
|
|
289
|
-
upload_metadata.split(",").
|
|
350
|
+
keys = upload_metadata.split(",").map do |string|
|
|
290
351
|
key, value = string.split(" ", 2)
|
|
291
352
|
|
|
292
353
|
error!(400, "Invalid Upload-Metadata header") if key.nil?
|
|
293
|
-
error!(400, "Invalid Upload-Metadata header")
|
|
294
|
-
error!(400, "Invalid Upload-Metadata header") if key =~ /,| /
|
|
354
|
+
error!(400, "Invalid Upload-Metadata header") unless key.ascii_only?
|
|
295
355
|
|
|
296
356
|
error!(400, "Invalid Upload-Metadata header") if value =~ /[^a-zA-Z0-9+\/=]/
|
|
357
|
+
|
|
358
|
+
key
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
error!(400, "Duplicate key in Upload-Metadata header") if keys.uniq.size != keys.size
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def validate_upload_defer_length!
|
|
365
|
+
upload_defer_length = request.headers["Upload-Defer-Length"]
|
|
366
|
+
|
|
367
|
+
error!(400, "Invalid Upload-Defer-Length header") if upload_defer_length != "1"
|
|
368
|
+
|
|
369
|
+
if request.headers["Upload-Length"]
|
|
370
|
+
error!(400, "Upload-Length and Upload-Defer-Length headers can't both be set")
|
|
297
371
|
end
|
|
298
372
|
end
|
|
299
373
|
|
|
@@ -310,50 +384,34 @@ module Tus
|
|
|
310
384
|
end
|
|
311
385
|
end
|
|
312
386
|
|
|
313
|
-
# Validates that each partial upload exists and is marked as one
|
|
387
|
+
# Validates that each partial upload exists and is marked as one, and at the
|
|
388
|
+
# same time calculates the sum of part lengths.
|
|
314
389
|
def validate_partial_uploads!(part_uids)
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
thread_count ||= 10
|
|
323
|
-
|
|
324
|
-
threads = thread_count.times.map do
|
|
325
|
-
Thread.new do
|
|
326
|
-
begin
|
|
327
|
-
loop do
|
|
328
|
-
part_uid = input.pop or break
|
|
329
|
-
part_info = storage.read_info(part_uid)
|
|
330
|
-
results << Tus::Info.new(part_info)
|
|
331
|
-
end
|
|
332
|
-
nil
|
|
333
|
-
rescue => error
|
|
334
|
-
input.clear
|
|
335
|
-
error
|
|
336
|
-
end
|
|
390
|
+
length = 0
|
|
391
|
+
|
|
392
|
+
part_uids.each do |part_uid|
|
|
393
|
+
begin
|
|
394
|
+
part_info = storage.read_info(part_uid)
|
|
395
|
+
rescue Tus::NotFound
|
|
396
|
+
error!(400, "Partial upload not found")
|
|
337
397
|
end
|
|
338
|
-
end
|
|
339
398
|
|
|
340
|
-
|
|
399
|
+
part_info = Tus::Info.new(part_info)
|
|
341
400
|
|
|
342
|
-
|
|
343
|
-
error!(400, "One or more partial uploads were not found")
|
|
344
|
-
elsif errors.any?
|
|
345
|
-
fail errors.first
|
|
346
|
-
end
|
|
401
|
+
error!(400, "Upload is not partial") unless part_info.partial?
|
|
347
402
|
|
|
348
|
-
|
|
403
|
+
unless part_info.length == part_info.offset
|
|
404
|
+
error!(400, "Partial upload is not finished")
|
|
405
|
+
end
|
|
349
406
|
|
|
350
|
-
|
|
351
|
-
error!(400, "One or more uploads were not partial")
|
|
407
|
+
length += part_info.length
|
|
352
408
|
end
|
|
353
409
|
|
|
354
|
-
if max_size &&
|
|
355
|
-
error!(400, "The sum of partial upload lengths
|
|
410
|
+
if max_size && length > max_size
|
|
411
|
+
error!(400, "The sum of partial upload lengths exceeds Tus-Max-Size")
|
|
356
412
|
end
|
|
413
|
+
|
|
414
|
+
length
|
|
357
415
|
end
|
|
358
416
|
|
|
359
417
|
def validate_upload_checksum!(input)
|
|
@@ -369,11 +427,7 @@ module Tus
|
|
|
369
427
|
# Handles partial responses requested in the "Range" header. Implementation
|
|
370
428
|
# is mostly copied from Rack::File.
|
|
371
429
|
def handle_range_request!(length)
|
|
372
|
-
|
|
373
|
-
ranges = Rack::Utils.get_byte_ranges(request.headers["Range"], length)
|
|
374
|
-
else
|
|
375
|
-
ranges = Rack::Utils.byte_ranges(request.env, length)
|
|
376
|
-
end
|
|
430
|
+
ranges = Rack::Utils.get_byte_ranges(request.headers["Range"], length)
|
|
377
431
|
|
|
378
432
|
# we support ranged requests
|
|
379
433
|
response.headers["Accept-Ranges"] = "bytes"
|
|
@@ -392,9 +446,18 @@ module Tus
|
|
|
392
446
|
response.headers["Content-Range"] = "bytes #{range.begin}-#{range.end}/#{length}"
|
|
393
447
|
end
|
|
394
448
|
|
|
449
|
+
response.headers["Content-Length"] = range.size.to_s
|
|
450
|
+
|
|
395
451
|
range
|
|
396
452
|
end
|
|
397
453
|
|
|
454
|
+
def redirect_download
|
|
455
|
+
value = opts[:redirect_download]
|
|
456
|
+
value = storage.method(:file_url) if value == true
|
|
457
|
+
|
|
458
|
+
value
|
|
459
|
+
end
|
|
460
|
+
|
|
398
461
|
def handle_cors!
|
|
399
462
|
origin = request.headers["Origin"]
|
|
400
463
|
|
|
@@ -404,13 +467,25 @@ module Tus
|
|
|
404
467
|
|
|
405
468
|
if request.options?
|
|
406
469
|
response.headers["Access-Control-Allow-Methods"] = "POST, GET, HEAD, PATCH, DELETE, OPTIONS"
|
|
407
|
-
response.headers["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata"
|
|
470
|
+
response.headers["Access-Control-Allow-Headers"] = "Authorization, Origin, X-Requested-With, X-Request-ID, X-HTTP-Method-Override, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata, Upload-Defer-Length, Upload-Concat, Upload-Checksum"
|
|
408
471
|
response.headers["Access-Control-Max-Age"] = "86400"
|
|
409
472
|
else
|
|
410
|
-
response.headers["Access-Control-Expose-Headers"] = "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata"
|
|
473
|
+
response.headers["Access-Control-Expose-Headers"] = "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata, Upload-Defer-Length, Upload-Concat, Upload-Expires, Tus-Checksum-Algorithm"
|
|
411
474
|
end
|
|
412
475
|
end
|
|
413
476
|
|
|
477
|
+
# Responds with the protocol versions, extensions and limits of the server.
|
|
478
|
+
def server_info!
|
|
479
|
+
response.headers.update(
|
|
480
|
+
"Tus-Version" => SUPPORTED_VERSIONS.join(","),
|
|
481
|
+
"Tus-Extension" => SUPPORTED_EXTENSIONS.join(","),
|
|
482
|
+
"Tus-Max-Size" => max_size.to_s,
|
|
483
|
+
"Tus-Checksum-Algorithm" => SUPPORTED_CHECKSUM_ALGORITHMS.join(","),
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
no_content!
|
|
487
|
+
end
|
|
488
|
+
|
|
414
489
|
def no_content!
|
|
415
490
|
response.status = 204
|
|
416
491
|
request.halt
|
|
@@ -429,21 +504,16 @@ module Tus
|
|
|
429
504
|
request.halt
|
|
430
505
|
end
|
|
431
506
|
|
|
432
|
-
def
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
if opts[:download_url]
|
|
436
|
-
value ||= opts[:download_url]
|
|
437
|
-
warn "[TUS-RUBY-SERVER DEPRECATION] The :download_url option has been renamed to :redirect_download."
|
|
438
|
-
end
|
|
439
|
-
|
|
440
|
-
value = storage.method(:file_url) if value == true
|
|
507
|
+
def storage
|
|
508
|
+
opts[:storage] || Tus::Storage::Filesystem.new("data")
|
|
509
|
+
end
|
|
441
510
|
|
|
442
|
-
|
|
511
|
+
def locker
|
|
512
|
+
opts[:locker] || Tus::Locker.new
|
|
443
513
|
end
|
|
444
514
|
|
|
445
|
-
def
|
|
446
|
-
opts[:
|
|
515
|
+
def lock_timeout
|
|
516
|
+
opts[:lock_timeout]
|
|
447
517
|
end
|
|
448
518
|
|
|
449
519
|
def max_size
|
|
@@ -453,9 +523,5 @@ module Tus
|
|
|
453
523
|
def expiration_time
|
|
454
524
|
opts[:expiration_time]
|
|
455
525
|
end
|
|
456
|
-
|
|
457
|
-
def expiration_interval
|
|
458
|
-
opts[:expiration_interval]
|
|
459
|
-
end
|
|
460
526
|
end
|
|
461
527
|
end
|
|
@@ -54,15 +54,26 @@ module Tus
|
|
|
54
54
|
|
|
55
55
|
# Delete parts after concatenation.
|
|
56
56
|
delete(part_uids)
|
|
57
|
-
|
|
58
|
-
# Tus server requires us to return the size of the concatenated file.
|
|
59
|
-
file_path(uid).size
|
|
60
57
|
end
|
|
61
58
|
|
|
62
|
-
#
|
|
59
|
+
# Writes data to the specified upload in a streaming fashion, and
|
|
63
60
|
# returns the number of bytes it managed to save.
|
|
61
|
+
#
|
|
62
|
+
# Data is written at the upload's offset rather than appended, so a
|
|
63
|
+
# retried request that overlaps an earlier one writes the same bytes to
|
|
64
|
+
# the same place instead of duplicating them. Without an offset in the
|
|
65
|
+
# info, data is appended.
|
|
66
|
+
#
|
|
67
|
+
# Raises Tus::NotFound if the upload was deleted.
|
|
64
68
|
def patch_file(uid, input, info = {})
|
|
65
|
-
file_path(uid).open("
|
|
69
|
+
file_path(uid).open("r+b") do |file|
|
|
70
|
+
offset = info["Upload-Offset"] ? Integer(info["Upload-Offset"]) : file.size
|
|
71
|
+
file.seek(offset)
|
|
72
|
+
|
|
73
|
+
IO.copy_stream(input, file)
|
|
74
|
+
end
|
|
75
|
+
rescue Errno::ENOENT
|
|
76
|
+
raise Tus::NotFound
|
|
66
77
|
end
|
|
67
78
|
|
|
68
79
|
# Returns info of the specified upload. Raises Tus::NotFound if the upload
|
|
@@ -73,9 +84,15 @@ module Tus
|
|
|
73
84
|
JSON.parse(info_path(uid).binread)
|
|
74
85
|
end
|
|
75
86
|
|
|
76
|
-
# Updates info of the specified upload.
|
|
87
|
+
# Updates info of the specified upload. The info file is replaced
|
|
88
|
+
# atomically, so concurrent readers never see a partially written file.
|
|
77
89
|
def update_info(uid, info)
|
|
78
|
-
|
|
90
|
+
temp_path = directory.join("#{uid}.info.tmp")
|
|
91
|
+
temp_path.binwrite(JSON.generate(info))
|
|
92
|
+
temp_path.chmod(@permissions)
|
|
93
|
+
temp_path.rename(info_path(uid))
|
|
94
|
+
ensure
|
|
95
|
+
temp_path.unlink if temp_path && temp_path.exist?
|
|
79
96
|
end
|
|
80
97
|
|
|
81
98
|
# Returns a Tus::Response object through which data of the specified
|
|
@@ -110,7 +127,7 @@ module Tus
|
|
|
110
127
|
# Deletes data and info files of uploads older than the specified date.
|
|
111
128
|
def expire_files(expiration_date)
|
|
112
129
|
uids = directory.children
|
|
113
|
-
.select { |pathname| pathname.mtime <= expiration_date }
|
|
130
|
+
.select { |pathname| pathname.file? && pathname.mtime <= expiration_date }
|
|
114
131
|
.map { |pathname| pathname.basename(".*").to_s }
|
|
115
132
|
|
|
116
133
|
delete(uids)
|