tus-server 2.3.0 → 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 +64 -0
- data/README.md +132 -313
- data/lib/tus/checksum.rb +4 -28
- data/lib/tus/errors.rb +2 -0
- data/lib/tus/info.rb +17 -16
- 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 +196 -109
- data/lib/tus/storage/filesystem.rb +25 -5
- data/lib/tus/storage/s3.rb +62 -87
- data/tus-server.gemspec +5 -6
- metadata +12 -31
- data/lib/tus/input/unicorn.rb +0 -16
- data/lib/tus/storage/gridfs.rb +0 -275
data/lib/tus/storage/s3.rb
CHANGED
|
@@ -21,25 +21,17 @@ module Tus
|
|
|
21
21
|
MAX_MULTIPART_PARTS = 10_000
|
|
22
22
|
MAX_OBJECT_SIZE = 5 * 1024 * 1024 * 1024 * 1024
|
|
23
23
|
|
|
24
|
-
attr_reader :
|
|
24
|
+
attr_reader :bucket, :prefix, :upload_options, :limits, :disposition
|
|
25
25
|
|
|
26
26
|
# Initializes an aws-sdk-s3 client with the given credentials.
|
|
27
|
-
def initialize(bucket:, prefix: nil, upload_options: {}, limits: {},
|
|
27
|
+
def initialize(bucket:, prefix: nil, upload_options: {}, limits: {}, disposition: "inline", **client_options)
|
|
28
28
|
fail ArgumentError, "the :bucket option was nil" unless bucket
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
warn "[Tus-Ruby-Server] :thread_count is deprecated and will be removed in the next major version, use :concurrency instead, e.g `concurrency: { concatenation: 20 }`"
|
|
32
|
-
concurrency[:concatenation] = thread_count
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
resource = Aws::S3::Resource.new(**client_options)
|
|
36
|
-
|
|
37
|
-
@client = resource.client
|
|
38
|
-
@bucket = resource.bucket(bucket)
|
|
30
|
+
@bucket = Aws::S3::Bucket.new(name: bucket, **client_options)
|
|
39
31
|
@prefix = prefix
|
|
40
32
|
@upload_options = upload_options
|
|
41
33
|
@limits = limits
|
|
42
|
-
@
|
|
34
|
+
@disposition = disposition
|
|
43
35
|
end
|
|
44
36
|
|
|
45
37
|
# Initiates multipart upload for the given upload, and stores its
|
|
@@ -51,12 +43,7 @@ module Tus
|
|
|
51
43
|
fail Tus::Error, "upload length exceeds maximum S3 object size"
|
|
52
44
|
end
|
|
53
45
|
|
|
54
|
-
|
|
55
|
-
options[:content_type] = tus_info.type if tus_info.type
|
|
56
|
-
options[:content_disposition] = ContentDisposition.inline(tus_info.name) if tus_info.name
|
|
57
|
-
options.merge!(upload_options)
|
|
58
|
-
|
|
59
|
-
multipart_upload = object(uid).initiate_multipart_upload(options)
|
|
46
|
+
multipart_upload = object(uid).initiate_multipart_upload(object_options(tus_info))
|
|
60
47
|
|
|
61
48
|
info["multipart_id"] = multipart_upload.id
|
|
62
49
|
info["multipart_parts"] = []
|
|
@@ -84,22 +71,25 @@ module Tus
|
|
|
84
71
|
finalize_file(uid, info)
|
|
85
72
|
|
|
86
73
|
delete(part_uids.flat_map { |part_uid| [object(part_uid), object("#{part_uid}.info")] })
|
|
87
|
-
rescue
|
|
88
|
-
multipart_upload
|
|
89
|
-
raise
|
|
74
|
+
rescue
|
|
75
|
+
multipart_upload&.abort
|
|
76
|
+
raise
|
|
90
77
|
end
|
|
91
78
|
|
|
92
79
|
# Appends data to the specified upload in a streaming fashion, and returns
|
|
93
80
|
# the number of bytes it managed to save.
|
|
94
81
|
#
|
|
95
|
-
#
|
|
96
|
-
#
|
|
97
|
-
#
|
|
98
|
-
#
|
|
99
|
-
#
|
|
82
|
+
# Input data is read into memory one multipart part at a time (at least
|
|
83
|
+
# 5MB, S3's minimum part size). Before a part is uploaded, the next part
|
|
84
|
+
# is read as well, so that a short final chunk can be merged into the
|
|
85
|
+
# previous part instead of being uploaded below the minimum size. Parts
|
|
86
|
+
# are uploaded synchronously, so input isn't read during an upload.
|
|
100
87
|
#
|
|
101
|
-
#
|
|
102
|
-
#
|
|
88
|
+
# A trailing chunk smaller than the part size is discarded unless it
|
|
89
|
+
# completes the upload, because S3 wouldn't accept it as a part.
|
|
90
|
+
#
|
|
91
|
+
# If a network error is raised during the upload to S3, the upload of
|
|
92
|
+
# further input data stops and the number of bytes that managed to get
|
|
103
93
|
# uploaded is returned.
|
|
104
94
|
def patch_file(uid, input, info = {})
|
|
105
95
|
tus_info = Tus::Info.new(info)
|
|
@@ -145,7 +135,9 @@ module Tus
|
|
|
145
135
|
end
|
|
146
136
|
|
|
147
137
|
# Completes the multipart upload using the part information saved in the
|
|
148
|
-
# info hash.
|
|
138
|
+
# info hash. An empty upload has no parts, which S3 doesn't accept for
|
|
139
|
+
# completing a multipart upload, so we abort it and create an empty object
|
|
140
|
+
# instead.
|
|
149
141
|
def finalize_file(uid, info = {})
|
|
150
142
|
upload_id = info["multipart_id"]
|
|
151
143
|
parts = info["multipart_parts"].map do |part|
|
|
@@ -153,7 +145,13 @@ module Tus
|
|
|
153
145
|
end
|
|
154
146
|
|
|
155
147
|
multipart_upload = object(uid).multipart_upload(upload_id)
|
|
156
|
-
|
|
148
|
+
|
|
149
|
+
if parts.empty?
|
|
150
|
+
multipart_upload.abort
|
|
151
|
+
object(uid).put(body: "", **object_options(Tus::Info.new(info)))
|
|
152
|
+
else
|
|
153
|
+
multipart_upload.complete(multipart_upload: { parts: parts })
|
|
154
|
+
end
|
|
157
155
|
|
|
158
156
|
info.delete("multipart_id")
|
|
159
157
|
info.delete("multipart_parts")
|
|
@@ -177,8 +175,12 @@ module Tus
|
|
|
177
175
|
# upload can be retrieved in a streaming fashion. Accepts an optional
|
|
178
176
|
# range parameter for selecting a subset of bytes to retrieve.
|
|
179
177
|
def get_file(uid, info = {}, range: nil)
|
|
180
|
-
|
|
181
|
-
|
|
178
|
+
# an empty range (of an empty upload) cannot be expressed in the header
|
|
179
|
+
range = (range && range.size > 0) ? "bytes=#{range.begin}-#{range.end}" : nil
|
|
180
|
+
chunks = Enumerator.new do |yielder|
|
|
181
|
+
# newer aws-sdk-s3 versions also yield response headers
|
|
182
|
+
object(uid).get(range: range) { |chunk| yielder << chunk }
|
|
183
|
+
end
|
|
182
184
|
|
|
183
185
|
Tus::Response.new(chunks: chunks)
|
|
184
186
|
end
|
|
@@ -215,15 +217,24 @@ module Tus
|
|
|
215
217
|
bucket.multipart_uploads
|
|
216
218
|
.select { |multipart_upload| multipart_upload.key.start_with?(prefix.to_s) }
|
|
217
219
|
.select { |multipart_upload| multipart_upload.initiated <= expiration_date }
|
|
218
|
-
.select { |multipart_upload|
|
|
219
|
-
last_modified = multipart_upload.parts.map(&:last_modified).max
|
|
220
|
-
last_modified.nil? || last_modified <= expiration_date
|
|
221
|
-
}
|
|
220
|
+
.select { |multipart_upload| multipart_upload.parts.all?{|p| p.last_modified <= expiration_date} }
|
|
222
221
|
.each(&:abort)
|
|
223
222
|
end
|
|
224
223
|
|
|
224
|
+
def client
|
|
225
|
+
bucket.client
|
|
226
|
+
end
|
|
227
|
+
|
|
225
228
|
private
|
|
226
229
|
|
|
230
|
+
# Options for the S3 object, from the upload metadata and :upload_options.
|
|
231
|
+
def object_options(tus_info)
|
|
232
|
+
options = {}
|
|
233
|
+
options[:content_type] = tus_info.type if tus_info.type
|
|
234
|
+
options[:content_disposition] = ContentDisposition.(disposition: disposition, filename: tus_info.name) if tus_info.name || disposition != "inline"
|
|
235
|
+
options.merge(upload_options)
|
|
236
|
+
end
|
|
237
|
+
|
|
227
238
|
# Uploads given body as a new multipart part with the specified part
|
|
228
239
|
# number to the specified multipart upload. Returns part number and ETag
|
|
229
240
|
# that will be required later for completing the multipart upload.
|
|
@@ -240,7 +251,7 @@ module Tus
|
|
|
240
251
|
# file, taking into account AWS S3 multipart limits on part size and
|
|
241
252
|
# number of parts.
|
|
242
253
|
def calculate_part_size(length)
|
|
243
|
-
return min_part_size if length.nil?
|
|
254
|
+
return min_part_size if length.nil? || length == 0 # reading 0 bytes never reaches the end
|
|
244
255
|
return length if length <= min_part_size
|
|
245
256
|
return min_part_size if length <= min_part_size * max_multipart_parts
|
|
246
257
|
|
|
@@ -262,64 +273,28 @@ module Tus
|
|
|
262
273
|
end
|
|
263
274
|
|
|
264
275
|
# Creates multipart parts for the specified multipart upload by copying
|
|
265
|
-
# given objects into them.
|
|
266
|
-
# which consumes that queue.
|
|
276
|
+
# given objects into them.
|
|
267
277
|
def copy_parts(objects, multipart_upload)
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
results = Queue.new
|
|
271
|
-
|
|
272
|
-
parts.each { |part| input << part }
|
|
273
|
-
input.close
|
|
274
|
-
|
|
275
|
-
thread_count = concurrency[:concatenation] || 10
|
|
276
|
-
threads = thread_count.times.map { copy_part_thread(input, results) }
|
|
277
|
-
|
|
278
|
-
errors = threads.map(&:value).compact
|
|
279
|
-
fail errors.first if errors.any?
|
|
280
|
-
|
|
281
|
-
part_results = Array.new(results.size) { results.pop } # convert Queue into an Array
|
|
282
|
-
part_results.sort_by { |part| part.fetch("part_number") }
|
|
283
|
-
end
|
|
284
|
-
|
|
285
|
-
# Computes data required for copying objects into new multipart parts.
|
|
286
|
-
def compute_parts(objects, multipart_upload)
|
|
287
|
-
objects.map.with_index do |object, idx|
|
|
288
|
-
{
|
|
289
|
-
bucket: multipart_upload.bucket_name,
|
|
290
|
-
key: multipart_upload.object_key,
|
|
291
|
-
upload_id: multipart_upload.id,
|
|
292
|
-
copy_source: [object.bucket_name, object.key].join("/"),
|
|
293
|
-
part_number: idx + 1,
|
|
294
|
-
}
|
|
278
|
+
threads = objects.map.with_index do |object, idx|
|
|
279
|
+
Thread.new { copy_part(object, idx + 1, multipart_upload) }
|
|
295
280
|
end
|
|
296
|
-
end
|
|
297
281
|
|
|
298
|
-
|
|
299
|
-
# copy requests.
|
|
300
|
-
def copy_part_thread(input, results)
|
|
301
|
-
Thread.new do
|
|
302
|
-
begin
|
|
303
|
-
loop do
|
|
304
|
-
part = input.pop or break
|
|
305
|
-
part_result = copy_part(part)
|
|
306
|
-
results << part_result
|
|
307
|
-
end
|
|
308
|
-
nil
|
|
309
|
-
rescue => error
|
|
310
|
-
input.clear # clear other work
|
|
311
|
-
error
|
|
312
|
-
end
|
|
313
|
-
end
|
|
282
|
+
threads.map(&:value)
|
|
314
283
|
end
|
|
315
284
|
|
|
316
285
|
# Creates a new multipart part by copying the object specified in the
|
|
317
286
|
# given data. Returns part number and ETag that will be required later
|
|
318
287
|
# for completing the multipart upload.
|
|
319
|
-
def copy_part(
|
|
320
|
-
response = client.upload_part_copy(
|
|
321
|
-
|
|
322
|
-
|
|
288
|
+
def copy_part(object, part_number, multipart_upload)
|
|
289
|
+
response = client.upload_part_copy(
|
|
290
|
+
bucket: multipart_upload.bucket_name,
|
|
291
|
+
key: multipart_upload.object_key,
|
|
292
|
+
upload_id: multipart_upload.id,
|
|
293
|
+
copy_source: [object.bucket_name, object.key].join("/"),
|
|
294
|
+
part_number: part_number,
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
{ "part_number" => part_number, "etag" => response.copy_part_result.etag }
|
|
323
298
|
end
|
|
324
299
|
|
|
325
300
|
# Retuns an Aws::S3::Object with the prefix applied.
|
data/tus-server.gemspec
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Gem::Specification.new do |gem|
|
|
2
2
|
gem.name = "tus-server"
|
|
3
|
-
gem.version = "
|
|
3
|
+
gem.version = "3.0.0"
|
|
4
4
|
|
|
5
|
-
gem.required_ruby_version = ">= 2
|
|
5
|
+
gem.required_ruby_version = ">= 3.2"
|
|
6
6
|
|
|
7
7
|
gem.summary = "Ruby server implementation of tus.io, the open protocol for resumable file uploads."
|
|
8
8
|
|
|
@@ -17,10 +17,9 @@ Gem::Specification.new do |gem|
|
|
|
17
17
|
gem.add_dependency "roda", ">= 2.27", "< 4"
|
|
18
18
|
gem.add_dependency "content_disposition", "~> 1.0"
|
|
19
19
|
|
|
20
|
-
gem.add_development_dependency "rake", "
|
|
20
|
+
gem.add_development_dependency "rake", ">= 12"
|
|
21
21
|
gem.add_development_dependency "minitest", "~> 5.8"
|
|
22
|
-
gem.add_development_dependency "rack-
|
|
23
|
-
gem.add_development_dependency "cucumber", "
|
|
22
|
+
gem.add_development_dependency "rack-test"
|
|
23
|
+
gem.add_development_dependency "cucumber", ">= 3.1"
|
|
24
24
|
gem.add_development_dependency "aws-sdk-s3", "~> 1.2"
|
|
25
|
-
gem.add_development_dependency "aws-sdk-core", "~> 3.23"
|
|
26
25
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tus-server
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Janko Marohnić
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: roda
|
|
@@ -48,16 +47,16 @@ dependencies:
|
|
|
48
47
|
name: rake
|
|
49
48
|
requirement: !ruby/object:Gem::Requirement
|
|
50
49
|
requirements:
|
|
51
|
-
- - "
|
|
50
|
+
- - ">="
|
|
52
51
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: '
|
|
52
|
+
version: '12'
|
|
54
53
|
type: :development
|
|
55
54
|
prerelease: false
|
|
56
55
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
56
|
requirements:
|
|
58
|
-
- - "
|
|
57
|
+
- - ">="
|
|
59
58
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: '
|
|
59
|
+
version: '12'
|
|
61
60
|
- !ruby/object:Gem::Dependency
|
|
62
61
|
name: minitest
|
|
63
62
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -73,7 +72,7 @@ dependencies:
|
|
|
73
72
|
- !ruby/object:Gem::Version
|
|
74
73
|
version: '5.8'
|
|
75
74
|
- !ruby/object:Gem::Dependency
|
|
76
|
-
name: rack-
|
|
75
|
+
name: rack-test
|
|
77
76
|
requirement: !ruby/object:Gem::Requirement
|
|
78
77
|
requirements:
|
|
79
78
|
- - ">="
|
|
@@ -90,14 +89,14 @@ dependencies:
|
|
|
90
89
|
name: cucumber
|
|
91
90
|
requirement: !ruby/object:Gem::Requirement
|
|
92
91
|
requirements:
|
|
93
|
-
- - "
|
|
92
|
+
- - ">="
|
|
94
93
|
- !ruby/object:Gem::Version
|
|
95
94
|
version: '3.1'
|
|
96
95
|
type: :development
|
|
97
96
|
prerelease: false
|
|
98
97
|
version_requirements: !ruby/object:Gem::Requirement
|
|
99
98
|
requirements:
|
|
100
|
-
- - "
|
|
99
|
+
- - ">="
|
|
101
100
|
- !ruby/object:Gem::Version
|
|
102
101
|
version: '3.1'
|
|
103
102
|
- !ruby/object:Gem::Dependency
|
|
@@ -114,21 +113,6 @@ dependencies:
|
|
|
114
113
|
- - "~>"
|
|
115
114
|
- !ruby/object:Gem::Version
|
|
116
115
|
version: '1.2'
|
|
117
|
-
- !ruby/object:Gem::Dependency
|
|
118
|
-
name: aws-sdk-core
|
|
119
|
-
requirement: !ruby/object:Gem::Requirement
|
|
120
|
-
requirements:
|
|
121
|
-
- - "~>"
|
|
122
|
-
- !ruby/object:Gem::Version
|
|
123
|
-
version: '3.23'
|
|
124
|
-
type: :development
|
|
125
|
-
prerelease: false
|
|
126
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
127
|
-
requirements:
|
|
128
|
-
- - "~>"
|
|
129
|
-
- !ruby/object:Gem::Version
|
|
130
|
-
version: '3.23'
|
|
131
|
-
description:
|
|
132
116
|
email:
|
|
133
117
|
- janko.marohnic@gmail.com
|
|
134
118
|
executables: []
|
|
@@ -143,18 +127,16 @@ files:
|
|
|
143
127
|
- lib/tus/errors.rb
|
|
144
128
|
- lib/tus/info.rb
|
|
145
129
|
- lib/tus/input.rb
|
|
146
|
-
- lib/tus/
|
|
130
|
+
- lib/tus/locker.rb
|
|
147
131
|
- lib/tus/response.rb
|
|
148
132
|
- lib/tus/server.rb
|
|
149
133
|
- lib/tus/storage/filesystem.rb
|
|
150
|
-
- lib/tus/storage/gridfs.rb
|
|
151
134
|
- lib/tus/storage/s3.rb
|
|
152
135
|
- tus-server.gemspec
|
|
153
136
|
homepage: https://github.com/janko/tus-ruby-server
|
|
154
137
|
licenses:
|
|
155
138
|
- MIT
|
|
156
139
|
metadata: {}
|
|
157
|
-
post_install_message:
|
|
158
140
|
rdoc_options: []
|
|
159
141
|
require_paths:
|
|
160
142
|
- lib
|
|
@@ -162,15 +144,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
162
144
|
requirements:
|
|
163
145
|
- - ">="
|
|
164
146
|
- !ruby/object:Gem::Version
|
|
165
|
-
version: '2
|
|
147
|
+
version: '3.2'
|
|
166
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
149
|
requirements:
|
|
168
150
|
- - ">="
|
|
169
151
|
- !ruby/object:Gem::Version
|
|
170
152
|
version: '0'
|
|
171
153
|
requirements: []
|
|
172
|
-
rubygems_version:
|
|
173
|
-
signing_key:
|
|
154
|
+
rubygems_version: 4.0.20
|
|
174
155
|
specification_version: 4
|
|
175
156
|
summary: Ruby server implementation of tus.io, the open protocol for resumable file
|
|
176
157
|
uploads.
|
data/lib/tus/input/unicorn.rb
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
module Tus
|
|
2
|
-
class Input
|
|
3
|
-
# Extension for Unicorn to gracefully handle interrupted uploads.
|
|
4
|
-
module Unicorn
|
|
5
|
-
# Rescues Unicorn::ClientShutdown exception when reading, and instead of
|
|
6
|
-
# failing just returns blank data to signal end of input.
|
|
7
|
-
def read(length = nil, outbuf = nil)
|
|
8
|
-
super
|
|
9
|
-
rescue => exception
|
|
10
|
-
raise unless exception.class.name == "Unicorn::ClientShutdown"
|
|
11
|
-
outbuf = outbuf.to_s.clear
|
|
12
|
-
outbuf unless length
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
data/lib/tus/storage/gridfs.rb
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
# frozen-string-literal: true
|
|
2
|
-
|
|
3
|
-
gem "mongo", "~> 2.3"
|
|
4
|
-
|
|
5
|
-
require "mongo"
|
|
6
|
-
|
|
7
|
-
require "tus/info"
|
|
8
|
-
require "tus/response"
|
|
9
|
-
require "tus/errors"
|
|
10
|
-
|
|
11
|
-
require "digest"
|
|
12
|
-
|
|
13
|
-
module Tus
|
|
14
|
-
module Storage
|
|
15
|
-
class Gridfs
|
|
16
|
-
BATCH_SIZE = 5 * 1024 * 1024
|
|
17
|
-
|
|
18
|
-
attr_reader :client, :prefix, :bucket, :chunk_size
|
|
19
|
-
|
|
20
|
-
# Initializes the GridFS storage and creates necessary indexes.
|
|
21
|
-
def initialize(client:, prefix: "fs", chunk_size: 256*1024)
|
|
22
|
-
@client = client
|
|
23
|
-
@prefix = prefix
|
|
24
|
-
@chunk_size = chunk_size
|
|
25
|
-
@bucket = client.database.fs(bucket_name: prefix)
|
|
26
|
-
|
|
27
|
-
@bucket.send(:ensure_indexes!)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Creates a file for the specified upload.
|
|
31
|
-
def create_file(uid, info = {})
|
|
32
|
-
content_type = Tus::Info.new(info).metadata["content_type"]
|
|
33
|
-
|
|
34
|
-
create_grid_file(
|
|
35
|
-
filename: uid,
|
|
36
|
-
content_type: content_type,
|
|
37
|
-
)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Concatenates multiple partial uploads into a single upload, and returns
|
|
41
|
-
# the size of the resulting upload. The partial uploads are deleted after
|
|
42
|
-
# concatenation.
|
|
43
|
-
#
|
|
44
|
-
# It concatenates by updating partial upload's GridFS chunks to point to
|
|
45
|
-
# the new upload.
|
|
46
|
-
#
|
|
47
|
-
# Raises Tus::Error if GridFS chunks of partial uploads don't exist or
|
|
48
|
-
# aren't completely filled.
|
|
49
|
-
def concatenate(uid, part_uids, info = {})
|
|
50
|
-
grid_infos = files_collection.find(filename: {"$in" => part_uids}).to_a
|
|
51
|
-
grid_infos.sort_by! { |grid_info| part_uids.index(grid_info[:filename]) }
|
|
52
|
-
|
|
53
|
-
validate_parts!(grid_infos, part_uids)
|
|
54
|
-
|
|
55
|
-
length = grid_infos.map { |doc| doc[:length] }.reduce(0, :+)
|
|
56
|
-
content_type = Tus::Info.new(info).type
|
|
57
|
-
|
|
58
|
-
grid_file = create_grid_file(
|
|
59
|
-
filename: uid,
|
|
60
|
-
length: length,
|
|
61
|
-
content_type: content_type,
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
# Update the chunks belonging to parts so that they point to the new file.
|
|
65
|
-
grid_infos.inject(0) do |offset, grid_info|
|
|
66
|
-
result = chunks_collection
|
|
67
|
-
.find(files_id: grid_info[:_id])
|
|
68
|
-
.update_many(
|
|
69
|
-
"$set" => { files_id: grid_file.id },
|
|
70
|
-
"$inc" => { n: offset },
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
offset += result.modified_count
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
# Delete the parts after concatenation.
|
|
77
|
-
files_collection.delete_many(filename: {"$in" => part_uids})
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
# Appends data to the specified upload in a streaming fashion, and
|
|
81
|
-
# returns the number of bytes it managed to save.
|
|
82
|
-
#
|
|
83
|
-
# It does so by reading the input data in batches of chunks, creating a
|
|
84
|
-
# new GridFS chunk for each chunk of data and appending it to the
|
|
85
|
-
# existing list.
|
|
86
|
-
def patch_file(uid, input, info = {})
|
|
87
|
-
grid_info = files_collection.find(filename: uid).first
|
|
88
|
-
current_length = grid_info[:length]
|
|
89
|
-
chunk_size = grid_info[:chunkSize]
|
|
90
|
-
bytes_saved = 0
|
|
91
|
-
|
|
92
|
-
# It's possible that the previous data append didn't fill in the last
|
|
93
|
-
# GridFS chunk completely, so we fill in that gap now before creating
|
|
94
|
-
# new GridFS chunks.
|
|
95
|
-
bytes_saved += patch_last_chunk(input, grid_info) if current_length % chunk_size != 0
|
|
96
|
-
|
|
97
|
-
# Create an Enumerator which yields chunks of input data which have the
|
|
98
|
-
# size of the configured :chunkSize of the GridFS file.
|
|
99
|
-
chunks_enumerator = Enumerator.new do |yielder|
|
|
100
|
-
while (data = input.read(chunk_size))
|
|
101
|
-
yielder << data
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
chunks_in_batch = (BATCH_SIZE.to_f / chunk_size).ceil
|
|
106
|
-
chunks_offset = chunks_collection.count(files_id: grid_info[:_id]) - 1
|
|
107
|
-
|
|
108
|
-
# Iterate in batches of data chunks and bulk-insert new GridFS chunks.
|
|
109
|
-
# This way we try to have a balance between bulking inserts and keeping
|
|
110
|
-
# memory usage low.
|
|
111
|
-
chunks_enumerator.each_slice(chunks_in_batch) do |chunks|
|
|
112
|
-
grid_chunks = chunks.map do |data|
|
|
113
|
-
Mongo::Grid::File::Chunk.new(
|
|
114
|
-
data: BSON::Binary.new(data),
|
|
115
|
-
files_id: grid_info[:_id],
|
|
116
|
-
n: chunks_offset += 1,
|
|
117
|
-
)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
chunks_collection.insert_many(grid_chunks)
|
|
121
|
-
|
|
122
|
-
# Update the total length and refresh the upload date on each update,
|
|
123
|
-
# which are used in #get_file, #concatenate and #expire_files.
|
|
124
|
-
files_collection.find(filename: uid).update_one(
|
|
125
|
-
"$inc" => { length: chunks.map(&:bytesize).inject(0, :+) },
|
|
126
|
-
"$set" => { uploadDate: Time.now.utc },
|
|
127
|
-
)
|
|
128
|
-
bytes_saved += chunks.map(&:bytesize).inject(0, :+)
|
|
129
|
-
|
|
130
|
-
chunks.each(&:clear) # deallocate strings
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
bytes_saved
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
# Returns info of the specified upload. Raises Tus::NotFound if the upload
|
|
137
|
-
# wasn't found.
|
|
138
|
-
def read_info(uid)
|
|
139
|
-
grid_info = files_collection.find(filename: uid).first or raise Tus::NotFound
|
|
140
|
-
grid_info[:metadata]
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
# Updates info of the specified upload.
|
|
144
|
-
def update_info(uid, info)
|
|
145
|
-
files_collection.update_one({filename: uid}, {"$set" => {metadata: info}})
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
# Returns a Tus::Response object through which data of the specified
|
|
149
|
-
# upload can be retrieved in a streaming fashion. Accepts an optional
|
|
150
|
-
# range parameter for selecting a subset of bytes we want to retrieve.
|
|
151
|
-
def get_file(uid, info = {}, range: nil)
|
|
152
|
-
grid_info = files_collection.find(filename: uid).first
|
|
153
|
-
|
|
154
|
-
filter = { files_id: grid_info[:_id] }
|
|
155
|
-
|
|
156
|
-
if range
|
|
157
|
-
chunk_start = range.begin / grid_info[:chunkSize]
|
|
158
|
-
chunk_stop = range.end / grid_info[:chunkSize]
|
|
159
|
-
|
|
160
|
-
filter[:n] = {"$gte" => chunk_start, "$lte" => chunk_stop}
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
# Query only the subset of chunks specified by the range query. We
|
|
164
|
-
# cannot use Mongo::FsBucket#open_download_stream here because it
|
|
165
|
-
# doesn't support changing the filter.
|
|
166
|
-
chunks_view = chunks_collection.find(filter).sort(n: 1)
|
|
167
|
-
|
|
168
|
-
# Create an Enumerator which will yield chunks of the requested file
|
|
169
|
-
# content, allowing tus server to efficiently stream requested content
|
|
170
|
-
# to the client.
|
|
171
|
-
chunks = Enumerator.new do |yielder|
|
|
172
|
-
chunks_view.each do |document|
|
|
173
|
-
data = document[:data].data
|
|
174
|
-
|
|
175
|
-
if document[:n] == chunk_start && document[:n] == chunk_stop
|
|
176
|
-
byte_start = range.begin % grid_info[:chunkSize]
|
|
177
|
-
byte_stop = range.end % grid_info[:chunkSize]
|
|
178
|
-
elsif document[:n] == chunk_start
|
|
179
|
-
byte_start = range.begin % grid_info[:chunkSize]
|
|
180
|
-
byte_stop = grid_info[:chunkSize] - 1
|
|
181
|
-
elsif document[:n] == chunk_stop
|
|
182
|
-
byte_start = 0
|
|
183
|
-
byte_stop = range.end % grid_info[:chunkSize]
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
# If we're on the first or last chunk, return a subset of the chunk
|
|
187
|
-
# specified by the given range, otherwise return the full chunk.
|
|
188
|
-
if byte_start && byte_stop
|
|
189
|
-
yielder << data[byte_start..byte_stop]
|
|
190
|
-
else
|
|
191
|
-
yielder << data
|
|
192
|
-
end
|
|
193
|
-
end
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
Tus::Response.new(chunks: chunks, close: chunks_view.method(:close_query))
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
# Deletes the GridFS file and chunks for the specified upload.
|
|
200
|
-
def delete_file(uid, info = {})
|
|
201
|
-
grid_info = files_collection.find(filename: uid).first
|
|
202
|
-
bucket.delete(grid_info[:_id]) if grid_info
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
# Deletes GridFS file and chunks of uploads older than the specified date.
|
|
206
|
-
def expire_files(expiration_date)
|
|
207
|
-
grid_infos = files_collection.find(uploadDate: {"$lte" => expiration_date}).to_a
|
|
208
|
-
grid_info_ids = grid_infos.map { |info| info[:_id] }
|
|
209
|
-
|
|
210
|
-
files_collection.delete_many(_id: {"$in" => grid_info_ids})
|
|
211
|
-
chunks_collection.delete_many(files_id: {"$in" => grid_info_ids})
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
private
|
|
215
|
-
|
|
216
|
-
# Creates a GridFS file.
|
|
217
|
-
def create_grid_file(**options)
|
|
218
|
-
grid_file = Mongo::Grid::File.new("", metadata: {}, chunk_size: chunk_size, **options)
|
|
219
|
-
|
|
220
|
-
bucket.insert_one(grid_file)
|
|
221
|
-
|
|
222
|
-
grid_file
|
|
223
|
-
end
|
|
224
|
-
|
|
225
|
-
# If the last GridFS chunk of the file is incomplete (meaning it's smaller
|
|
226
|
-
# than the configured :chunkSize of the GridFS file), fills the missing
|
|
227
|
-
# data by reading a chunk of the input data.
|
|
228
|
-
def patch_last_chunk(input, grid_info)
|
|
229
|
-
last_chunk = chunks_collection.find(files_id: grid_info[:_id]).sort(n: -1).limit(1).first
|
|
230
|
-
data = last_chunk[:data].data
|
|
231
|
-
patch = input.read(grid_info[:chunkSize] - data.bytesize)
|
|
232
|
-
data << patch
|
|
233
|
-
|
|
234
|
-
chunks_collection.find(files_id: grid_info[:_id], n: last_chunk[:n])
|
|
235
|
-
.update_one("$set" => { data: BSON::Binary.new(data) })
|
|
236
|
-
|
|
237
|
-
files_collection.find(_id: grid_info[:_id])
|
|
238
|
-
.update_one("$inc" => { length: patch.bytesize })
|
|
239
|
-
|
|
240
|
-
patch.bytesize
|
|
241
|
-
end
|
|
242
|
-
|
|
243
|
-
# Validates that GridFS files of partial uploads are suitable for
|
|
244
|
-
# concatentation.
|
|
245
|
-
def validate_parts!(grid_infos, part_uids)
|
|
246
|
-
validate_parts_presence!(grid_infos, part_uids)
|
|
247
|
-
validate_parts_full_chunks!(grid_infos)
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
# Validates that each partial upload has a corresponding GridFS file.
|
|
251
|
-
def validate_parts_presence!(grid_infos, part_uids)
|
|
252
|
-
if grid_infos.count != part_uids.count
|
|
253
|
-
raise Tus::Error, "some parts for concatenation are missing"
|
|
254
|
-
end
|
|
255
|
-
end
|
|
256
|
-
|
|
257
|
-
# Validates that GridFS chunks of each file are filled completely.
|
|
258
|
-
def validate_parts_full_chunks!(grid_infos)
|
|
259
|
-
grid_infos.each do |grid_info|
|
|
260
|
-
if grid_info[:length] % grid_info[:chunkSize] != 0 && grid_info != grid_infos.last
|
|
261
|
-
raise Tus::Error, "cannot concatenate parts which aren't evenly distributed across chunks"
|
|
262
|
-
end
|
|
263
|
-
end
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
def files_collection
|
|
267
|
-
bucket.files_collection
|
|
268
|
-
end
|
|
269
|
-
|
|
270
|
-
def chunks_collection
|
|
271
|
-
bucket.chunks_collection
|
|
272
|
-
end
|
|
273
|
-
end
|
|
274
|
-
end
|
|
275
|
-
end
|