Class: RefileCache::Cache::Logic
- Inherits:
-
Object
- Object
- RefileCache::Cache::Logic
- Defined in:
- lib/refile_cache/cache.rb
Overview
This is for autoreloading to be working :D
Constant Summary collapse
- URL_PATH =
/\A#{Refile.mount_point}\/.*\/store\/(fill|fit|limit|pad|convert)\//- URL_PROCESSOR =
/\A#{Refile.mount_point}\/(?<token>.*)\/(?<backend>.*)\/(?<processor>(fill|fit|limit|pad|convert))\/(?<splat>.*)\/(?<id>.*)\/(?<file_base>.*)\.(?<extension>.*)/
Instance Method Summary collapse
- #backend ⇒ Object
- #call(env) ⇒ Object
- #get_params(env) ⇒ Object
-
#initialize(app) ⇒ Logic
constructor
A new instance of Logic.
- #own_headers(filename, content_length) ⇒ Object
- #stream_file(env, file) ⇒ Object
- #valid_token?(env) ⇒ Boolean
Constructor Details
#initialize(app) ⇒ Logic
Returns a new instance of Logic.
17 18 19 |
# File 'lib/refile_cache/cache.rb', line 17 def initialize(app) @app = app end |
Instance Method Details
#backend ⇒ Object
58 59 60 61 62 63 |
# File 'lib/refile_cache/cache.rb', line 58 def backend Refile.backends.fetch('image_cache') do |name| log_error("Could not find backend: #{name}") halt 404 end end |
#call(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/refile_cache/cache.rb', line 21 def call(env) # do not process other endpoints return @app.call(env) if env['PATH_INFO'] !~ URL_PATH # do not process invalid token return @app.call(env) unless valid_token?(env) params = get_params(env) cache_key = "cache#{params[:id]}#{params[:processor]}#{params[:splat].delete('/')}" if backend.exists?(cache_key) file = backend.get(cache_key) filename = "#{params[:file_base]}.#{params[:extension]}" return [200, own_headers(filename, file.size), stream_file(env, file)] end status, headers, response = @app.call(env) # cache only existing images if status == 200 image = RefileCache::FileDouble.new(File.open(response.path).read, cache_key, content_type: headers['Content-Type']) backend.upload(image) end [status, headers, response] end |
#get_params(env) ⇒ Object
54 55 56 |
# File 'lib/refile_cache/cache.rb', line 54 def get_params(env) env['PATH_INFO'].match(URL_PROCESSOR) end |
#own_headers(filename, content_length) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/refile_cache/cache.rb', line 65 def own_headers(filename, content_length) { 'Content-Type' => 'image/jpeg', 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Headers' => '', 'Access-Control-Allow-Method' => '', 'Cache-Control' => 'public, max-age=31536000', 'Expires' => (Time.now + 1.year).to_s, 'Content-Disposition' => "inline; filename=\"#{filename}\"", 'Last-Modified' => (Time.now - 1.month).to_s, 'Content-Length' => content_length.to_s, 'X-Content-Type-Options' => 'nosniff' } end |
#stream_file(env, file) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/refile_cache/cache.rb', line 80 def stream_file(env, file) if file.respond_to?(:path) path = file.path else path = Dir::Tmpname.create(get_params(env)[:id]) {} IO.copy_stream file, path end File.open(path) end |
#valid_token?(env) ⇒ Boolean
47 48 49 50 51 52 |
# File 'lib/refile_cache/cache.rb', line 47 def valid_token?(env) token = get_params(env)[:token] base_path = env['PATH_INFO'].gsub(::File.join(Refile.mount_point, token), '') Refile.valid_token?(base_path, token) end |