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/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,21 +145,27 @@ Tus::Server.opts[:storage] = Tus::Storage::S3.new(
|
|
|
206
145
|
)
|
|
207
146
|
```
|
|
208
147
|
|
|
209
|
-
|
|
210
|
-
a `:prefix` in the storage configuration.
|
|
148
|
+
To store files in a subdirectory, specify a `:prefix`:
|
|
211
149
|
|
|
212
150
|
```rb
|
|
213
151
|
Tus::Storage::S3.new(prefix: "tus", **options)
|
|
214
152
|
```
|
|
215
153
|
|
|
216
|
-
|
|
217
|
-
|
|
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:
|
|
157
|
+
|
|
158
|
+
```rb
|
|
159
|
+
Tus::Storage::S3.new(disposition: "attachment", **options)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`:upload_options` are forwarded to [`Aws::S3::Client#create_multipart_upload`]:
|
|
218
163
|
|
|
219
164
|
```rb
|
|
220
165
|
Tus::Storage::S3.new(upload_options: { acl: "public-read" }, **options)
|
|
221
166
|
```
|
|
222
167
|
|
|
223
|
-
All other options
|
|
168
|
+
All other options are forwarded to [`Aws::S3::Client#initialize`]:
|
|
224
169
|
|
|
225
170
|
```rb
|
|
226
171
|
Tus::Storage::S3.new(
|
|
@@ -232,16 +177,12 @@ Tus::Storage::S3.new(
|
|
|
232
177
|
)
|
|
233
178
|
```
|
|
234
179
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
```rb
|
|
239
|
-
Tus::Storage::S3.new(concurrency: { concatenation: 20 }, **options)
|
|
240
|
-
```
|
|
180
|
+
To use an S3-compatible service (e.g. MinIO), pass its `endpoint` and
|
|
181
|
+
`force_path_style: true`.
|
|
241
182
|
|
|
242
183
|
#### Limits
|
|
243
184
|
|
|
244
|
-
|
|
185
|
+
The S3 Multipart Upload API has the following limits:
|
|
245
186
|
|
|
246
187
|
| Item | Specification |
|
|
247
188
|
| ---- | ------------- |
|
|
@@ -249,16 +190,16 @@ Be aware that the AWS S3 Multipart Upload API has the following limits:
|
|
|
249
190
|
| Maximum number of parts per upload | 10,000 |
|
|
250
191
|
| Maximum object size | 5 TB |
|
|
251
192
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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)`.
|
|
196
|
+
|
|
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.
|
|
258
201
|
|
|
259
|
-
|
|
260
|
-
part size. If you're using a different S3-compatible service which has different
|
|
261
|
-
limits, you should pass them in when initializing the storage:
|
|
202
|
+
If your S3-compatible service has different limits, pass them in:
|
|
262
203
|
|
|
263
204
|
```rb
|
|
264
205
|
Tus::Storage::S3.new(limits: {
|
|
@@ -271,20 +212,16 @@ Tus::Storage::S3.new(limits: {
|
|
|
271
212
|
|
|
272
213
|
#### Serving files
|
|
273
214
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
avoid tus server downloading and serving the file from S3, and instead have the
|
|
277
|
-
download endpoint redirect to the direct S3 object URL.
|
|
215
|
+
Set `:redirect_download` to have the download endpoint redirect to the S3
|
|
216
|
+
object URL instead of streaming the file through the app:
|
|
278
217
|
|
|
279
218
|
```rb
|
|
280
219
|
Tus::Server.opts[:redirect_download] = true
|
|
281
220
|
```
|
|
282
221
|
|
|
283
|
-
You can
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
[`Aws::S3::Object#get`] for the list of options that
|
|
287
|
-
`Tus::Storage::S3#file_url` accepts.
|
|
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`]):
|
|
288
225
|
|
|
289
226
|
```rb
|
|
290
227
|
Tus::Server.opts[:redirect_download] = -> (uid, info, **options) do
|
|
@@ -292,95 +229,9 @@ Tus::Server.opts[:redirect_download] = -> (uid, info, **options) do
|
|
|
292
229
|
end
|
|
293
230
|
```
|
|
294
231
|
|
|
295
|
-
### Google Cloud Storage, Microsoft Azure Blob Storage
|
|
296
|
-
|
|
297
|
-
While tus-ruby-server doesn't currently ship with integrations for Google Cloud
|
|
298
|
-
Storage or Microsoft Azure Blob Storage, you can still use these services via
|
|
299
|
-
**[Minio]**.
|
|
300
|
-
|
|
301
|
-
Minio is an open source distributed object storage server that implements the
|
|
302
|
-
Amazon S3 API and provides gateways for [GCP][minio gcp] and [Azure][minio
|
|
303
|
-
azure]. This means it's possible to use the existing S3 tus-ruby-server
|
|
304
|
-
integration to point to the Minio server, which will then in turn forward those
|
|
305
|
-
calls to GCP or Azure.
|
|
306
|
-
|
|
307
|
-
Let's begin by installing Minio via Homebrew:
|
|
308
|
-
|
|
309
|
-
```sh
|
|
310
|
-
$ brew install minio/stable/minio
|
|
311
|
-
```
|
|
312
|
-
|
|
313
|
-
And starting the Minio server as a gateway to GCP or Azure:
|
|
314
|
-
|
|
315
|
-
```sh
|
|
316
|
-
# Google Cloud Storage
|
|
317
|
-
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/credentials.json
|
|
318
|
-
$ export MINIO_ACCESS_KEY=minioaccesskey
|
|
319
|
-
$ export MINIO_SECRET_KEY=miniosecretkey
|
|
320
|
-
$ minio gateway gcs yourprojectid
|
|
321
|
-
```
|
|
322
|
-
```sh
|
|
323
|
-
# Microsoft Azure Blob Storage
|
|
324
|
-
$ export MINIO_ACCESS_KEY=azureaccountname
|
|
325
|
-
$ export MINIO_SECRET_KEY=azureaccountkey
|
|
326
|
-
$ minio gateway azure
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
Now follow the displayed link to open the Minio web UI and create a bucket in
|
|
330
|
-
which you want your files to be stored. Once you've done that, you can
|
|
331
|
-
initialize `Tus::Storage::S3` to point to your Minio server and bucket:
|
|
332
|
-
|
|
333
|
-
```rb
|
|
334
|
-
Tus::Storage::S3.new(
|
|
335
|
-
access_key_id: "<MINIO_ACCESS_KEY>", # "AccessKey" value
|
|
336
|
-
secret_access_key: "<MINIO_SECRET_KEY>", # "SecretKey" value
|
|
337
|
-
endpoint: "<MINIO_ENDPOINT>", # "Endpoint" value
|
|
338
|
-
bucket: "<MINIO_BUCKET>", # name of the bucket you created
|
|
339
|
-
region: "us-east-1",
|
|
340
|
-
force_path_style: true,
|
|
341
|
-
)
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
### MongoDB GridFS
|
|
345
|
-
|
|
346
|
-
MongoDB has a specification for storing and retrieving large files, called
|
|
347
|
-
"[GridFS]". Tus-ruby-server ships with `Tus::Storage::Gridfs` that you can
|
|
348
|
-
use, which uses the [Mongo] gem.
|
|
349
|
-
|
|
350
|
-
```rb
|
|
351
|
-
# Gemfile
|
|
352
|
-
gem "mongo", "~> 2.3"
|
|
353
|
-
```
|
|
354
|
-
|
|
355
|
-
```rb
|
|
356
|
-
require "tus/storage/gridfs"
|
|
357
|
-
|
|
358
|
-
client = Mongo::Client.new("mongodb://127.0.0.1:27017/mydb")
|
|
359
|
-
Tus::Server.opts[:storage] = Tus::Storage::Gridfs.new(client: client)
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
You can change the database prefix (defaults to `fs`):
|
|
363
|
-
|
|
364
|
-
```rb
|
|
365
|
-
Tus::Storage::Gridfs.new(client: client, prefix: "fs_temp")
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
By default MongoDB Gridfs stores files in chunks of 256KB, but you can change
|
|
369
|
-
that with the `:chunk_size` option:
|
|
370
|
-
|
|
371
|
-
```rb
|
|
372
|
-
Tus::Storage::Gridfs.new(client: client, chunk_size: 1*1024*1024) # 1 MB
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
Note that if you're using the [concatenation] tus feature with Gridfs, all
|
|
376
|
-
partial uploads except the last one are required to fill in their Gridfs
|
|
377
|
-
chunks, meaning the length of each partial upload needs to be a multiple of the
|
|
378
|
-
`:chunk_size` number.
|
|
379
|
-
|
|
380
232
|
### Other storages
|
|
381
233
|
|
|
382
|
-
|
|
383
|
-
implement the same public interface:
|
|
234
|
+
A custom storage needs to implement the following interface:
|
|
384
235
|
|
|
385
236
|
```rb
|
|
386
237
|
def create_file(uid, info = {}) ... end
|
|
@@ -396,15 +247,15 @@ def expire_files(expiration_date) ... end
|
|
|
396
247
|
|
|
397
248
|
## Hooks
|
|
398
249
|
|
|
399
|
-
You can register code to
|
|
250
|
+
You can register code to run on the following events:
|
|
400
251
|
|
|
401
|
-
* `before_create` – before upload
|
|
402
|
-
* `after_create` –
|
|
403
|
-
* `after_finish` – after the last chunk
|
|
404
|
-
* `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
|
|
405
256
|
|
|
406
|
-
Each hook
|
|
407
|
-
|
|
257
|
+
Each hook receives the upload ID (`String`) and upload information
|
|
258
|
+
(`Tus::Info`):
|
|
408
259
|
|
|
409
260
|
```rb
|
|
410
261
|
Tus::Server.after_finish do |uid, info|
|
|
@@ -423,19 +274,8 @@ Tus::Server.after_finish do |uid, info|
|
|
|
423
274
|
end
|
|
424
275
|
```
|
|
425
276
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
and headers:
|
|
429
|
-
|
|
430
|
-
```rb
|
|
431
|
-
Tus::Server.after_terminate do |uid, info|
|
|
432
|
-
self #=> #<Tus::Server> (and #<Roda>)
|
|
433
|
-
request #=> #<Roda::Request>
|
|
434
|
-
response #=> #<Roda::Response>
|
|
435
|
-
end
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
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:
|
|
439
279
|
|
|
440
280
|
```rb
|
|
441
281
|
Tus::Server.before_create do |uid, info|
|
|
@@ -449,24 +289,19 @@ Tus::Server.before_create do |uid, info|
|
|
|
449
289
|
end
|
|
450
290
|
```
|
|
451
291
|
|
|
452
|
-
|
|
453
|
-
[hooks][roda hooks] plugin to set `before` or `after` hooks for any request,
|
|
454
|
-
which are also evaluated in context of the `Roda` instance:
|
|
292
|
+
For hooks around every request, use Roda's [hooks][roda hooks] plugin:
|
|
455
293
|
|
|
456
294
|
```rb
|
|
457
295
|
Tus::Server.plugin :hooks
|
|
458
296
|
Tus::Server.before do
|
|
459
|
-
# called before each
|
|
297
|
+
# called before each request
|
|
460
298
|
end
|
|
461
299
|
Tus::Server.after do
|
|
462
|
-
# called after each
|
|
300
|
+
# called after each request
|
|
463
301
|
end
|
|
464
302
|
```
|
|
465
303
|
|
|
466
|
-
|
|
467
|
-
middlewares that your main app uses will also be called for requests routed to
|
|
468
|
-
the `Tus::Server`. If you want to add Rack middlewares to `Tus::Server`, you
|
|
469
|
-
can do it by calling `Tus::Server.use`:
|
|
304
|
+
You can also add Rack middleware to `Tus::Server`:
|
|
470
305
|
|
|
471
306
|
```rb
|
|
472
307
|
Tus::Server.use SomeRackMiddleware
|
|
@@ -474,8 +309,7 @@ Tus::Server.use SomeRackMiddleware
|
|
|
474
309
|
|
|
475
310
|
## Maximum size
|
|
476
311
|
|
|
477
|
-
|
|
478
|
-
can configure the maximum file size:
|
|
312
|
+
File size is unlimited by default, but you can set a maximum:
|
|
479
313
|
|
|
480
314
|
```rb
|
|
481
315
|
Tus::Server.opts[:max_size] = 5 * 1024*1024*1024 # 5GB
|
|
@@ -483,46 +317,38 @@ Tus::Server.opts[:max_size] = 5 * 1024*1024*1024 # 5GB
|
|
|
483
317
|
|
|
484
318
|
## Expiration
|
|
485
319
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
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:
|
|
489
322
|
|
|
490
323
|
```rb
|
|
491
324
|
Tus::Server.opts[:expiration_time] = 2*24*60*60 # 2 days
|
|
492
325
|
```
|
|
493
326
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
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:
|
|
497
329
|
|
|
498
330
|
```rb
|
|
499
331
|
expiration_time = Tus::Server.opts[:expiration_time]
|
|
500
332
|
tus_storage = Tus::Server.opts[:storage]
|
|
501
333
|
expiration_date = Time.now.utc - expiration_time
|
|
502
334
|
|
|
503
|
-
tus_storage.expire_files(
|
|
335
|
+
tus_storage.expire_files(expiration_date)
|
|
504
336
|
```
|
|
505
337
|
|
|
506
338
|
## Download
|
|
507
339
|
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
It's highly recommended not to serve files through the app, but offload it to
|
|
514
|
-
your frontend server if using disk storage, or if using S3 storage have the
|
|
515
|
-
download endpoint redirect to the S3 object URL. See the documentation for the
|
|
516
|
-
individual storage for instructions how to set this up.
|
|
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.
|
|
517
344
|
|
|
518
|
-
The endpoint
|
|
519
|
-
they're available:
|
|
345
|
+
The endpoint uses these `Upload-Metadata` values when they're available:
|
|
520
346
|
|
|
521
|
-
* `type`
|
|
522
|
-
* `name`
|
|
347
|
+
* `type` – sets the `Content-Type` response header
|
|
348
|
+
* `name` – sets the `Content-Disposition` response header
|
|
523
349
|
|
|
524
|
-
The
|
|
525
|
-
|
|
350
|
+
The disposition is "inline" by default, which you can change to "attachment"
|
|
351
|
+
to force a download:
|
|
526
352
|
|
|
527
353
|
```rb
|
|
528
354
|
Tus::Server.opts[:disposition] = "attachment"
|
|
@@ -530,43 +356,44 @@ Tus::Server.opts[:disposition] = "attachment"
|
|
|
530
356
|
|
|
531
357
|
## Checksum
|
|
532
358
|
|
|
533
|
-
The
|
|
359
|
+
The `checksum` extension supports SHA1, SHA256, SHA384, SHA512, MD5 and CRC32.
|
|
534
360
|
|
|
535
|
-
|
|
536
|
-
* SHA256
|
|
537
|
-
* SHA384
|
|
538
|
-
* SHA512
|
|
539
|
-
* MD5
|
|
540
|
-
* CRC32
|
|
361
|
+
## Locking
|
|
541
362
|
|
|
542
|
-
|
|
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.
|
|
543
367
|
|
|
544
|
-
When
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
with
|
|
548
|
-
`:concurrency`, and `Tus::Server` will automatically pick it up for its
|
|
549
|
-
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:
|
|
550
372
|
|
|
551
373
|
```rb
|
|
552
|
-
Tus::
|
|
374
|
+
Tus::Server.opts[:lock_timeout] = 10 # seconds
|
|
553
375
|
```
|
|
554
376
|
|
|
555
|
-
|
|
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
|
+
```
|
|
556
384
|
|
|
557
|
-
|
|
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
|
|
558
391
|
|
|
559
392
|
```sh
|
|
560
393
|
$ bundle exec rake test # unit tests
|
|
561
394
|
$ bundle exec cucumber # acceptance tests
|
|
562
395
|
```
|
|
563
396
|
|
|
564
|
-
Set `MONGO=1` environment variable if you want to also run MongoDB tests.
|
|
565
|
-
|
|
566
|
-
## Inspiration
|
|
567
|
-
|
|
568
|
-
The tus-ruby-server was inspired by [rubytus] and [tusd].
|
|
569
|
-
|
|
570
397
|
## License
|
|
571
398
|
|
|
572
399
|
[MIT](/LICENSE.txt)
|
|
@@ -575,17 +402,14 @@ The tus-ruby-server was inspired by [rubytus] and [tusd].
|
|
|
575
402
|
[roda hooks]: http://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/Hooks.html
|
|
576
403
|
[tus resumable upload protocol]: http://tus.io/
|
|
577
404
|
[tus-js-client]: https://github.com/tus/tus-js-client
|
|
405
|
+
[Uppy]: https://uppy.io
|
|
578
406
|
[creation]: http://tus.io/protocols/resumable-upload.html#creation
|
|
407
|
+
[creation-with-upload]: http://tus.io/protocols/resumable-upload.html#creation-with-upload
|
|
579
408
|
[concatenation]: http://tus.io/protocols/resumable-upload.html#concatenation
|
|
580
409
|
[checksum]: http://tus.io/protocols/resumable-upload.html#checksum
|
|
581
410
|
[expiration]: http://tus.io/protocols/resumable-upload.html#expiration
|
|
582
411
|
[termination]: http://tus.io/protocols/resumable-upload.html#termination
|
|
583
|
-
[GridFS]: https://docs.mongodb.org/v3.0/core/gridfs/
|
|
584
|
-
[Mongo]: https://github.com/mongodb/mongo-ruby-driver
|
|
585
|
-
[shrine-tus-demo]: https://github.com/shrinerb/shrine-tus-demo
|
|
586
412
|
[Shrine]: https://github.com/shrinerb/shrine
|
|
587
|
-
[trailing headers]: https://tools.ietf.org/html/rfc7230#section-4.1.2
|
|
588
|
-
[rubytus]: https://github.com/picocandy/rubytus
|
|
589
413
|
[aws-sdk-s3]: https://github.com/aws/aws-sdk-ruby/tree/master/gems/aws-sdk-s3
|
|
590
414
|
[`Aws::S3::Client#initialize`]: http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#initialize-instance_method
|
|
591
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
|
|
@@ -593,9 +417,4 @@ The tus-ruby-server was inspired by [rubytus] and [tusd].
|
|
|
593
417
|
[Rack::Sendfile]: https://www.rubydoc.info/github/rack/rack/master/Rack/Sendfile
|
|
594
418
|
[`Aws::S3::Object#get`]: https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Object.html#get-instance_method
|
|
595
419
|
[shrine resumable walkthrough]: https://github.com/shrinerb/shrine/wiki/Adding-Resumable-Uploads
|
|
596
|
-
[Minio]: https://minio.io
|
|
597
|
-
[minio gcp]: https://minio.io/gcp.html
|
|
598
|
-
[minio azure]: https://minio.io/azure.html
|
|
599
|
-
[tusd]: https://github.com/tus/tusd
|
|
600
420
|
[Falcon]: https://github.com/socketry/falcon
|
|
601
|
-
[async]: https://github.com/socketry
|