Class: Tus::Storage::Filesystem
- Inherits:
-
Object
- Object
- Tus::Storage::Filesystem
- Defined in:
- lib/tus/storage/filesystem.rb
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
Instance Method Summary collapse
- #concatenate(uid, part_uids, info = {}) ⇒ Object
- #create_file(uid, info = {}) ⇒ Object
- #delete_file(uid, info = {}) ⇒ Object
- #expire_files(expiration_date) ⇒ Object
- #get_file(uid, info = {}, range: nil) ⇒ Object
-
#initialize(directory) ⇒ Filesystem
constructor
A new instance of Filesystem.
- #patch_file(uid, io, info = {}) ⇒ Object
- #read_info(uid) ⇒ Object
- #update_info(uid, info) ⇒ Object
Constructor Details
#initialize(directory) ⇒ Filesystem
Returns a new instance of Filesystem.
12 13 14 15 16 |
# File 'lib/tus/storage/filesystem.rb', line 12 def initialize(directory) @directory = Pathname(directory) create_directory! unless @directory.exist? end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
10 11 12 |
# File 'lib/tus/storage/filesystem.rb', line 10 def directory @directory end |
Instance Method Details
#concatenate(uid, part_uids, info = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tus/storage/filesystem.rb', line 22 def concatenate(uid, part_uids, info = {}) file_path(uid).open("wb") do |file| begin part_uids.each do |part_uid| IO.copy_stream(file_path(part_uid), file) end rescue Errno::ENOENT raise Tus::Error, "some parts for concatenation are missing" end end delete(part_uids) # server requires us to return the size of the concatenated file file_path(uid).size end |
#create_file(uid, info = {}) ⇒ Object
18 19 20 |
# File 'lib/tus/storage/filesystem.rb', line 18 def create_file(uid, info = {}) file_path(uid).open("wb") { |file| file.write("") } end |
#delete_file(uid, info = {}) ⇒ Object
84 85 86 |
# File 'lib/tus/storage/filesystem.rb', line 84 def delete_file(uid, info = {}) delete([uid]) end |
#expire_files(expiration_date) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/tus/storage/filesystem.rb', line 88 def expire_files(expiration_date) uids = [] Pathname.glob(directory.join("*.file")).each do |pathname| uids << pathname.basename(".*") if pathname.mtime <= expiration_date end delete(uids) end |
#get_file(uid, info = {}, range: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/tus/storage/filesystem.rb', line 61 def get_file(uid, info = {}, range: nil) raise Tus::NotFound if !file_path(uid).exist? file = file_path(uid).open("rb") range ||= 0..file.size-1 chunks = Enumerator.new do |yielder| file.seek(range.begin) remaining_length = range.end - range.begin + 1 buffer = "" while remaining_length > 0 chunk = file.read([16*1024, remaining_length].min, buffer) break unless chunk remaining_length -= chunk.length yielder << chunk end end Response.new(chunks: chunks, close: ->{file.close}) end |
#patch_file(uid, io, info = {}) ⇒ Object
39 40 41 42 43 |
# File 'lib/tus/storage/filesystem.rb', line 39 def patch_file(uid, io, info = {}) raise Tus::NotFound if !file_path(uid).exist? file_path(uid).open("ab") { |file| IO.copy_stream(io, file) } end |
#read_info(uid) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tus/storage/filesystem.rb', line 45 def read_info(uid) raise Tus::NotFound if !file_path(uid).exist? begin data = info_path(uid).binread rescue Errno::ENOENT data = "{}" end JSON.parse(data) end |
#update_info(uid, info) ⇒ Object
57 58 59 |
# File 'lib/tus/storage/filesystem.rb', line 57 def update_info(uid, info) info_path(uid).open("wb") { |file| file.write(info.to_json) } end |