Class: Thumbo::Filesystem

Inherits:
AbstractStorage show all
Defined in:
lib/thumbo/storages/filesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Filesystem

Returns a new instance of Filesystem.



9
10
11
12
# File 'lib/thumbo/storages/filesystem.rb', line 9

def initialize opts = {}
  @path = opts[:path] || 'public/images'
  @prefix_size = opts[:prefix_size] || 1
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/thumbo/storages/filesystem.rb', line 8

def path
  @path
end

#prefix_sizeObject

Returns the value of attribute prefix_size.



8
9
10
# File 'lib/thumbo/storages/filesystem.rb', line 8

def prefix_size
  @prefix_size
end

Instance Method Details

#delete(filename) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/thumbo/storages/filesystem.rb', line 28

def delete filename
  target = calculate_path(filename)
  if File.exist?(target)
    File.delete(target)

  else
    raise_file_not_found(filename)

  end
end

#exist?(filename) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/thumbo/storages/filesystem.rb', line 49

def exist? filename
  target = calculate_path(filename)
  File.exist?(target) ? target : false
end

#paths(filename) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/thumbo/storages/filesystem.rb', line 39

def paths filename
  if target = exist?(filename)
    [target]

  else
    raise_file_not_found(filename)

  end
end

#read(filename) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/thumbo/storages/filesystem.rb', line 14

def read filename
  File.read(calculate_path(filename))

rescue Errno::ENOENT
  raise_file_not_found(filename)

end

#write(filename, blob) ⇒ Object



22
23
24
25
26
# File 'lib/thumbo/storages/filesystem.rb', line 22

def write filename, blob
  target = calculate_path(filename)
  FileUtils.mkdir_p(target.split('/')[0..-2].join('/'))
  (File.open(target, 'w') << blob).close
end