Class: LaserBlob::Storage::Filesystem

Inherits:
Object
  • Object
show all
Defined in:
lib/laserblob/storage/filesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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

Returns:

  • (Boolean)


31
32
33
# File 'lib/laserblob/storage/filesystem.rb', line 31

def exists?(id)
  File.exist?(path_for(id))
end

#local?Boolean

Returns:

  • (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, **options)
  "/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, options = {})
  file_path = path_for(id)
  FileUtils.mkdir_p(File.dirname(file_path))
  FileUtils.cp(file.path, file_path)
end