Class: Minimage::Storage::File

Inherits:
Base
  • Object
show all
Defined in:
lib/minimage/storage/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#configure

Instance Attribute Details

#root_pathObject

Returns the value of attribute root_path.



7
8
9
# File 'lib/minimage/storage/file.rb', line 7

def root_path
  @root_path
end

Instance Method Details

#fetch!(uid) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/minimage/storage/file.rb', line 24

def fetch!(uid)
  matchdata = uid.match(/^(\d+)_\d{5}$/)
  raise Minimage::Storage::NotFound.new unless matchdata

  atime = Time.at(matchdata.captures.shift.to_i)
  path  = ::File.expand_path(::File.join(root_path, atime.strftime('%Y/%m/%d/%H/%M'), uid))
  ::File.open(path, "rb") { |f| f.read }
rescue Errno::ENOENT => e
  raise Minimage::Storage::NotFound.new
end

#store!(pathname) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/minimage/storage/file.rb', line 9

def store!(pathname)
  atime = pathname.mtime
  rnd   = '%05d' % (rand(99999) + 1)
  uid   = "#{atime.to_i}_#{rnd}"

  path     = pathname.to_path
  new_path = ::File.expand_path(::File.join(root_path, atime.strftime('%Y/%m/%d/%H/%M'), uid))

  FileUtils.mkdir_p(::File.dirname(new_path)) unless ::File.exists?(::File.dirname(new_path))
  FileUtils.copy(path, new_path) unless new_path == path
  uid
rescue Errno::EACCES => e
  raise Minimage::Storage::Error.new
end