Class: LaserBlob::Storage::Filesystem
- Inherits:
-
Object
- Object
- LaserBlob::Storage::Filesystem
- Defined in:
- lib/laserblob/storage/filesystem.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #copy_to_tempfile(id, basename: nil, &block) ⇒ Object
- #delete(id) ⇒ Object
- #exists?(id) ⇒ Boolean
-
#initialize(config = {}) ⇒ Filesystem
constructor
A new instance of Filesystem.
- #local? ⇒ Boolean
- #read(id) ⇒ Object
- #url(id, **options) ⇒ Object
- #write(id, file, options = {}) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Filesystem
Returns a new instance of Filesystem.
8 9 10 11 |
# File 'lib/laserblob/storage/filesystem.rb', line 8 def initialize(config = {}) @path = config[:path] || Rails.root.join('storage', 'blobs') FileUtils.mkdir_p(@path) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/laserblob/storage/filesystem.rb', line 6 def path @path end |
Instance Method Details
#copy_to_tempfile(id, basename: nil, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/laserblob/storage/filesystem.rb', line 39 def copy_to_tempfile(id, basename: nil, &block) basename ||= ['blob', ''] source_path = path_for(id) Tempfile.create(basename, binmode: true) do |tmpfile| FileUtils.cp(source_path, tmpfile.path) tmpfile.rewind block.call(tmpfile) end end |
#delete(id) ⇒ Object
27 28 29 |
# File 'lib/laserblob/storage/filesystem.rb', line 27 def delete(id) FileUtils.rm_f(path_for(id)) end |
#exists?(id) ⇒ Boolean
31 32 33 |
# File 'lib/laserblob/storage/filesystem.rb', line 31 def exists?(id) File.exist?(path_for(id)) end |
#local? ⇒ Boolean
13 14 15 |
# File 'lib/laserblob/storage/filesystem.rb', line 13 def local? true end |
#read(id) ⇒ Object
23 24 25 |
# File 'lib/laserblob/storage/filesystem.rb', line 23 def read(id) File.read(path_for(id)) end |
#url(id, **options) ⇒ Object
35 36 37 |
# File 'lib/laserblob/storage/filesystem.rb', line 35 def url(id, **) "/blobs/#{id}/download" end |
#write(id, file, options = {}) ⇒ Object
17 18 19 20 21 |
# File 'lib/laserblob/storage/filesystem.rb', line 17 def write(id, file, = {}) file_path = path_for(id) FileUtils.mkdir_p(File.dirname(file_path)) FileUtils.cp(file.path, file_path) end |