tus-server 2.2.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +181 -85
- data/README.md +152 -300
- data/lib/tus/checksum.rb +4 -28
- data/lib/tus/errors.rb +2 -0
- data/lib/tus/info.rb +23 -14
- data/lib/tus/input.rb +53 -8
- data/lib/tus/locker.rb +108 -0
- data/lib/tus/response.rb +1 -1
- data/lib/tus/server.rb +225 -159
- data/lib/tus/storage/filesystem.rb +25 -8
- data/lib/tus/storage/s3.rb +106 -125
- data/tus-server.gemspec +6 -7
- metadata +13 -33
- data/lib/tus/input/unicorn.rb +0 -16
- data/lib/tus/storage/gridfs.rb +0 -278
data/lib/tus/checksum.rb
CHANGED
|
@@ -12,6 +12,7 @@ module Tus
|
|
|
12
12
|
# * CRC32
|
|
13
13
|
class Checksum
|
|
14
14
|
CHUNK_SIZE = 16*1024
|
|
15
|
+
DIGESTS = { "sha1" => :SHA1, "sha256" => :SHA256, "sha384" => :SHA384, "sha512" => :SHA512, "md5" => :MD5 }
|
|
15
16
|
|
|
16
17
|
attr_reader :algorithm
|
|
17
18
|
|
|
@@ -23,46 +24,21 @@ module Tus
|
|
|
23
24
|
@algorithm = algorithm
|
|
24
25
|
end
|
|
25
26
|
|
|
26
|
-
def match?(checksum, io)
|
|
27
|
-
generate(io) == checksum
|
|
28
|
-
end
|
|
29
|
-
|
|
30
27
|
def generate(io)
|
|
31
|
-
hash =
|
|
28
|
+
hash = algorithm.to_s == "crc32" ? crc32(io) : digest(DIGESTS.fetch(algorithm.to_s), io)
|
|
32
29
|
io.rewind
|
|
33
30
|
hash
|
|
34
31
|
end
|
|
35
32
|
|
|
36
33
|
private
|
|
37
34
|
|
|
38
|
-
def
|
|
39
|
-
digest(:SHA1, io)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def generate_sha256(io)
|
|
43
|
-
digest(:SHA256, io)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def generate_sha384(io)
|
|
47
|
-
digest(:SHA384, io)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def generate_sha512(io)
|
|
51
|
-
digest(:SHA512, io)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def generate_md5(io)
|
|
55
|
-
digest(:MD5, io)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def generate_crc32(io)
|
|
35
|
+
def crc32(io)
|
|
59
36
|
require "zlib"
|
|
60
|
-
require "base64"
|
|
61
37
|
crc = Zlib.crc32("")
|
|
62
38
|
while (data = io.read(CHUNK_SIZE, buffer ||= String.new))
|
|
63
39
|
crc = Zlib.crc32(data, crc)
|
|
64
40
|
end
|
|
65
|
-
|
|
41
|
+
[crc.to_s].pack("m0") # Base64
|
|
66
42
|
end
|
|
67
43
|
|
|
68
44
|
def digest(name, io)
|
data/lib/tus/errors.rb
CHANGED
data/lib/tus/info.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen-string-literal: true
|
|
2
2
|
|
|
3
|
-
require "base64"
|
|
4
3
|
require "time"
|
|
5
4
|
|
|
6
5
|
module Tus
|
|
@@ -35,18 +34,36 @@ module Tus
|
|
|
35
34
|
@hash.select { |key, value| HEADERS.include?(key) && !value.nil? }
|
|
36
35
|
end
|
|
37
36
|
|
|
37
|
+
# Parsed as decimal, as Integer() would read a leading zero as octal.
|
|
38
38
|
def length
|
|
39
|
-
Integer(@hash["Upload-Length"]) if @hash["Upload-Length"]
|
|
39
|
+
Integer(@hash["Upload-Length"], 10) if @hash["Upload-Length"]
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def offset
|
|
43
|
-
Integer(@hash["Upload-Offset"])
|
|
43
|
+
Integer(@hash["Upload-Offset"], 10) if @hash["Upload-Offset"]
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
def metadata
|
|
47
47
|
parse_metadata(@hash["Upload-Metadata"])
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
def name
|
|
51
|
+
metadata.values_at("name", "filename").compact.first
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# A token as defined in RFC 9110.
|
|
55
|
+
TOKEN = /[!#$%&'*+\-.^_`|~0-9A-Za-z]+/
|
|
56
|
+
# A media type with optional parameters, e.g. "text/plain; charset=utf-8".
|
|
57
|
+
MEDIA_TYPE = /\A#{TOKEN}\/#{TOKEN}(?:[ \t]*;[ \t]*#{TOKEN}=(?:#{TOKEN}|"[^"\\\x00-\x1f\x7f]*"))*\z/
|
|
58
|
+
|
|
59
|
+
# Returns the media type from metadata. Returns nil if it isn't a valid
|
|
60
|
+
# media type, because it's used as a header value, where it could otherwise
|
|
61
|
+
# inject headers.
|
|
62
|
+
def type
|
|
63
|
+
type = metadata.values_at("type", "content_type").compact.first
|
|
64
|
+
type if type && type.b.match?(MEDIA_TYPE)
|
|
65
|
+
end
|
|
66
|
+
|
|
50
67
|
def expires
|
|
51
68
|
Time.parse(@hash["Upload-Expires"])
|
|
52
69
|
end
|
|
@@ -68,23 +85,15 @@ module Tus
|
|
|
68
85
|
@hash["Upload-Defer-Length"] == "1"
|
|
69
86
|
end
|
|
70
87
|
|
|
71
|
-
def remaining_length
|
|
72
|
-
length - offset
|
|
73
|
-
end
|
|
74
|
-
|
|
75
88
|
private
|
|
76
89
|
|
|
77
90
|
def parse_metadata(string)
|
|
78
91
|
return {} if string == nil || string == ""
|
|
79
92
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
hash.each do |key, value|
|
|
84
|
-
hash[key] = value && Base64.decode64(value)
|
|
93
|
+
string.split(",").to_h do |pair|
|
|
94
|
+
key, value = pair.split(" ")
|
|
95
|
+
[key, value&.unpack1("m")] # Base64
|
|
85
96
|
end
|
|
86
|
-
|
|
87
|
-
hash
|
|
88
97
|
end
|
|
89
98
|
end
|
|
90
99
|
end
|
data/lib/tus/input.rb
CHANGED
|
@@ -1,24 +1,45 @@
|
|
|
1
1
|
# frozen-string-literal: true
|
|
2
2
|
|
|
3
|
-
require "tus/input/unicorn"
|
|
4
3
|
require "tus/errors"
|
|
5
4
|
|
|
5
|
+
require "stringio"
|
|
6
|
+
require "tempfile"
|
|
7
|
+
|
|
6
8
|
module Tus
|
|
7
9
|
# Wrapper around the Rack input, which adds the ability to limit the amount of
|
|
8
10
|
# bytes that will be read from the Rack input. If there are more bytes in the
|
|
9
11
|
# Rack input than the specified limit, a Tus::MaxSizeExceeded exception is
|
|
10
12
|
# raised.
|
|
13
|
+
#
|
|
14
|
+
# The input can also be interrupted, after which it behaves as if it reached
|
|
15
|
+
# the end, so that the data received so far is saved.
|
|
11
16
|
class Input
|
|
12
|
-
prepend Tus::Input::Unicorn
|
|
13
|
-
|
|
14
17
|
def initialize(input, limit: nil)
|
|
15
|
-
@input
|
|
16
|
-
@limit
|
|
17
|
-
@pos
|
|
18
|
+
@input = input
|
|
19
|
+
@limit = limit
|
|
20
|
+
@pos = 0
|
|
21
|
+
@interrupted = false
|
|
22
|
+
@reader = nil
|
|
18
23
|
end
|
|
19
24
|
|
|
20
25
|
def read(length = nil, outbuf = nil)
|
|
21
|
-
|
|
26
|
+
return eof(length, outbuf) if @interrupted
|
|
27
|
+
|
|
28
|
+
begin
|
|
29
|
+
# Fibers can only be resumed by the thread that created them, and Ruby
|
|
30
|
+
# can't tell us a fiber's thread, so we record it.
|
|
31
|
+
@reader = [Thread.current, Fiber.current]
|
|
32
|
+
data = @input.read(*length, *outbuf)
|
|
33
|
+
rescue Tus::Interrupted
|
|
34
|
+
return eof(length, outbuf)
|
|
35
|
+
rescue IOError, Errno::ECONNRESET, Errno::EPIPE
|
|
36
|
+
# The client disconnected or the read timed out (e.g. EOFError and
|
|
37
|
+
# IO::TimeoutError on Falcon), so we save the data received so far.
|
|
38
|
+
@interrupted = true
|
|
39
|
+
return eof(length, outbuf)
|
|
40
|
+
ensure
|
|
41
|
+
@reader = nil
|
|
42
|
+
end
|
|
22
43
|
|
|
23
44
|
@pos += data.bytesize if data
|
|
24
45
|
raise MaxSizeExceeded if @limit && @pos > @limit
|
|
@@ -26,6 +47,21 @@ module Tus
|
|
|
26
47
|
data
|
|
27
48
|
end
|
|
28
49
|
|
|
50
|
+
# Makes further reads return end of input. If a fiber in this thread is
|
|
51
|
+
# waiting for data in #read (e.g. a stalled upload on Falcon), raises into
|
|
52
|
+
# it through the fiber scheduler so that it stops waiting.
|
|
53
|
+
def interrupt!
|
|
54
|
+
@interrupted = true
|
|
55
|
+
|
|
56
|
+
thread, fiber = @reader
|
|
57
|
+
return unless thread == Thread.current
|
|
58
|
+
|
|
59
|
+
scheduler = Fiber.scheduler
|
|
60
|
+
scheduler.raise(fiber, Tus::Interrupted) if scheduler.respond_to?(:raise)
|
|
61
|
+
rescue FiberError
|
|
62
|
+
# the fiber can't be resumed right now; it will see the flag on next read
|
|
63
|
+
end
|
|
64
|
+
|
|
29
65
|
def pos
|
|
30
66
|
@pos
|
|
31
67
|
end
|
|
@@ -38,11 +74,20 @@ module Tus
|
|
|
38
74
|
def rewindable?
|
|
39
75
|
@input.is_a?(Tempfile) || # Puma, Thin
|
|
40
76
|
@input.is_a?(StringIO) || # WEBRick, Puma, Thin
|
|
41
|
-
@input.class.name.end_with?("TeeInput") #
|
|
77
|
+
@input.class.name.end_with?("TeeInput") # Pitchfork, Passenger
|
|
42
78
|
end
|
|
43
79
|
|
|
44
80
|
def close
|
|
45
81
|
# Rack input shouldn't be closed, we just support the interface
|
|
46
82
|
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def eof(length, outbuf)
|
|
87
|
+
outbuf.clear if outbuf
|
|
88
|
+
return nil if length
|
|
89
|
+
|
|
90
|
+
outbuf || String.new
|
|
91
|
+
end
|
|
47
92
|
end
|
|
48
93
|
end
|
data/lib/tus/locker.rb
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
require "tus/errors"
|
|
4
|
+
|
|
5
|
+
require "pathname"
|
|
6
|
+
require "tmpdir"
|
|
7
|
+
|
|
8
|
+
module Tus
|
|
9
|
+
# Locks uploads, so that only one request at a time can read or modify an
|
|
10
|
+
# upload. It uses `flock` on a lock file per upload, which works across
|
|
11
|
+
# threads, fibers and processes on the same host.
|
|
12
|
+
#
|
|
13
|
+
# A request waiting for the lock asks the holder to release it, by calling
|
|
14
|
+
# the holder's `release` callback. Only a holder in the same process can be
|
|
15
|
+
# asked; a holder in another process is waited for until the timeout.
|
|
16
|
+
#
|
|
17
|
+
# Lock files are removed when the lock is released. Use a directory on a
|
|
18
|
+
# local disk, as `flock` is unreliable on network filesystems like NFS.
|
|
19
|
+
class Locker
|
|
20
|
+
MUTEX = Mutex.new
|
|
21
|
+
HOLDERS = {} # uid => release callback of the request holding the lock
|
|
22
|
+
|
|
23
|
+
attr_reader :directory
|
|
24
|
+
|
|
25
|
+
def initialize(directory = File.join(Dir.tmpdir, "tus-locks"), poll_interval: 0.05)
|
|
26
|
+
@directory = Pathname(directory)
|
|
27
|
+
@poll_interval = poll_interval
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Yields while holding the lock for the given upload. Raises Tus::Locked
|
|
31
|
+
# if the lock couldn't be acquired within `timeout` seconds. The `release`
|
|
32
|
+
# callable is invoked when another request wants the lock.
|
|
33
|
+
def lock(uid, timeout:, release: nil)
|
|
34
|
+
file = acquire(uid, timeout)
|
|
35
|
+
|
|
36
|
+
begin
|
|
37
|
+
MUTEX.synchronize { HOLDERS[uid] = release }
|
|
38
|
+
yield
|
|
39
|
+
ensure
|
|
40
|
+
MUTEX.synchronize { HOLDERS.delete(uid) }
|
|
41
|
+
|
|
42
|
+
# Remove the lock file while still holding the lock; waiters that
|
|
43
|
+
# opened it notice the path changed and open it again.
|
|
44
|
+
begin
|
|
45
|
+
lock_path(uid).unlink
|
|
46
|
+
rescue Errno::ENOENT
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
file.close
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def acquire(uid, timeout)
|
|
56
|
+
deadline = now + timeout
|
|
57
|
+
path = lock_path(uid)
|
|
58
|
+
|
|
59
|
+
loop do
|
|
60
|
+
directory.mkpath # each time, as temporary directories get cleaned up
|
|
61
|
+
file = File.open(path, File::RDWR | File::CREAT, 0644)
|
|
62
|
+
|
|
63
|
+
until file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
64
|
+
if now >= deadline
|
|
65
|
+
file.close
|
|
66
|
+
raise Tus::Locked, "upload #{uid} is locked"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
request_release(uid)
|
|
70
|
+
sleep @poll_interval
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
return file if same_file?(path, file)
|
|
74
|
+
|
|
75
|
+
# The holder removed the lock file after we opened it, so our lock
|
|
76
|
+
# is on a deleted file. Try again with the current one.
|
|
77
|
+
file.close
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Asks the holder to release the lock, if it's in this process and hasn't
|
|
82
|
+
# been asked yet.
|
|
83
|
+
def request_release(uid)
|
|
84
|
+
release = MUTEX.synchronize { HOLDERS.delete(uid) }
|
|
85
|
+
|
|
86
|
+
# Called outside the mutex, because it can switch to the holder's fiber,
|
|
87
|
+
# which needs the mutex to unlock.
|
|
88
|
+
release.call if release
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def same_file?(path, file)
|
|
92
|
+
current = File.stat(path)
|
|
93
|
+
locked = file.stat
|
|
94
|
+
|
|
95
|
+
current.dev == locked.dev && current.ino == locked.ino
|
|
96
|
+
rescue Errno::ENOENT
|
|
97
|
+
false
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def lock_path(uid)
|
|
101
|
+
directory.join("#{uid}.lock")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def now
|
|
105
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
data/lib/tus/response.rb
CHANGED