Class: ImageCache
- Inherits:
-
Object
- Object
- ImageCache
- 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
-
#basename ⇒ Object
Returns the value of attribute basename.
-
#cache_key ⇒ Object
Returns the value of attribute cache_key.
-
#dirname ⇒ Object
Returns the value of attribute dirname.
-
#io ⇒ Object
Returns the value of attribute io.
-
#key ⇒ Object
Returns the value of attribute key.
-
#path ⇒ Object
Returns the value of attribute path.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
- .base ⇒ Object
- .base=(base) ⇒ Object
- .cache_key_for(key) ⇒ Object
- .cleanname(path) ⇒ Object
- .clear!(options = {}) ⇒ Object
- .finalizer(object_id) ⇒ Object
- .for(params, key = :image) ⇒ Object
- .root ⇒ Object
- .tmpdir(&block) ⇒ Object
- .url ⇒ Object
- .uuid(*args) ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
- #clear! ⇒ Object
- #hidden ⇒ Object
-
#initialize(key, path) ⇒ ImageCache
constructor
A new instance of ImageCache.
- #raw(*args) ⇒ Object
- #to_s ⇒ Object
- #url ⇒ Object
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
#basename ⇒ Object
Returns the value of attribute basename.
125 126 127 |
# File 'lib/image_cache.rb', line 125 def basename @basename end |
#cache_key ⇒ Object
Returns the value of attribute cache_key.
122 123 124 |
# File 'lib/image_cache.rb', line 122 def cache_key @cache_key end |
#dirname ⇒ Object
Returns the value of attribute dirname.
124 125 126 |
# File 'lib/image_cache.rb', line 124 def dirname @dirname end |
#io ⇒ Object
Returns the value of attribute io.
127 128 129 |
# File 'lib/image_cache.rb', line 127 def io @io end |
#key ⇒ Object
Returns the value of attribute key.
121 122 123 |
# File 'lib/image_cache.rb', line 121 def key @key end |
#path ⇒ Object
Returns the value of attribute path.
123 124 125 |
# File 'lib/image_cache.rb', line 123 def path @path end |
#value ⇒ Object
Returns the value of attribute value.
126 127 128 |
# File 'lib/image_cache.rb', line 126 def value @value end |
Class Method Details
.base ⇒ Object
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!( = {}) glob = File.join(root, '*') age = Integer([:age] || ['age'] || Age) since = [:since] || ['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 |
.root ⇒ Object
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 |
.url ⇒ Object
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..to_s end |
.version ⇒ Object
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 |
#hidden ⇒ Object
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_s ⇒ Object
152 153 154 |
# File 'lib/image_cache.rb', line 152 def to_s hidden.to_s end |
#url ⇒ Object
156 157 158 |
# File 'lib/image_cache.rb', line 156 def url File.join(ImageCache.url, @value) if @value end |