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/README.md
CHANGED
|
@@ -4,6 +4,7 @@ A Ruby server for the [tus resumable upload protocol]. It implements the core
|
|
|
4
4
|
1.0 protocol, along with the following extensions:
|
|
5
5
|
|
|
6
6
|
* [`creation`][creation] (and `creation-defer-length`)
|
|
7
|
+
* [`creation-with-upload`][creation-with-upload]
|
|
7
8
|
* [`concatenation`][concatenation]
|
|
8
9
|
* [`checksum`][checksum]
|
|
9
10
|
* [`expiration`][expiration]
|
|
@@ -11,15 +12,14 @@ A Ruby server for the [tus resumable upload protocol]. It implements the core
|
|
|
11
12
|
|
|
12
13
|
## Installation
|
|
13
14
|
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
gem "tus-server", "~> 2.0"
|
|
15
|
+
```sh
|
|
16
|
+
$ bundle add tus-server
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
21
|
The gem provides a `Tus::Server` [Roda] app, which you can mount inside your
|
|
22
|
-
main application
|
|
22
|
+
main application:
|
|
23
23
|
|
|
24
24
|
```rb
|
|
25
25
|
# config/routes.rb (Rails)
|
|
@@ -28,86 +28,50 @@ Rails.application.routes.draw do
|
|
|
28
28
|
end
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```rb
|
|
34
|
-
# config.ru (Rack)
|
|
35
|
-
require "tus/server"
|
|
36
|
-
|
|
37
|
-
map "/files" do
|
|
38
|
-
run Tus::Server
|
|
39
|
-
end
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
Now you can tell your tus client library (e.g. [tus-js-client]) to use this
|
|
43
|
-
endpoint:
|
|
31
|
+
Then point your tus client (e.g. [tus-js-client] or [Uppy]) to this endpoint:
|
|
44
32
|
|
|
45
33
|
```js
|
|
46
34
|
// using tus-js-client
|
|
47
35
|
new tus.Upload(file, {
|
|
48
|
-
endpoint:
|
|
49
|
-
chunkSize: 5*1024*1024, // required unless using Falcon
|
|
36
|
+
endpoint: '/files',
|
|
37
|
+
chunkSize: 5*1024*1024, // required unless you're using Falcon
|
|
50
38
|
// ...
|
|
51
39
|
})
|
|
52
40
|
```
|
|
53
41
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
resumable walkthrough] that adds resumable uploads from scratch, and for a
|
|
59
|
-
complete example you can check out the [demo app][shrine-tus-demo].
|
|
60
|
-
|
|
61
|
-
### Streaming web server
|
|
62
|
-
|
|
63
|
-
Running the tus server alongside your main app using popular web servers like
|
|
64
|
-
Puma or Unicorn is probably fine for most cases, however, it does come with a
|
|
65
|
-
few gotchas. First, since these web servers don't accept partial requests
|
|
66
|
-
(request where the request body hasn't been fully received), the tus client
|
|
67
|
-
must be configured to split the upload into multiple requests. Second, since
|
|
68
|
-
web workers are tied for the duration of the request, serving uploaded files
|
|
69
|
-
through the tus server app could significantly impact request throughput; this
|
|
70
|
-
can be avoided by having your frontend server (Nginx) serve the files if using
|
|
71
|
-
`Filesystem` storage, or if you're using a cloud service like S3 having
|
|
72
|
-
download requests redirect to the service file URL.
|
|
73
|
-
|
|
74
|
-
That being said, there is a ruby web server that addresses these limitations
|
|
75
|
-
– [Falcon]. Falcon is part of the [`async` ecosystem][async], and it utilizes
|
|
76
|
-
non-blocking IO to process requests and responses in a streaming fashion
|
|
77
|
-
without tying up your web workers. This has several benefits for
|
|
78
|
-
`tus-ruby-server`:
|
|
79
|
-
|
|
80
|
-
* since tus server is called to handle the request as soon as the request
|
|
81
|
-
headers are received, data from the request body will be uploaded to the
|
|
82
|
-
configured storage as it's coming in from the client
|
|
83
|
-
|
|
84
|
-
* your web workers don't get tied up waiting for the client data, because as
|
|
85
|
-
soon as the server needs to wait for more data from the client, Falcon's
|
|
86
|
-
reactor switches to processing another request
|
|
87
|
-
|
|
88
|
-
* if the upload request that's in progress gets interrupted, tus server will be
|
|
89
|
-
able save data that has been received so far, so it's not necessary for the
|
|
90
|
-
tus client to split the upload into multiple chunks
|
|
91
|
-
|
|
92
|
-
* when uploaded files are being downloaded from the tus server, the request
|
|
93
|
-
throughput won't be impacted by the speed in which the client retrieves the
|
|
94
|
-
response body, because Falcon's reactor will switch to another request if the
|
|
95
|
-
client buffer gets full
|
|
96
|
-
|
|
97
|
-
Falcon provides a Rack adapter and is compatible with Rails, so you can use it
|
|
98
|
-
even if you're mounting `tus-ruby-server` inside your main app, just add
|
|
99
|
-
`falcon` to the Gemfile and run `falcon serve`.
|
|
42
|
+
```js
|
|
43
|
+
// using Uppy
|
|
44
|
+
import Uppy from '@uppy/core'
|
|
45
|
+
import Tus from '@uppy/tus'
|
|
100
46
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
47
|
+
new Uppy().use(Tus, {
|
|
48
|
+
endpoint: '/files',
|
|
49
|
+
chunkSize: 5*1024*1024, // required unless you're using Falcon
|
|
50
|
+
})
|
|
104
51
|
```
|
|
52
|
+
|
|
53
|
+
By default uploaded files are stored in the `data/` directory. To attach
|
|
54
|
+
uploaded files to database records, see [Shrine]'s [resumable uploads
|
|
55
|
+
walkthrough][shrine resumable walkthrough].
|
|
56
|
+
|
|
57
|
+
### Falcon
|
|
58
|
+
|
|
59
|
+
Web servers like Puma only call the app once the whole request body
|
|
60
|
+
has been received, so the tus client must split uploads into multiple chunks.
|
|
61
|
+
|
|
62
|
+
On [Falcon], the app is called as soon as the request headers arrive, so:
|
|
63
|
+
|
|
64
|
+
* the request body is written to storage as it comes in
|
|
65
|
+
* an interrupted upload keeps the data received so far, so clients don't need
|
|
66
|
+
to chunk uploads
|
|
67
|
+
* slow uploads and downloads don't block other requests
|
|
68
|
+
|
|
105
69
|
```sh
|
|
106
|
-
$
|
|
70
|
+
$ bundle add falcon
|
|
71
|
+
$ falcon serve # reads config.ru
|
|
107
72
|
```
|
|
108
73
|
|
|
109
|
-
|
|
110
|
-
a separate "rackup" file just for it and pointing Falcon to that rackup file:
|
|
74
|
+
To run tus-ruby-server as a standalone app, give it its own rackup file:
|
|
111
75
|
|
|
112
76
|
```rb
|
|
113
77
|
# tus.ru
|
|
@@ -128,7 +92,7 @@ $ falcon serve --config tus.ru
|
|
|
128
92
|
### Filesystem
|
|
129
93
|
|
|
130
94
|
By default `Tus::Server` stores uploaded files in the `data/` directory. You
|
|
131
|
-
can configure a different directory:
|
|
95
|
+
can configure a different directory, which will be created if it doesn't exist:
|
|
132
96
|
|
|
133
97
|
```rb
|
|
134
98
|
require "tus/storage/filesystem"
|
|
@@ -136,62 +100,37 @@ require "tus/storage/filesystem"
|
|
|
136
100
|
Tus::Server.opts[:storage] = Tus::Storage::Filesystem.new("public/tus")
|
|
137
101
|
```
|
|
138
102
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
directories, but you can set different permissions:
|
|
103
|
+
Files get 0644 permissions and directories get 0755 by default, which you can
|
|
104
|
+
change:
|
|
142
105
|
|
|
143
106
|
```rb
|
|
144
107
|
Tus::Storage::Filesystem.new("data", permissions: 0600, directory_permissions: 0777)
|
|
145
108
|
```
|
|
146
109
|
|
|
147
|
-
One downside of filesystem storage is that it doesn't work by default if you
|
|
148
|
-
want to run tus-ruby-server on multiple servers, you'd have to set up a shared
|
|
149
|
-
filesystem between the servers. Another downside is that you have to make sure
|
|
150
|
-
your servers have enough disk space. Also, if you're using Heroku, you cannot
|
|
151
|
-
store files on the filesystem as they won't persist.
|
|
152
|
-
|
|
153
|
-
All these are reasons why you might store uploaded data on a different storage,
|
|
154
|
-
and luckily tus-ruby-server ships with two more storages.
|
|
155
|
-
|
|
156
110
|
#### Serving files
|
|
157
111
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
serve additional requests for that duration.
|
|
162
|
-
|
|
163
|
-
Therefore, it's highly recommended to delegate serving uploaded files to your
|
|
164
|
-
frontend server (Nginx, Apache). This can be achieved with the
|
|
165
|
-
`Rack::Sendfile` middleware, see its [documentation][Rack::Sendfile] to learn
|
|
166
|
-
more about how to use it with popular frontend servers.
|
|
167
|
-
|
|
168
|
-
If you're using Rails, you can enable the `Rack::Sendfile` middleware by
|
|
169
|
-
setting the `config.action_dispatch.x_sendfile_header` value accordingly:
|
|
112
|
+
To have your frontend server (Nginx, Apache) serve files from the download
|
|
113
|
+
endpoint instead of the Ruby app, use the [`Rack::Sendfile`][Rack::Sendfile]
|
|
114
|
+
middleware:
|
|
170
115
|
|
|
171
116
|
```rb
|
|
172
|
-
|
|
173
|
-
# or
|
|
117
|
+
# Rails
|
|
174
118
|
Rails.application.config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # Nginx
|
|
119
|
+
# or "X-Sendfile" for Apache and lighttpd
|
|
175
120
|
```
|
|
176
|
-
|
|
177
|
-
Otherwise you can add the `Rack::Sendfile` middleware to the stack in
|
|
178
|
-
`config.ru`:
|
|
179
|
-
|
|
180
121
|
```rb
|
|
181
|
-
|
|
182
|
-
# or
|
|
122
|
+
# config.ru
|
|
183
123
|
use Rack::Sendfile, "X-Accel-Redirect" # Nginx
|
|
124
|
+
# or "X-Sendfile" for Apache and lighttpd
|
|
184
125
|
```
|
|
185
126
|
|
|
186
127
|
### Amazon S3
|
|
187
128
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
Gemfile.
|
|
129
|
+
`Tus::Storage::S3` uploads files to S3 using the multipart API. It requires the
|
|
130
|
+
[aws-sdk-s3] gem:
|
|
191
131
|
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
gem "aws-sdk-s3", "~> 1.2"
|
|
132
|
+
```sh
|
|
133
|
+
$ bundle add aws-sdk-s3
|
|
195
134
|
```
|
|
196
135
|
|
|
197
136
|
```rb
|
|
@@ -206,148 +145,93 @@ Tus::Server.opts[:storage] = Tus::Storage::S3.new(
|
|
|
206
145
|
)
|
|
207
146
|
```
|
|
208
147
|
|
|
209
|
-
|
|
210
|
-
last to be **5MB or larger**, so that is the minimum chunk size that you can
|
|
211
|
-
specify on your tus client if you want to use the S3 storage.
|
|
212
|
-
|
|
213
|
-
If you'll be retrieving uploaded files through the tus server app, it's
|
|
214
|
-
recommended to set `Tus::Server.opts[:redirect_download]` to `true`. This will
|
|
215
|
-
avoid tus server downloading and serving the file from S3, and instead have the
|
|
216
|
-
download endpoint redirect to the direct S3 object URL.
|
|
217
|
-
|
|
218
|
-
```rb
|
|
219
|
-
Tus::Server.opts[:redirect_download] = true
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
You can customize how the S3 object URL is being generated by passing a block
|
|
223
|
-
to `:redirect_download`, which will then be evaluated in the context of the
|
|
224
|
-
`Tus::Server` instance (which allows accessing the `request` object). See
|
|
225
|
-
[`Aws::S3::Object#get`] for the list of options that
|
|
226
|
-
`Tus::Storage::S3#file_url` accepts.
|
|
227
|
-
|
|
228
|
-
```rb
|
|
229
|
-
Tus::Server.opts[:redirect_download] = -> (uid, info, **options) do
|
|
230
|
-
storage.file_url(uid, info, expires_in: 10, **options) # link expires after 10 seconds
|
|
231
|
-
end
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
If you want to files to be stored in a certain subdirectory, you can specify
|
|
235
|
-
a `:prefix` in the storage configuration.
|
|
148
|
+
To store files in a subdirectory, specify a `:prefix`:
|
|
236
149
|
|
|
237
150
|
```rb
|
|
238
151
|
Tus::Storage::S3.new(prefix: "tus", **options)
|
|
239
152
|
```
|
|
240
153
|
|
|
241
|
-
|
|
242
|
-
|
|
154
|
+
The `name` and `type` upload metadata are saved as the S3 object's
|
|
155
|
+
`Content-Disposition` and `Content-Type`. The disposition is "inline" by
|
|
156
|
+
default, which you can change to "attachment" to force a download:
|
|
243
157
|
|
|
244
158
|
```rb
|
|
245
|
-
Tus::Storage::S3.new(
|
|
159
|
+
Tus::Storage::S3.new(disposition: "attachment", **options)
|
|
246
160
|
```
|
|
247
161
|
|
|
248
|
-
|
|
249
|
-
can for example change the `:endpoint` to use S3's accelerate host:
|
|
162
|
+
`:upload_options` are forwarded to [`Aws::S3::Client#create_multipart_upload`]:
|
|
250
163
|
|
|
251
164
|
```rb
|
|
252
|
-
Tus::Storage::S3.new(
|
|
165
|
+
Tus::Storage::S3.new(upload_options: { acl: "public-read" }, **options)
|
|
253
166
|
```
|
|
254
167
|
|
|
255
|
-
|
|
256
|
-
storage will copy partial uploads to the final upload (defaults to `10`):
|
|
168
|
+
All other options are forwarded to [`Aws::S3::Client#initialize`]:
|
|
257
169
|
|
|
258
170
|
```rb
|
|
259
|
-
Tus::Storage::S3.new(
|
|
171
|
+
Tus::Storage::S3.new(
|
|
172
|
+
use_accelerate_endpoint: true,
|
|
173
|
+
logger: Logger.new(STDOUT),
|
|
174
|
+
retry_limit: 5,
|
|
175
|
+
http_open_timeout: 10,
|
|
176
|
+
# ...
|
|
177
|
+
)
|
|
260
178
|
```
|
|
261
179
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
While tus-ruby-server doesn't currently ship with integrations for Google Cloud
|
|
265
|
-
Storage or Microsoft Azure Blob Storage, you can still use these services via
|
|
266
|
-
**[Minio]**.
|
|
267
|
-
|
|
268
|
-
Minio is an open source distributed object storage server that implements the
|
|
269
|
-
Amazon S3 API and provides gateways for [GCP][minio gcp] and [Azure][minio
|
|
270
|
-
azure]. This means it's possible to use the existing S3 tus-ruby-server
|
|
271
|
-
integration to point to the Minio server, which will then in turn forward those
|
|
272
|
-
calls to GCP or Azure.
|
|
273
|
-
|
|
274
|
-
Let's begin by installing Minio via Homebrew:
|
|
180
|
+
To use an S3-compatible service (e.g. MinIO), pass its `endpoint` and
|
|
181
|
+
`force_path_style: true`.
|
|
275
182
|
|
|
276
|
-
|
|
277
|
-
$ brew install minio/stable/minio
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
And starting the Minio server as a gateway to GCP or Azure:
|
|
183
|
+
#### Limits
|
|
281
184
|
|
|
282
|
-
|
|
283
|
-
# Google Cloud Storage
|
|
284
|
-
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/credentials.json
|
|
285
|
-
$ export MINIO_ACCESS_KEY=minioaccesskey
|
|
286
|
-
$ export MINIO_SECRET_KEY=miniosecretkey
|
|
287
|
-
$ minio gateway gcs yourprojectid
|
|
288
|
-
```
|
|
289
|
-
```sh
|
|
290
|
-
# Microsoft Azure Blob Storage
|
|
291
|
-
$ export MINIO_ACCESS_KEY=azureaccountname
|
|
292
|
-
$ export MINIO_SECRET_KEY=azureaccountkey
|
|
293
|
-
$ minio gateway azure
|
|
294
|
-
```
|
|
185
|
+
The S3 Multipart Upload API has the following limits:
|
|
295
186
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
187
|
+
| Item | Specification |
|
|
188
|
+
| ---- | ------------- |
|
|
189
|
+
| Part size | 5 MB to 5 GB, last part can be < 5 MB |
|
|
190
|
+
| Maximum number of parts per upload | 10,000 |
|
|
191
|
+
| Maximum object size | 5 TB |
|
|
299
192
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
secret_access_key: "<MINIO_SECRET_KEY>", # "SecretKey" value
|
|
304
|
-
endpoint: "<MINIO_ENDPOINT>", # "Endpoint" value
|
|
305
|
-
bucket: "<MINIO_BUCKET>", # name of the bucket you created
|
|
306
|
-
region: "us-east-1",
|
|
307
|
-
force_path_style: true,
|
|
308
|
-
)
|
|
309
|
-
```
|
|
193
|
+
So if your tus client chunks uploads, the chunk size must be **5 MB or
|
|
194
|
+
larger**. For files larger than 50 GB, the chunk size must be at least
|
|
195
|
+
`ceil(max_length / max_multipart_parts)`.
|
|
310
196
|
|
|
311
|
-
|
|
197
|
+
While the length of an upload is deferred, the storage can't tell whether a
|
|
198
|
+
chunk is the last one, so it uploads a chunk smaller than 5 MB as a part. If
|
|
199
|
+
more data follows it, S3 rejects the upload when it's completed, so this
|
|
200
|
+
limit matters most for uploads with a deferred length.
|
|
312
201
|
|
|
313
|
-
|
|
314
|
-
"[GridFS]". Tus-ruby-server ships with `Tus::Storage::Gridfs` that you can
|
|
315
|
-
use, which uses the [Mongo] gem.
|
|
202
|
+
If your S3-compatible service has different limits, pass them in:
|
|
316
203
|
|
|
317
204
|
```rb
|
|
318
|
-
|
|
319
|
-
|
|
205
|
+
Tus::Storage::S3.new(limits: {
|
|
206
|
+
min_part_size: 5 * 1024 * 1024,
|
|
207
|
+
max_part_size: 5 * 1024 * 1024 * 1024,
|
|
208
|
+
max_multipart_parts: 10_000,
|
|
209
|
+
max_object_size: 5 * 1024 * 1024 * 1024,
|
|
210
|
+
}, **options)
|
|
320
211
|
```
|
|
321
212
|
|
|
322
|
-
|
|
323
|
-
require "tus/storage/gridfs"
|
|
324
|
-
|
|
325
|
-
client = Mongo::Client.new("mongodb://127.0.0.1:27017/mydb")
|
|
326
|
-
Tus::Server.opts[:storage] = Tus::Storage::Gridfs.new(client: client)
|
|
327
|
-
```
|
|
213
|
+
#### Serving files
|
|
328
214
|
|
|
329
|
-
|
|
215
|
+
Set `:redirect_download` to have the download endpoint redirect to the S3
|
|
216
|
+
object URL instead of streaming the file through the app:
|
|
330
217
|
|
|
331
218
|
```rb
|
|
332
|
-
Tus::
|
|
219
|
+
Tus::Server.opts[:redirect_download] = true
|
|
333
220
|
```
|
|
334
221
|
|
|
335
|
-
|
|
336
|
-
|
|
222
|
+
You can pass a block to customize the URL. It's evaluated in the context of the
|
|
223
|
+
`Tus::Server` instance, and receives options that `Tus::Storage::S3#file_url`
|
|
224
|
+
accepts (see [`Aws::S3::Object#get`]):
|
|
337
225
|
|
|
338
226
|
```rb
|
|
339
|
-
Tus::
|
|
227
|
+
Tus::Server.opts[:redirect_download] = -> (uid, info, **options) do
|
|
228
|
+
storage.file_url(uid, info, expires_in: 10, **options) # link expires after 10 seconds
|
|
229
|
+
end
|
|
340
230
|
```
|
|
341
231
|
|
|
342
|
-
Note that if you're using the [concatenation] tus feature with Gridfs, all
|
|
343
|
-
partial uploads except the last one are required to fill in their Gridfs
|
|
344
|
-
chunks, meaning the length of each partial upload needs to be a multiple of the
|
|
345
|
-
`:chunk_size` number.
|
|
346
|
-
|
|
347
232
|
### Other storages
|
|
348
233
|
|
|
349
|
-
|
|
350
|
-
implement the same public interface:
|
|
234
|
+
A custom storage needs to implement the following interface:
|
|
351
235
|
|
|
352
236
|
```rb
|
|
353
237
|
def create_file(uid, info = {}) ... end
|
|
@@ -363,15 +247,15 @@ def expire_files(expiration_date) ... end
|
|
|
363
247
|
|
|
364
248
|
## Hooks
|
|
365
249
|
|
|
366
|
-
You can register code to
|
|
250
|
+
You can register code to run on the following events:
|
|
367
251
|
|
|
368
|
-
* `before_create` – before upload
|
|
369
|
-
* `after_create` –
|
|
370
|
-
* `after_finish` – after the last chunk
|
|
371
|
-
* `after_terminate` – after the upload
|
|
252
|
+
* `before_create` – before the upload is created
|
|
253
|
+
* `after_create` – after the upload is created
|
|
254
|
+
* `after_finish` – after the last chunk is stored
|
|
255
|
+
* `after_terminate` – after the upload is deleted
|
|
372
256
|
|
|
373
|
-
Each hook
|
|
374
|
-
|
|
257
|
+
Each hook receives the upload ID (`String`) and upload information
|
|
258
|
+
(`Tus::Info`):
|
|
375
259
|
|
|
376
260
|
```rb
|
|
377
261
|
Tus::Server.after_finish do |uid, info|
|
|
@@ -390,19 +274,8 @@ Tus::Server.after_finish do |uid, info|
|
|
|
390
274
|
end
|
|
391
275
|
```
|
|
392
276
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
and headers:
|
|
396
|
-
|
|
397
|
-
```rb
|
|
398
|
-
Tus::Server.after_terminate do |uid, info|
|
|
399
|
-
self #=> #<Tus::Server> (and #<Roda>)
|
|
400
|
-
request #=> #<Roda::Request>
|
|
401
|
-
response #=> #<Roda::Response>
|
|
402
|
-
end
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
So, with hooks you could for example add authentication to `Tus::Server`:
|
|
277
|
+
Hooks are evaluated inside the `Tus::Server` (Roda) instance, so they can
|
|
278
|
+
access the request and response, for example to add authentication:
|
|
406
279
|
|
|
407
280
|
```rb
|
|
408
281
|
Tus::Server.before_create do |uid, info|
|
|
@@ -416,24 +289,19 @@ Tus::Server.before_create do |uid, info|
|
|
|
416
289
|
end
|
|
417
290
|
```
|
|
418
291
|
|
|
419
|
-
|
|
420
|
-
[hooks][roda hooks] plugin to set `before` or `after` hooks for any request,
|
|
421
|
-
which are also evaluated in context of the `Roda` instance:
|
|
292
|
+
For hooks around every request, use Roda's [hooks][roda hooks] plugin:
|
|
422
293
|
|
|
423
294
|
```rb
|
|
424
295
|
Tus::Server.plugin :hooks
|
|
425
296
|
Tus::Server.before do
|
|
426
|
-
# called before each
|
|
297
|
+
# called before each request
|
|
427
298
|
end
|
|
428
299
|
Tus::Server.after do
|
|
429
|
-
# called after each
|
|
300
|
+
# called after each request
|
|
430
301
|
end
|
|
431
302
|
```
|
|
432
303
|
|
|
433
|
-
|
|
434
|
-
middlewares that your main app uses will also be called for requests routed to
|
|
435
|
-
the `Tus::Server`. If you want to add Rack middlewares to `Tus::Server`, you
|
|
436
|
-
can do it by calling `Tus::Server.use`:
|
|
304
|
+
You can also add Rack middleware to `Tus::Server`:
|
|
437
305
|
|
|
438
306
|
```rb
|
|
439
307
|
Tus::Server.use SomeRackMiddleware
|
|
@@ -441,8 +309,7 @@ Tus::Server.use SomeRackMiddleware
|
|
|
441
309
|
|
|
442
310
|
## Maximum size
|
|
443
311
|
|
|
444
|
-
|
|
445
|
-
can configure the maximum file size:
|
|
312
|
+
File size is unlimited by default, but you can set a maximum:
|
|
446
313
|
|
|
447
314
|
```rb
|
|
448
315
|
Tus::Server.opts[:max_size] = 5 * 1024*1024*1024 # 5GB
|
|
@@ -450,46 +317,38 @@ Tus::Server.opts[:max_size] = 5 * 1024*1024*1024 # 5GB
|
|
|
450
317
|
|
|
451
318
|
## Expiration
|
|
452
319
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
they were last updated, but you can modify `:expiration_time`:
|
|
320
|
+
Each upload gets an expiration date, which is refreshed on each PATCH request.
|
|
321
|
+
Uploads expire 7 days after they were last updated by default:
|
|
456
322
|
|
|
457
323
|
```rb
|
|
458
324
|
Tus::Server.opts[:expiration_time] = 2*24*60*60 # 2 days
|
|
459
325
|
```
|
|
460
326
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
that will call `#expire_files`.
|
|
327
|
+
Expired files aren't deleted automatically. Set up a recurring task that calls
|
|
328
|
+
`#expire_files` on the storage:
|
|
464
329
|
|
|
465
330
|
```rb
|
|
466
331
|
expiration_time = Tus::Server.opts[:expiration_time]
|
|
467
332
|
tus_storage = Tus::Server.opts[:storage]
|
|
468
333
|
expiration_date = Time.now.utc - expiration_time
|
|
469
334
|
|
|
470
|
-
tus_storage.expire_files(
|
|
335
|
+
tus_storage.expire_files(expiration_date)
|
|
471
336
|
```
|
|
472
337
|
|
|
473
338
|
## Download
|
|
474
339
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
340
|
+
Tus-ruby-server also provides a GET endpoint that streams the uploaded file
|
|
341
|
+
from the storage. It supports [Range requests], so the file URL can be used as
|
|
342
|
+
`src` in `<video>` and `<audio>` tags. See the storage sections above for how
|
|
343
|
+
to offload serving files from the app.
|
|
479
344
|
|
|
480
|
-
|
|
481
|
-
your frontend server if using disk storage, or if using S3 storage have the
|
|
482
|
-
download endpoint redirect to the S3 object URL. See the documentation for the
|
|
483
|
-
individual storage for instructions how to set this up.
|
|
345
|
+
The endpoint uses these `Upload-Metadata` values when they're available:
|
|
484
346
|
|
|
485
|
-
|
|
486
|
-
|
|
347
|
+
* `type` – sets the `Content-Type` response header
|
|
348
|
+
* `name` – sets the `Content-Disposition` response header
|
|
487
349
|
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
The `Content-Disposition` header will be set to "inline" by default, but you
|
|
492
|
-
can change it to "attachment" if you want the browser to always force download:
|
|
350
|
+
The disposition is "inline" by default, which you can change to "attachment"
|
|
351
|
+
to force a download:
|
|
493
352
|
|
|
494
353
|
```rb
|
|
495
354
|
Tus::Server.opts[:disposition] = "attachment"
|
|
@@ -497,43 +356,44 @@ Tus::Server.opts[:disposition] = "attachment"
|
|
|
497
356
|
|
|
498
357
|
## Checksum
|
|
499
358
|
|
|
500
|
-
The
|
|
359
|
+
The `checksum` extension supports SHA1, SHA256, SHA384, SHA512, MD5 and CRC32.
|
|
501
360
|
|
|
502
|
-
|
|
503
|
-
* SHA256
|
|
504
|
-
* SHA384
|
|
505
|
-
* SHA512
|
|
506
|
-
* MD5
|
|
507
|
-
* CRC32
|
|
361
|
+
## Locking
|
|
508
362
|
|
|
509
|
-
|
|
363
|
+
HEAD, PATCH and DELETE requests lock the upload, so that two requests can't
|
|
364
|
+
modify it at the same time. This happens when a client loses its connection
|
|
365
|
+
and resumes before the server notices, especially on Falcon, where the
|
|
366
|
+
original request keeps waiting for data.
|
|
510
367
|
|
|
511
|
-
When
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
with
|
|
515
|
-
`:concurrency`, and `Tus::Server` will automatically pick it up for its
|
|
516
|
-
validation:
|
|
368
|
+
When a request finds the upload locked by another request in the same process,
|
|
369
|
+
it asks that request to stop reading its body and save what it received, then
|
|
370
|
+
waits for the lock. If the lock isn't released within 20 seconds, it responds
|
|
371
|
+
with `423 Locked`. You can change the timeout:
|
|
517
372
|
|
|
518
373
|
```rb
|
|
519
|
-
Tus::
|
|
374
|
+
Tus::Server.opts[:lock_timeout] = 10 # seconds
|
|
520
375
|
```
|
|
521
376
|
|
|
522
|
-
|
|
377
|
+
Uploads are locked with lock files in the system's temporary directory, which
|
|
378
|
+
works across threads, fibers and processes on the same host. You can change
|
|
379
|
+
the directory, which needs to be on a local disk:
|
|
380
|
+
|
|
381
|
+
```rb
|
|
382
|
+
Tus::Server.opts[:locker] = Tus::Locker.new("tmp/tus-locks")
|
|
383
|
+
```
|
|
523
384
|
|
|
524
|
-
|
|
385
|
+
A request holding a lock in another process can't be asked to release it, so a
|
|
386
|
+
client resuming through a different process may get `423 Locked` until the
|
|
387
|
+
original request times out. Uploads aren't locked across servers, so run the
|
|
388
|
+
tus server on a single host.
|
|
389
|
+
|
|
390
|
+
## Tests
|
|
525
391
|
|
|
526
392
|
```sh
|
|
527
393
|
$ bundle exec rake test # unit tests
|
|
528
394
|
$ bundle exec cucumber # acceptance tests
|
|
529
395
|
```
|
|
530
396
|
|
|
531
|
-
Set `MONGO=1` environment variable if you want to also run MongoDB tests.
|
|
532
|
-
|
|
533
|
-
## Inspiration
|
|
534
|
-
|
|
535
|
-
The tus-ruby-server was inspired by [rubytus] and [tusd].
|
|
536
|
-
|
|
537
397
|
## License
|
|
538
398
|
|
|
539
399
|
[MIT](/LICENSE.txt)
|
|
@@ -542,17 +402,14 @@ The tus-ruby-server was inspired by [rubytus] and [tusd].
|
|
|
542
402
|
[roda hooks]: http://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/Hooks.html
|
|
543
403
|
[tus resumable upload protocol]: http://tus.io/
|
|
544
404
|
[tus-js-client]: https://github.com/tus/tus-js-client
|
|
405
|
+
[Uppy]: https://uppy.io
|
|
545
406
|
[creation]: http://tus.io/protocols/resumable-upload.html#creation
|
|
407
|
+
[creation-with-upload]: http://tus.io/protocols/resumable-upload.html#creation-with-upload
|
|
546
408
|
[concatenation]: http://tus.io/protocols/resumable-upload.html#concatenation
|
|
547
409
|
[checksum]: http://tus.io/protocols/resumable-upload.html#checksum
|
|
548
410
|
[expiration]: http://tus.io/protocols/resumable-upload.html#expiration
|
|
549
411
|
[termination]: http://tus.io/protocols/resumable-upload.html#termination
|
|
550
|
-
[GridFS]: https://docs.mongodb.org/v3.0/core/gridfs/
|
|
551
|
-
[Mongo]: https://github.com/mongodb/mongo-ruby-driver
|
|
552
|
-
[shrine-tus-demo]: https://github.com/shrinerb/shrine-tus-demo
|
|
553
412
|
[Shrine]: https://github.com/shrinerb/shrine
|
|
554
|
-
[trailing headers]: https://tools.ietf.org/html/rfc7230#section-4.1.2
|
|
555
|
-
[rubytus]: https://github.com/picocandy/rubytus
|
|
556
413
|
[aws-sdk-s3]: https://github.com/aws/aws-sdk-ruby/tree/master/gems/aws-sdk-s3
|
|
557
414
|
[`Aws::S3::Client#initialize`]: http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#initialize-instance_method
|
|
558
415
|
[`Aws::S3::Client#create_multipart_upload`]: http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#create_multipart_upload-instance_method
|
|
@@ -560,9 +417,4 @@ The tus-ruby-server was inspired by [rubytus] and [tusd].
|
|
|
560
417
|
[Rack::Sendfile]: https://www.rubydoc.info/github/rack/rack/master/Rack/Sendfile
|
|
561
418
|
[`Aws::S3::Object#get`]: https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Object.html#get-instance_method
|
|
562
419
|
[shrine resumable walkthrough]: https://github.com/shrinerb/shrine/wiki/Adding-Resumable-Uploads
|
|
563
|
-
[Minio]: https://minio.io
|
|
564
|
-
[minio gcp]: https://minio.io/gcp.html
|
|
565
|
-
[minio azure]: https://minio.io/azure.html
|
|
566
|
-
[tusd]: https://github.com/tus/tusd
|
|
567
420
|
[Falcon]: https://github.com/socketry/falcon
|
|
568
|
-
[async]: https://github.com/socketry
|