Class: Dao::ImageCache

Inherits:
Object
  • Object
show all
Defined in:
lib/dao/image_cache.rb

Constant Summary collapse

Version =
'1.0.0'
UUIDPattern =
%r/^[a-zA-Z0-9-]+$/io
Age =
60 * 60 * 24
IOs =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, path) ⇒ ImageCache

Returns a new instance of ImageCache.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dao/image_cache.rb', line 132

def initialize(key, path)
  @key = key.to_s
  @cache_key = ImageCache.cache_key_for(key)

  if path
    @path = path
    @dirname, @basename = File.split(@path)
    @value = File.join(File.basename(@dirname), @basename).strip
    @io = open(@path)
    IOs[object_id] = @io.fileno
    ObjectSpace.define_finalizer(self, ImageCache.method(:finalizer).to_proc)
  else
    @path = nil
    @value = nil
  end
end

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



126
127
128
# File 'lib/dao/image_cache.rb', line 126

def basename
  @basename
end

#cache_keyObject

Returns the value of attribute cache_key.



123
124
125
# File 'lib/dao/image_cache.rb', line 123

def cache_key
  @cache_key
end

#dirnameObject

Returns the value of attribute dirname.



125
126
127
# File 'lib/dao/image_cache.rb', line 125

def dirname
  @dirname
end

#ioObject

Returns the value of attribute io.



128
129
130
# File 'lib/dao/image_cache.rb', line 128

def io
  @io
end

#keyObject

Returns the value of attribute key.



122
123
124
# File 'lib/dao/image_cache.rb', line 122

def key
  @key
end

#pathObject

Returns the value of attribute path.



124
125
126
# File 'lib/dao/image_cache.rb', line 124

def path
  @path
end

#valueObject

Returns the value of attribute value.



127
128
129
# File 'lib/dao/image_cache.rb', line 127

def value
  @value
end

Class Method Details

.baseObject



14
15
16
# File 'lib/dao/image_cache.rb', line 14

def base
  @base ||= 'images/cache'
end

.base=(base) ⇒ Object



18
19
20
# File 'lib/dao/image_cache.rb', line 18

def base=(base)
  @base = base.to_s.sub(%r|^/+|, '')
end

.cache_key_for(key) ⇒ Object



50
51
52
# File 'lib/dao/image_cache.rb', line 50

def cache_key_for(key)
  "#{ key }__cache"
end

.cleanname(path) ⇒ Object



45
46
47
48
# File 'lib/dao/image_cache.rb', line 45

def cleanname(path)
  basename = File.basename(path.to_s)
  CGI.unescape(basename).gsub(%r/[^0-9a-zA-Z_@)(~.-]/, '_').gsub(%r/_+/,'_')
end

.clear!(options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dao/image_cache.rb', line 91

def clear!(options = {})
  glob = File.join(root, '*')
  age = Integer(options[:age] || options['age'] || Age)
  since = options[:since] || options['since'] || Time.now

  Dir.glob(glob) do |entry|
    begin
      next unless test(?d, entry)
      next unless File.basename(entry) =~ UUIDPattern

      files = Dir.glob(File.join(entry, '**/**'))

      all_files_are_old =
        files.all? do |file|
          begin
            stat = File.stat(file)
            age = since - stat.atime
            age >= Age
          rescue
            false
          end
        end

      FileUtils.rm_rf(entry) if all_files_are_old
    rescue
      next
    end
  end
end

.finalizer(object_id) ⇒ Object



81
82
83
84
85
86
# File 'lib/dao/image_cache.rb', line 81

def finalizer(object_id)
  if fd = IOs[object_id]
    IO.for_fd(fd).close
    IOs.delete(object_id)
  end
end

.for(params, key = :image) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dao/image_cache.rb', line 54

def for(params, key = :image)
  image = params[key]
  if image.respond_to?(:read)
    tmpdir do |tmp|
      basename = cleanname(image.original_path)

      path = File.join(tmp, basename)
      open(path, 'w'){|fd| fd.write(image.read)}
      image_cache = new(key, path)
      params[key] = image_cache.io
      return image_cache
    end
  end

  cache_key = cache_key_for(key)
  image_cache = params[cache_key]
  if image_cache
    dirname, basename = File.split(image_cache)
    path = root + '/' + File.join(File.basename(dirname), basename)
    image_cache = new(key, path)
    params[key] = image_cache.io
    return image_cache
  end

  return new(key, path=nil)
end

.rootObject



26
27
28
# File 'lib/dao/image_cache.rb', line 26

def root
  @root ||= File.join(Rails.root, 'public', base)
end

.tmpdir(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/dao/image_cache.rb', line 34

def tmpdir(&block)
  tmpdir = File.join(root, uuid)

  if block
    FileUtils.mkdir_p(tmpdir)
    block.call(tmpdir)
  else
    tmpdir
  end
end

.urlObject



22
23
24
# File 'lib/dao/image_cache.rb', line 22

def url
  '/' + base
end

.uuid(*args) ⇒ Object



30
31
32
# File 'lib/dao/image_cache.rb', line 30

def uuid(*args)
  UUIDTools::UUID.timestamp_create.to_s
end

.versionObject



10
11
12
# File 'lib/dao/image_cache.rb', line 10

def version
  ImageCache::Version
end

Instance Method Details

#clear!Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dao/image_cache.rb', line 170

def clear!
  FileUtils.rm_rf(@dirname) if test(?d, @dirname)
rescue
  nil
ensure
  if @io
    @io.close
    IOs.delete(object_id)
  end
  Thread.new{ ImageCache.clear! }
end

#hiddenObject



149
150
151
# File 'lib/dao/image_cache.rb', line 149

def hidden
  raw("<input type='hidden' name='#{ @cache_key }' value='#{ @value }' />") if @value
end

#raw(*args) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/dao/image_cache.rb', line 161

def raw(*args)
  string = args.join
  if string.respond_to?(:html_safe)
    string.html_safe
  else
    string
  end
end

#to_sObject



153
154
155
# File 'lib/dao/image_cache.rb', line 153

def to_s
  hidden.to_s
end

#urlObject



157
158
159
# File 'lib/dao/image_cache.rb', line 157

def url
  File.join(ImageCache.url, @value) if @value
end