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/thumbo'
  @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



32
33
34
35
36
37
38
39
40
41
# File 'lib/thumbo/storages/filesystem.rb', line 32

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)


53
54
55
56
# File 'lib/thumbo/storages/filesystem.rb', line 53

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

#paths(filename) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/thumbo/storages/filesystem.rb', line 43

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

  else
    raise_file_not_found(filename)

  end
end

#prefix(filename) ⇒ Object



58
59
60
# File 'lib/thumbo/storages/filesystem.rb', line 58

def prefix filename
  Digest::MD5.hexdigest(filename)[0, prefix_size]
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
# File 'lib/thumbo/storages/filesystem.rb', line 22

def write filename, blob
  target = ensure_path(filename)
  (File.open(target, 'w') << blob).close
end

#write_file(filename, file) ⇒ Object



27
28
29
30
# File 'lib/thumbo/storages/filesystem.rb', line 27

def write_file filename, file
  target = ensure_path(filename)
  FileUtils.cp(file.path, target)
end