Class: Tus::Storage::Gridfs

Inherits:
Object
  • Object
show all
Defined in:
lib/tus/storage/gridfs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, prefix: "fs") ⇒ Gridfs

Returns a new instance of Gridfs.



10
11
12
13
14
15
# File 'lib/tus/storage/gridfs.rb', line 10

def initialize(client:, prefix: "fs")
  @client = client
  @prefix = prefix
  @bucket = @client.database.fs(bucket_name: @prefix)
  @bucket.send(:ensure_indexes!)
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



8
9
10
# File 'lib/tus/storage/gridfs.rb', line 8

def bucket
  @bucket
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/tus/storage/gridfs.rb', line 8

def client
  @client
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/tus/storage/gridfs.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#create_file(uid, metadata = {}) ⇒ Object



17
18
19
20
# File 'lib/tus/storage/gridfs.rb', line 17

def create_file(uid,  = {})
  file = Mongo::Grid::File.new("", filename: uid, metadata: )
  bucket.insert_one(file)
end

#delete_file(uid) ⇒ Object



47
48
49
50
# File 'lib/tus/storage/gridfs.rb', line 47

def delete_file(uid)
  file_info = bucket.files_collection.find(filename: uid).first
  bucket.delete(file_info.fetch("_id")) if file_info
end

#download_file(uid) ⇒ Object



40
41
42
43
44
45
# File 'lib/tus/storage/gridfs.rb', line 40

def download_file(uid)
  tempfile = Tempfile.new("tus", binmode: true)
  tempfile.sync = true
  bucket.download_to_stream_by_name(uid, tempfile)
  tempfile.path
end

#file_exists?(uid) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/tus/storage/gridfs.rb', line 22

def file_exists?(uid)
  !!bucket.files_collection.find(filename: uid).first
end

#list_filesObject



61
62
63
64
# File 'lib/tus/storage/gridfs.rb', line 61

def list_files
  infos = bucket.files_collection.find.to_a
  infos.map { |info| info.fetch("filename") }
end

#patch_file(uid, content) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/tus/storage/gridfs.rb', line 31

def patch_file(uid, content)
  file_info = bucket.files_collection.find(filename: uid).first
  file_info["md5"] = Digest::MD5.new # hack around not able to update digest
  file_info = Mongo::Grid::File::Info.new(file_info)
  offset = bucket.chunks_collection.find(files_id: file_info.id).count
  chunks = Mongo::Grid::File::Chunk.split(content, file_info, offset)
  bucket.chunks_collection.insert_many(chunks)
end

#read_file(uid) ⇒ Object



26
27
28
29
# File 'lib/tus/storage/gridfs.rb', line 26

def read_file(uid)
  file = bucket.find_one(filename: uid)
  file.data
end

#read_info(uid) ⇒ Object



52
53
54
55
# File 'lib/tus/storage/gridfs.rb', line 52

def read_info(uid)
  info = bucket.files_collection.find(filename: uid).first
  info.fetch("metadata")
end

#update_info(uid, info) ⇒ Object



57
58
59
# File 'lib/tus/storage/gridfs.rb', line 57

def update_info(uid, info)
  bucket.files_collection.find(filename: uid).update_one("$set" => {metadata: info})
end