Class: Shrine::Storage::Gridfs
- Inherits:
-
Object
- Object
- Shrine::Storage::Gridfs
- Defined in:
- lib/shrine/storage/gridfs.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#chunk_size ⇒ Object
readonly
Returns the value of attribute chunk_size.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #clear! ⇒ Object
- #delete(id) ⇒ Object
- #exists?(id) ⇒ Boolean
-
#initialize(client:, prefix: "fs", chunk_size: 256*1024, batch_size: 5 * 1024*1024, **options) ⇒ Gridfs
constructor
A new instance of Gridfs.
- #open(id, rewindable: true) ⇒ Object
- #upload(io, id, shrine_metadata: {}) ⇒ Object
- #url(id) ⇒ Object
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, **) @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
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
9 10 11 |
# File 'lib/shrine/storage/gridfs.rb', line 9 def bucket @bucket end |
#chunk_size ⇒ Object (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 |
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/shrine/storage/gridfs.rb', line 9 def client @client end |
#prefix ⇒ Object (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
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 |