Module: FileCache

Defined in:
lib/rbbt/util/filecache.rb

Overview

Provides caching functionality for files downloaded from the internet

Defined Under Namespace

Classes: BadPathError, FileExistsError

Class Method Summary collapse

Class Method Details

.add_file(filename, content, options = {}) ⇒ Object

Add a file in the cache. Raise exception if exists, unless force is used.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rbbt/util/filecache.rb', line 41

def self.add_file(filename, content, options = {})
  sanity_check(filename)

  path = path(filename)
  FileUtils.makedirs(File.dirname(path), :mode => 0777)

  if File.exist?(path) and ! (options[:force] || options['force'])
    raise FileCache::FileExistsError, "File #{filename} already in cache"
  end

  File.open(path,'w'){|f|
    f.write(content)
  }
  FileUtils.chmod 0666, path

  nil
end

.del_file(filename) ⇒ Object

Removes the file from cache



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rbbt/util/filecache.rb', line 60

def self.del_file(filename)
  sanity_check(filename)

  path = path(filename)

  if File.exist? path
    FileUtils.rm path
  end

  nil
end

.path(filename) ⇒ Object

Find the path that a particular file would have in the cache



30
31
32
33
34
35
36
37
# File 'lib/rbbt/util/filecache.rb', line 30

def self.path(filename)
  sanity_check(filename)

  name, extension = filename.match(/(.+)\.(.+)/).values_at(1,2)
  dirs = name.scan(/./).reverse.values_at(0,1,2,3,4).reverse.compact.join('/')
                                          
  return File.join(File.join(Rbbt.cachedir,dirs),filename)
end