Class: Shrine::Storage::Gridfs

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, prefix: "fs", chunk_size: 256*1024, batch_size: 5 * 1024*1024, **options) ⇒ Gridfs

Returns a new instance of Gridfs.



11
12
13
14
15
16
17
18
19
# File 'lib/shrine/storage/gridfs.rb', line 11

def initialize(client:, prefix: "fs", chunk_size: 256*1024, batch_size: 5 * 1024*1024, **options)
  @client     = client
  @prefix     = prefix
  @chunk_size = chunk_size
  @batch_size = batch_size
  @bucket     = @client.database.fs(bucket_name: @prefix)

  @bucket.send(:ensure_indexes!)
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



9
10
11
# File 'lib/shrine/storage/gridfs.rb', line 9

def bucket
  @bucket
end

#chunk_sizeObject (readonly)

Returns the value of attribute chunk_size.



9
10
11
# File 'lib/shrine/storage/gridfs.rb', line 9

def chunk_size
  @chunk_size
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/shrine/storage/gridfs.rb', line 9

def client
  @client
end

#prefixObject (readonly)

Returns the value of attribute prefix.



9
10
11
# File 'lib/shrine/storage/gridfs.rb', line 9

def prefix
  @prefix
end

Instance Method Details

#clear!Object



53
54
55
56
# File 'lib/shrine/storage/gridfs.rb', line 53

def clear!
  files_collection.find.delete_many
  chunks_collection.find.delete_many
end

#delete(id) ⇒ Object



45
46
47
48
# File 'lib/shrine/storage/gridfs.rb', line 45

def delete(id)
  bucket.delete(bson_id(id))
rescue Mongo::Error::FileNotFound
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/shrine/storage/gridfs.rb', line 41

def exists?(id)
  !!file_info(id)
end

#open(id, rewindable: true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shrine/storage/gridfs.rb', line 29

def open(id, rewindable: true, **)
  info   = file_info(id) or raise Shrine::FileNotFound, "file #{id.inspect} not found on storage"
  stream = bucket.open_download_stream(bson_id(id))

  Down::ChunkedIO.new(
    size:       info[:length],
    chunks:     stream.enum_for(:each),
    on_close:   -> { stream.close },
    rewindable: rewindable,
  )
end

#upload(io, id, shrine_metadata: {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/shrine/storage/gridfs.rb', line 21

def upload(io, id, shrine_metadata: {}, **)
  if copyable?(io, id)
    copy(io, id, shrine_metadata: )
  else
    create(io, id, shrine_metadata: )
  end
end

#url(id) ⇒ Object



50
51
# File 'lib/shrine/storage/gridfs.rb', line 50

def url(id, **)
end