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.
@@ -3,6 +3,7 @@
3
3
  gem "aws-sdk-s3", "~> 1.2"
4
4
 
5
5
  require "aws-sdk-s3"
6
+ require "content_disposition"
6
7
 
7
8
  require "tus/info"
8
9
  require "tus/response"
@@ -14,26 +15,23 @@ require "cgi"
14
15
  module Tus
15
16
  module Storage
16
17
  class S3
17
- MIN_PART_SIZE = 5 * 1024 * 1024 # 5MB is the minimum part size for S3 multipart uploads
18
+ # AWS S3 multipart upload limits
19
+ MIN_PART_SIZE = 5 * 1024 * 1024
20
+ MAX_PART_SIZE = 5 * 1024 * 1024 * 1024
21
+ MAX_MULTIPART_PARTS = 10_000
22
+ MAX_OBJECT_SIZE = 5 * 1024 * 1024 * 1024 * 1024
18
23
 
19
- attr_reader :client, :bucket, :prefix, :upload_options, :concurrency
24
+ attr_reader :bucket, :prefix, :upload_options, :limits, :disposition
20
25
 
21
26
  # Initializes an aws-sdk-s3 client with the given credentials.
22
- def initialize(bucket:, prefix: nil, upload_options: {}, concurrency: {}, thread_count: nil, **client_options)
27
+ def initialize(bucket:, prefix: nil, upload_options: {}, limits: {}, disposition: "inline", **client_options)
23
28
  fail ArgumentError, "the :bucket option was nil" unless bucket
24
29
 
25
- if thread_count
26
- 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 }`"
27
- concurrency[:concatenation] = thread_count
28
- end
29
-
30
- resource = Aws::S3::Resource.new(**client_options)
31
-
32
- @client = resource.client
33
- @bucket = resource.bucket(bucket)
30
+ @bucket = Aws::S3::Bucket.new(name: bucket, **client_options)
34
31
  @prefix = prefix
35
32
  @upload_options = upload_options
36
- @concurrency = concurrency
33
+ @limits = limits
34
+ @disposition = disposition
37
35
  end
38
36
 
39
37
  # Initiates multipart upload for the given upload, and stores its
@@ -41,19 +39,11 @@ module Tus
41
39
  def create_file(uid, info = {})
42
40
  tus_info = Tus::Info.new(info)
43
41
 
44
- options = upload_options.dup
45
- options[:content_type] = tus_info.metadata["content_type"]
46
-
47
- if filename = tus_info.metadata["filename"]
48
- # Aws-sdk-s3 doesn't sign non-ASCII characters correctly, and browsers
49
- # will automatically URI-decode filenames.
50
- filename = CGI.escape(filename).gsub("+", " ")
51
-
52
- options[:content_disposition] ||= "inline"
53
- options[:content_disposition] += "; filename=\"#{filename}\""
42
+ if tus_info.length && tus_info.length > max_object_size
43
+ fail Tus::Error, "upload length exceeds maximum S3 object size"
54
44
  end
55
45
 
56
- multipart_upload = object(uid).initiate_multipart_upload(options)
46
+ multipart_upload = object(uid).initiate_multipart_upload(object_options(tus_info))
57
47
 
58
48
  info["multipart_id"] = multipart_upload.id
59
49
  info["multipart_parts"] = []
@@ -81,26 +71,25 @@ module Tus
81
71
  finalize_file(uid, info)
82
72
 
83
73
  delete(part_uids.flat_map { |part_uid| [object(part_uid), object("#{part_uid}.info")] })
84
-
85
- # Tus server requires us to return the size of the concatenated file.
86
- object = client.head_object(bucket: bucket.name, key: object(uid).key)
87
- object.content_length
88
- rescue => error
89
- abort_multipart_upload(multipart_upload) if multipart_upload
90
- raise error
74
+ rescue
75
+ multipart_upload&.abort
76
+ raise
91
77
  end
92
78
 
93
79
  # Appends data to the specified upload in a streaming fashion, and returns
94
80
  # the number of bytes it managed to save.
95
81
  #
96
- # The data read from the input is first buffered in memory, and once 5MB
97
- # (AWS S3's mininum allowed size for a multipart part) or more data has
98
- # been retrieved, it starts being uploaded in a background thread as the
99
- # next multipart part. This allows us to start reading the next chunk of
100
- # input data and soon as possible, achieving streaming.
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.
101
87
  #
102
- # If any network error is raised during the upload to S3, the upload of
103
- # further input data stops and the number of bytes that manged to get
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
104
93
  # uploaded is returned.
105
94
  def patch_file(uid, input, info = {})
106
95
  tus_info = Tus::Info.new(info)
@@ -109,21 +98,22 @@ module Tus
109
98
  part_offset = info["multipart_parts"].count
110
99
  bytes_uploaded = 0
111
100
 
112
- jobs = []
113
- chunk = input.read(MIN_PART_SIZE)
101
+ part_size = calculate_part_size(tus_info.length)
102
+
103
+ chunk = input.read(part_size)
114
104
 
115
105
  while chunk
116
- next_chunk = input.read(MIN_PART_SIZE)
106
+ next_chunk = input.read(part_size)
117
107
 
118
108
  # merge next chunk into previous if it's smaller than minimum chunk size
119
- if next_chunk && next_chunk.bytesize < MIN_PART_SIZE
109
+ if next_chunk && next_chunk.bytesize < part_size
120
110
  chunk << next_chunk
121
111
  next_chunk.clear
122
112
  next_chunk = nil
123
113
  end
124
114
 
125
- # abort if chunk is smaller than 5MB and is not the last chunk
126
- if chunk.bytesize < MIN_PART_SIZE
115
+ # abort if chunk is smaller than part size and is not the last chunk
116
+ if chunk.bytesize < part_size
127
117
  break if (tus_info.length && tus_info.offset) &&
128
118
  chunk.bytesize + tus_info.offset < tus_info.length
129
119
  end
@@ -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
- multipart_upload.complete(multipart_upload: { parts: parts })
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
- range = "bytes=#{range.begin}-#{range.end}" if range
181
- chunks = object(uid).enum_for(:get, range: range)
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
@@ -197,7 +199,7 @@ module Tus
197
199
  def delete_file(uid, info = {})
198
200
  if info["multipart_id"]
199
201
  multipart_upload = object(uid).multipart_upload(info["multipart_id"])
200
- abort_multipart_upload(multipart_upload)
202
+ multipart_upload.abort
201
203
 
202
204
  delete [object("#{uid}.info")]
203
205
  else
@@ -209,25 +211,30 @@ module Tus
209
211
  # multipart uploads still in progress, it checks the upload date of the
210
212
  # last multipart part.
211
213
  def expire_files(expiration_date)
212
- old_objects = bucket.objects.select do |object|
213
- object.last_modified <= expiration_date
214
- end
215
-
216
- delete(old_objects)
217
-
218
- bucket.multipart_uploads.each do |multipart_upload|
219
- # no need to check multipart uploads initiated before expiration date
220
- next if multipart_upload.initiated > expiration_date
214
+ delete bucket.objects(prefix: @prefix)
215
+ .select { |object| object.last_modified <= expiration_date }
216
+
217
+ bucket.multipart_uploads
218
+ .select { |multipart_upload| multipart_upload.key.start_with?(prefix.to_s) }
219
+ .select { |multipart_upload| multipart_upload.initiated <= expiration_date }
220
+ .select { |multipart_upload| multipart_upload.parts.all?{|p| p.last_modified <= expiration_date} }
221
+ .each(&:abort)
222
+ end
221
223
 
222
- most_recent_part = multipart_upload.parts.sort_by(&:last_modified).last
223
- if most_recent_part.nil? || most_recent_part.last_modified <= expiration_date
224
- abort_multipart_upload(multipart_upload)
225
- end
226
- end
224
+ def client
225
+ bucket.client
227
226
  end
228
227
 
229
228
  private
230
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
+
231
238
  # Uploads given body as a new multipart part with the specified part
232
239
  # number to the specified multipart upload. Returns part number and ETag
233
240
  # that will be required later for completing the multipart upload.
@@ -240,6 +247,23 @@ module Tus
240
247
  { "part_number" => part_number, "etag" => response.etag }
241
248
  end
242
249
 
250
+ # Calculates minimum multipart part size required to upload the whole
251
+ # file, taking into account AWS S3 multipart limits on part size and
252
+ # number of parts.
253
+ def calculate_part_size(length)
254
+ return min_part_size if length.nil? || length == 0 # reading 0 bytes never reaches the end
255
+ return length if length <= min_part_size
256
+ return min_part_size if length <= min_part_size * max_multipart_parts
257
+
258
+ part_size = Rational(length, max_multipart_parts).ceil
259
+
260
+ if part_size > max_part_size
261
+ fail Tus::Error, "chunk size for upload exceeds maximum part size"
262
+ end
263
+
264
+ part_size
265
+ end
266
+
243
267
  def delete(objects)
244
268
  # S3 can delete maximum of 1000 objects in a single request
245
269
  objects.each_slice(1000) do |objects_batch|
@@ -248,83 +272,40 @@ module Tus
248
272
  end
249
273
  end
250
274
 
251
- # In order to ensure the multipart upload was successfully aborted,
252
- # we need to check whether all parts have been deleted, and retry
253
- # the abort if the list is nonempty.
254
- def abort_multipart_upload(multipart_upload)
255
- loop do
256
- multipart_upload.abort
257
- break unless multipart_upload.parts.any?
258
- end
259
- rescue Aws::S3::Errors::NoSuchUpload
260
- # multipart upload was successfully aborted or doesn't exist
261
- end
262
-
263
275
  # Creates multipart parts for the specified multipart upload by copying
264
- # given objects into them. It uses a queue and a fixed-size thread pool
265
- # which consumes that queue.
276
+ # given objects into them.
266
277
  def copy_parts(objects, multipart_upload)
267
- parts = compute_parts(objects, multipart_upload)
268
- input = Queue.new
269
- results = Queue.new
270
-
271
- parts.each { |part| input << part }
272
- input.close
273
-
274
- thread_count = concurrency[:concatenation] || 10
275
- threads = thread_count.times.map { copy_part_thread(input, results) }
276
-
277
- errors = threads.map(&:value).compact
278
- fail errors.first if errors.any?
279
-
280
- part_results = Array.new(results.size) { results.pop } # convert Queue into an Array
281
- part_results.sort_by { |part| part.fetch("part_number") }
282
- end
283
-
284
- # Computes data required for copying objects into new multipart parts.
285
- def compute_parts(objects, multipart_upload)
286
- objects.map.with_index do |object, idx|
287
- {
288
- bucket: multipart_upload.bucket_name,
289
- key: multipart_upload.object_key,
290
- upload_id: multipart_upload.id,
291
- copy_source: [object.bucket_name, object.key].join("/"),
292
- part_number: idx + 1,
293
- }
278
+ threads = objects.map.with_index do |object, idx|
279
+ Thread.new { copy_part(object, idx + 1, multipart_upload) }
294
280
  end
295
- end
296
281
 
297
- # Consumes the queue for new multipart part information and issues the
298
- # copy requests.
299
- def copy_part_thread(input, results)
300
- Thread.new do
301
- begin
302
- loop do
303
- part = input.pop or break
304
- part_result = copy_part(part)
305
- results << part_result
306
- end
307
- nil
308
- rescue => error
309
- input.clear # clear other work
310
- error
311
- end
312
- end
282
+ threads.map(&:value)
313
283
  end
314
284
 
315
285
  # Creates a new multipart part by copying the object specified in the
316
286
  # given data. Returns part number and ETag that will be required later
317
287
  # for completing the multipart upload.
318
- def copy_part(part)
319
- response = client.upload_part_copy(part)
320
-
321
- { "part_number" => part[:part_number], "etag" => response.copy_part_result.etag }
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 }
322
298
  end
323
299
 
324
300
  # Retuns an Aws::S3::Object with the prefix applied.
325
301
  def object(key)
326
302
  bucket.object([*prefix, key].join("/"))
327
303
  end
304
+
305
+ def min_part_size; limits.fetch(:min_part_size, MIN_PART_SIZE); end
306
+ def max_part_size; limits.fetch(:max_part_size, MAX_PART_SIZE); end
307
+ def max_multipart_parts; limits.fetch(:max_multipart_parts, MAX_MULTIPART_PARTS); end
308
+ def max_object_size; limits.fetch(:max_object_size, MAX_OBJECT_SIZE); end
328
309
  end
329
310
  end
330
311
  end
data/tus-server.gemspec CHANGED
@@ -1,12 +1,12 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "tus-server"
3
- gem.version = "2.2.1"
3
+ gem.version = "3.0.0"
4
4
 
5
- gem.required_ruby_version = ">= 2.3"
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
 
9
- gem.homepage = "https://github.com/janko-m/tus-ruby-server"
9
+ gem.homepage = "https://github.com/janko/tus-ruby-server"
10
10
  gem.authors = ["Janko Marohnić"]
11
11
  gem.email = ["janko.marohnic@gmail.com"]
12
12
  gem.license = "MIT"
@@ -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", "~> 11.1"
20
+ gem.add_development_dependency "rake", ">= 12"
21
21
  gem.add_development_dependency "minitest", "~> 5.8"
22
- gem.add_development_dependency "rack-test_app"
23
- gem.add_development_dependency "cucumber", "~> 3.1"
24
- gem.add_development_dependency "mongo"
22
+ gem.add_development_dependency "rack-test"
23
+ gem.add_development_dependency "cucumber", ">= 3.1"
25
24
  gem.add_development_dependency "aws-sdk-s3", "~> 1.2"
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: 2.2.1
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: 2018-12-19 00:00:00.000000000 Z
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: '11.1'
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: '11.1'
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-test_app
75
+ name: rack-test
77
76
  requirement: !ruby/object:Gem::Requirement
78
77
  requirements:
79
78
  - - ">="
@@ -88,32 +87,18 @@ dependencies:
88
87
  version: '0'
89
88
  - !ruby/object:Gem::Dependency
90
89
  name: cucumber
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '3.1'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: '3.1'
103
- - !ruby/object:Gem::Dependency
104
- name: mongo
105
90
  requirement: !ruby/object:Gem::Requirement
106
91
  requirements:
107
92
  - - ">="
108
93
  - !ruby/object:Gem::Version
109
- version: '0'
94
+ version: '3.1'
110
95
  type: :development
111
96
  prerelease: false
112
97
  version_requirements: !ruby/object:Gem::Requirement
113
98
  requirements:
114
99
  - - ">="
115
100
  - !ruby/object:Gem::Version
116
- version: '0'
101
+ version: '3.1'
117
102
  - !ruby/object:Gem::Dependency
118
103
  name: aws-sdk-s3
119
104
  requirement: !ruby/object:Gem::Requirement
@@ -128,7 +113,6 @@ dependencies:
128
113
  - - "~>"
129
114
  - !ruby/object:Gem::Version
130
115
  version: '1.2'
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/input/unicorn.rb
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
- homepage: https://github.com/janko-m/tus-ruby-server
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,16 +144,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
144
  requirements:
163
145
  - - ">="
164
146
  - !ruby/object:Gem::Version
165
- version: '2.3'
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
- rubyforge_project:
173
- rubygems_version: 2.7.6
174
- signing_key:
154
+ rubygems_version: 4.0.20
175
155
  specification_version: 4
176
156
  summary: Ruby server implementation of tus.io, the open protocol for resumable file
177
157
  uploads.
@@ -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