Class: ImageCache

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



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

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.



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

def basename
  @basename
end

#cache_keyObject

Returns the value of attribute cache_key.



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

def cache_key
  @cache_key
end

#dirnameObject

Returns the value of attribute dirname.



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

def dirname
  @dirname
end

#ioObject

Returns the value of attribute io.



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

def io
  @io
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.baseObject



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

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

.base=(base) ⇒ Object



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

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

.cache_key_for(key) ⇒ Object



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

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

.cleanname(path) ⇒ Object



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

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

.clear!(options = {}) ⇒ Object



90
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
# File 'lib/image_cache.rb', line 90

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



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

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



53
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
# File 'lib/image_cache.rb', line 53

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



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

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

.tmpdir(&block) ⇒ Object



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

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

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

.urlObject



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

def url
  '/' + base
end

.uuid(*args) ⇒ Object



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

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

.versionObject



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

def version
  ImageCache::Version
end

Instance Method Details

#clear!Object



169
170
171
172
173
174
175
176
177
# File 'lib/image_cache.rb', line 169

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

#hiddenObject



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

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

#raw(*args) ⇒ Object



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

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

#to_sObject



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

def to_s
  hidden.to_s
end

#urlObject



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

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