rails_imager 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/javascripts/rails_imager/application.js +13 -0
  5. data/app/assets/javascripts/rails_imager/images.js +2 -0
  6. data/app/assets/stylesheets/rails_imager/application.css +13 -0
  7. data/app/assets/stylesheets/rails_imager/images.css +4 -0
  8. data/app/controllers/rails_imager/application_controller.rb +4 -0
  9. data/app/controllers/rails_imager/images_controller.rb +33 -0
  10. data/app/helpers/rails_imager/application_helper.rb +4 -0
  11. data/app/helpers/rails_imager/images_helper.rb +26 -0
  12. data/app/views/layouts/rails_imager/application.html.erb +14 -0
  13. data/app/views/rails_imager/images/show.html.erb +2 -0
  14. data/config/routes.rb +3 -0
  15. data/lib/rails_imager.rb +17 -0
  16. data/lib/rails_imager/engine.rb +5 -0
  17. data/lib/rails_imager/image_handler.rb +241 -0
  18. data/lib/rails_imager/version.rb +3 -0
  19. data/lib/tasks/rails_imager_tasks.rake +4 -0
  20. data/test/controllers/rails_imager/images_controller_test.rb +28 -0
  21. data/test/dummy/README.rdoc +28 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/javascripts/application.js +13 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  25. data/test/dummy/app/controllers/application_controller.rb +5 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/test/dummy/bin/bundle +3 -0
  29. data/test/dummy/bin/rails +4 -0
  30. data/test/dummy/bin/rake +4 -0
  31. data/test/dummy/config.ru +4 -0
  32. data/test/dummy/config/application.rb +23 -0
  33. data/test/dummy/config/boot.rb +5 -0
  34. data/test/dummy/config/database.yml +25 -0
  35. data/test/dummy/config/environment.rb +5 -0
  36. data/test/dummy/config/environments/development.rb +29 -0
  37. data/test/dummy/config/environments/production.rb +80 -0
  38. data/test/dummy/config/environments/test.rb +36 -0
  39. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/test/dummy/config/initializers/inflections.rb +16 -0
  42. data/test/dummy/config/initializers/mime_types.rb +5 -0
  43. data/test/dummy/config/initializers/secret_token.rb +12 -0
  44. data/test/dummy/config/initializers/session_store.rb +3 -0
  45. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/test/dummy/config/locales/en.yml +23 -0
  47. data/test/dummy/config/routes.rb +3 -0
  48. data/test/dummy/db/development.sqlite3 +0 -0
  49. data/test/dummy/db/test.sqlite3 +0 -0
  50. data/test/dummy/log/development.log +1367 -0
  51. data/test/dummy/log/test.log +3496 -0
  52. data/test/dummy/public/404.html +58 -0
  53. data/test/dummy/public/422.html +58 -0
  54. data/test/dummy/public/500.html +57 -0
  55. data/test/dummy/public/favicon.ico +0 -0
  56. data/test/dummy/public/test.png +0 -0
  57. data/test/dummy/tmp/cache/assets/development/sprockets/0664a19cc0f5d977f80bafcefe17e9c9 +0 -0
  58. data/test/dummy/tmp/cache/assets/development/sprockets/0a9ac96e2e41642bb5a979bc1bba099d +0 -0
  59. data/test/dummy/tmp/cache/assets/development/sprockets/2ca25a1b4dbb5046e79cdc5c2b0e6d55 +0 -0
  60. data/test/dummy/tmp/cache/assets/development/sprockets/510b6fd97773642f0b777a14f0ef5366 +0 -0
  61. data/test/dummy/tmp/cache/assets/development/sprockets/9e801b0fd4666d3a5cb45345d7de21a7 +0 -0
  62. data/test/dummy/tmp/cache/assets/development/sprockets/b40bea70cdbfe1b3b11ede81a1d20002 +0 -0
  63. data/test/dummy/tmp/cache/assets/development/sprockets/be7b64afb60161c417c6465682b304ac +0 -0
  64. data/test/dummy/tmp/cache/assets/development/sprockets/c0dbbd2455c6e7638bd41c5f003dff53 +0 -0
  65. data/test/dummy/tmp/cache/assets/development/sprockets/cc85e49d1f8af3e2bb0073f666f413be +0 -0
  66. data/test/dummy/tmp/cache/assets/development/sprockets/e31424e52369b0afcae5d0350c19ae82 +0 -0
  67. data/test/dummy/tmp/cache/assets/development/sprockets/f202557856e13ca5921856c87d402bd4 +0 -0
  68. data/test/dummy/tmp/cache/assets/development/sprockets/fe7c9c32074445201dccf284f19078bf +0 -0
  69. data/test/dummy/tmp/cache/assets/test/sprockets/0a9ac96e2e41642bb5a979bc1bba099d +0 -0
  70. data/test/dummy/tmp/cache/assets/test/sprockets/2ca25a1b4dbb5046e79cdc5c2b0e6d55 +0 -0
  71. data/test/dummy/tmp/cache/assets/test/sprockets/510b6fd97773642f0b777a14f0ef5366 +0 -0
  72. data/test/dummy/tmp/cache/assets/test/sprockets/9e801b0fd4666d3a5cb45345d7de21a7 +0 -0
  73. data/test/dummy/tmp/cache/assets/test/sprockets/b40bea70cdbfe1b3b11ede81a1d20002 +0 -0
  74. data/test/dummy/tmp/cache/assets/test/sprockets/be7b64afb60161c417c6465682b304ac +0 -0
  75. data/test/dummy/tmp/cache/assets/test/sprockets/c0dbbd2455c6e7638bd41c5f003dff53 +0 -0
  76. data/test/dummy/tmp/cache/assets/test/sprockets/e31424e52369b0afcae5d0350c19ae82 +0 -0
  77. data/test/dummy/tmp/cache/assets/test/sprockets/f202557856e13ca5921856c87d402bd4 +0 -0
  78. data/test/dummy/tmp/cache/assets/test/sprockets/fe7c9c32074445201dccf284f19078bf +0 -0
  79. data/test/helpers/rails_imager/images_helper_test.rb +8 -0
  80. data/test/image_handler_test.rb +138 -0
  81. data/test/integration/navigation_test.rb +10 -0
  82. data/test/rails_imager_test.rb +7 -0
  83. data/test/test.png +0 -0
  84. data/test/test_helper.rb +15 -0
  85. metadata +219 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c248058b9c9a017897c2e1fd52700bd58510a11
4
+ data.tar.gz: dc53d7c0db172e315f661f2af858b10eaf83c20c
5
+ SHA512:
6
+ metadata.gz: bf9a71fd2e691b4ec05addd493aaaa3f37e94976a683a3946470c33ab0a3c84d27e1137c635485d93e45a76510e40288c096ab5c6088e72131d30756052ff245
7
+ data.tar.gz: b4fb1dbe754b95503e4e23837174991cdc9d8d6dcc2fcab4d10509c7176ad3c93ea3abba418bc49504479359e566196f6290201d1822da3f7ee11bdc38ddb13d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RailsImager'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ module RailsImager
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,33 @@
1
+ require_dependency "rails_imager/application_controller"
2
+
3
+ class RailsImager::ImagesController < ApplicationController
4
+ def show
5
+ rimger = RailsImager::ImageHandler.new
6
+
7
+ image_params = params[:image] || {}
8
+
9
+ # Check for invalid parameters.
10
+ image_params.each do |key, val|
11
+ raise ArgumentError, "Invalid parameter: '#{key}'." unless RailsImager::ImageHandler::PARAMS_ARGS.map{ |param| param.to_s }.include?(key)
12
+ end
13
+
14
+ image_path = "#{Rails.public_path}/#{params[:id]}"
15
+ image_path = File.realpath(image_path)
16
+ validate_path(image_path)
17
+
18
+ image = Magick::Image.read(image_path).first
19
+ image = rimger.img_from_params(:image => image, :params => image_params)
20
+
21
+ response.headers["Expires"] = 2.hours.from_now.httpdate
22
+ response.headers["Last-Modified"] = File.mtime(image_path).httpdate
23
+
24
+ send_data image.to_blob, :type => "image/png", :disposition => "inline"
25
+ end
26
+
27
+ private
28
+
29
+ def validate_path(image_path)
30
+ raise "No such file: '#{image_path}'." unless File.exists?(image_path)
31
+ raise "Image wasn't in the public folder: '#{image_path}'." unless image_path.start_with?(Rails.public_path.to_s)
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ module RailsImager
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,26 @@
1
+ require "uri"
2
+
3
+ module RailsImager::ImagesHelper
4
+ def rails_imager_p(path, args = {})
5
+ newpath = "/rails_imager/images/"
6
+ newpath << URI.encode(path)
7
+ newpath << "/?"
8
+
9
+ first = true
10
+ args.each do |key, val|
11
+ if first
12
+ first = false
13
+ else
14
+ newpath << "&"
15
+ end
16
+
17
+ realkey = "image[#{key}]"
18
+
19
+ newpath << URI.encode(realkey.to_s)
20
+ newpath << "="
21
+ newpath << URI.encode(val.to_s)
22
+ end
23
+
24
+ return newpath
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>RailsImager</title>
5
+ <%= stylesheet_link_tag "rails_imager/application", media: "all" %>
6
+ <%= javascript_include_tag "rails_imager/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,2 @@
1
+ <h1>Images#show</h1>
2
+ <p>Find me in app/views/rails_imager/images/show.html.erb</p>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ RailsImager::Engine.routes.draw do
2
+ resources :images, :constraints => { :id => /.*/ }, :only => :show
3
+ end
@@ -0,0 +1,17 @@
1
+ require "rails_imager/engine"
2
+
3
+ module RailsImager
4
+ def self.const_missing(name)
5
+ if name.to_s.end_with?("Controller")
6
+ path = "#{File.dirname(__FILE__)}/../app/controllers/rails_imager/#{StringCases.camel_to_snake(name)}"
7
+ elsif name.to_s.end_with?("Helper")
8
+ path = "#{File.dirname(__FILE__)}/../app/helpers/rails_imager/#{StringCases.camel_to_snake(name)}"
9
+ else
10
+ path = "#{File.dirname(__FILE__)}/rails_imager/#{StringCases.camel_to_snake(name)}"
11
+ end
12
+
13
+ require path
14
+ raise LoadError, "Not autoloaded: #{name}" unless const_defined?(name)
15
+ return const_get(name)
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module RailsImager
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RailsImager
4
+ end
5
+ end
@@ -0,0 +1,241 @@
1
+ require "RMagick"
2
+ require "knjrbfw"
3
+ require "tmpdir"
4
+
5
+ class RailsImager::ImageHandler
6
+ PARAMS_ARGS = [:width, :height, :smartsize, :maxwidth, :maxheight, :rounded_corners, :border, :border_color]
7
+
8
+ #This is the default cache which is plased in the temp-directory, so it will be cleared on every restart. It should always exist.
9
+ DEFAULT_CACHE_DIR = "#{Dir.tmpdir}/rails-imager-cache"
10
+ Dir.mkdir(DEFAULT_CACHE_DIR) if !Dir.exists?(DEFAULT_CACHE_DIR)
11
+
12
+ #Default arguments unless something else is given in constructor.
13
+ DEFAULT_ARGS = {
14
+ :cache_dir => DEFAULT_CACHE_DIR
15
+ }
16
+
17
+ INITIALIZE_ALLOWED_ARGS = [:cache_dir]
18
+ #Initializes the RailsImager.
19
+ def initialize(args = {})
20
+ args.each do |key, val|
21
+ raise "Invalid argument: '#{key}'." unless INITIALIZE_ALLOWED_ARGS.include?(key)
22
+ end
23
+
24
+ @args = DEFAULT_ARGS.merge(args)
25
+ end
26
+
27
+ IMG_FROM_PARAMS_ALLOWED_ARGS = [:image, :params]
28
+ #Create a new image-object based on the given image-object and the parameters.
29
+ def img_from_params(args)
30
+ args.each do |key, val|
31
+ raise "Invalid argument: '#{key}'." unless IMG_FROM_PARAMS_ALLOWED_ARGS.include?(key)
32
+ end
33
+
34
+ #Set up vars.
35
+ img = args[:image]
36
+ raise "No image given." unless img
37
+ raise "Wrong image-object given: '#{img.class.name}'." unless img.is_a?(Magick::Image)
38
+
39
+ params = args[:params]
40
+ raise "No params given." unless params
41
+
42
+ img_width = img.columns
43
+ img_height = img.rows
44
+
45
+ width = img_width
46
+ height = img_height
47
+
48
+ width = params[:width].to_i if params[:width].to_i > 0
49
+ height = params[:height].to_i if params[:height].to_i > 0
50
+
51
+ #Check arguments and manipulate image.
52
+ if params[:smartsize]
53
+ if img_width > img_height
54
+ width = params[:smartsize].to_i
55
+ height = (img_height.to_f / (img_width.to_f / width.to_f)).to_i
56
+ else
57
+ height = params[:smartsize].to_i
58
+ width = (img_width.to_f / (img_height.to_f / height.to_f)).to_i
59
+ end
60
+ end
61
+
62
+ if params[:maxwidth]
63
+ maxwidth = params[:maxwidth].to_i
64
+
65
+ if width > maxwidth
66
+ height = (img_height.to_f / (img_width.to_f / maxwidth.to_f)).to_i
67
+ width = maxwidth
68
+ end
69
+ end
70
+
71
+ if params[:maxheight]
72
+ maxheight = params[:maxheight].to_i
73
+
74
+ if height > maxheight
75
+ width = (img_width.to_f / (img_height.to_f / maxheight.to_f)).to_i
76
+ height = maxheight
77
+ end
78
+ end
79
+
80
+ if params[:width] && !params[:height]
81
+ height = (img_height.to_f / (img_width.to_f / width.to_f)).to_i
82
+ elsif params[:height] && !params[:width]
83
+ width = (img_width.to_f / (img_height.to_f / height.to_f)).to_i
84
+ elsif params[:width] && params[:height]
85
+ width = params[:width].to_i
86
+ height = params[:height].to_i
87
+ end
88
+
89
+ if width != img_width || height != img_height
90
+ img = img.resize(width, height)
91
+ end
92
+
93
+ if params[:rounded_corners]
94
+ img = img.clone
95
+ args = {:img => img, :radius => params[:rounded_corners].to_i}
96
+
97
+ if params[:border] && params[:border_color]
98
+ args[:border] = params[:border].to_i
99
+ args[:border_color] = params[:border_color]
100
+ end
101
+
102
+ Knj::Image.rounded_corners(args)
103
+ end
104
+
105
+ return img
106
+ end
107
+
108
+ CACHENAME_FROM_PARAMS_ALLOWED_ARGS = [:params, :image, :fpath]
109
+ #Returns the path to a cached object based on the given filepath, image and request-parameters.
110
+ def cachename_from_params(args)
111
+ args.each do |key, val|
112
+ raise "Invalid argument: '#{key}'." unless CACHENAME_FROM_PARAMS_ALLOWED_ARGS.include?(key)
113
+ end
114
+
115
+ params = args[:params]
116
+ raise "No params was given." unless params
117
+
118
+ if args[:image] && !args[:image].filename.to_s.strip.empty?
119
+ name = Knj::Strings.sanitize_filename(args[:image].filename)
120
+ elsif args[:fpath]
121
+ name = Knj::Strings.sanitize_filename(args[:fpath])
122
+ else
123
+ raise "No image or fpath was given."
124
+ end
125
+
126
+ name << "__ARGS__"
127
+
128
+ PARAMS_ARGS.each do |val|
129
+ name += "_" unless name.empty?
130
+ name += "#{val}-#{params[val]}"
131
+ end
132
+
133
+ return name
134
+ end
135
+
136
+ FORCE_CACHE_FROM_PARAMS_ALLOWED_ARGS = [:fpath, :image, :request, :params]
137
+ #Checks if a cache-file is created for the given filepath or image. If not then it will be created. If the cache-object is too old, then it will updated. Then returns the path to the cache-object in the end.
138
+ def force_cache_from_request(args)
139
+ args.each do |key, val|
140
+ raise "Invalid argument: '#{key}'." if !FORCE_CACHE_FROM_PARAMS_ALLOWED_ARGS.include?(key)
141
+ end
142
+
143
+ img = nil
144
+
145
+ if args[:fpath] && !args[:fpath].to_s.strip.empty?
146
+ fpath = args[:fpath]
147
+ elsif args[:image] && !args[:image].filename.to_s.strip.empty?
148
+ fpath = args[:image].filename
149
+ img = args[:image]
150
+ else
151
+ raise "No image or filename was given."
152
+ end
153
+
154
+ request = args[:request]
155
+ raise "Invalid request: '#{request.class.name}'." if !request
156
+ params = args[:params]
157
+ raise "No parameters on that request: '#{params.class.name}'." if !params
158
+
159
+ mod_time = File.mtime(fpath)
160
+ cachename = self.cachename_from_params(:fpath => fpath, :params => params)
161
+ cachepath = "#{@args[:cache_dir]}/#{cachename}"
162
+ not_modified = false
163
+ headers = request.headers
164
+
165
+ if !File.exists?(cachepath) || File.mtime(cachepath) < File.mtime(fpath)
166
+ should_generate = true
167
+ else
168
+ should_generate = false
169
+ end
170
+
171
+ if should_generate
172
+ img = Magick::Image.read(fpath).first unless img
173
+ img = self.img_from_params(:image => img, :params => params)
174
+ img.write(cachepath)
175
+ else
176
+ if_mod_since_time = nil
177
+ if headers["HTTP_IF_MODIFIED_SINCE"]
178
+ if_mod_since_time = Datet.in(headers["HTTP_IF_MODIFIED_SINCE"]).time
179
+ else
180
+ if_mod_since_time = request.if_modified_since
181
+ end
182
+
183
+ not_modified = true if if_mod_since_time && if_mod_since_time.utc.to_s == mod_time.utc.to_s
184
+ end
185
+
186
+ return {
187
+ :cachepath => cachepath,
188
+ :generated => should_generate,
189
+ :not_modified => not_modified,
190
+ :mod_time => mod_time
191
+ }
192
+ end
193
+
194
+ HANDLE_ALLOWED_ARGS = [:fpath, :image, :controller, :params]
195
+ #Automatically handles the image with generation, cache control and more.
196
+ def handle(args)
197
+ args.each do |key, val|
198
+ raise "Invalid argument: '#{key}'." if !HANDLE_ALLOWED_ARGS.include?(key)
199
+ end
200
+
201
+ controller = args[:controller]
202
+ raise "No controller was given." if !controller
203
+ request = controller.request
204
+ params = args[:params]
205
+ raise "No params was given." if !params
206
+
207
+ if args[:image]
208
+ fpath = args[:image].filename
209
+ elsif args[:fpath]
210
+ fpath = args[:fpath]
211
+ end
212
+
213
+ raise "No filepath was given." if !fpath
214
+ res = self.force_cache_from_request(fpath: fpath, request: request, params: params)
215
+
216
+ if res[:not_modified]
217
+ controller.render :nothing => true, :status => "304 Not Modified"
218
+ else
219
+ controller.expires_in 1.weeks, public: true
220
+ controller.response.header["Last-Modified"] = res[:mod_time].to_s
221
+ controller.send_file res[:cachepath], type: "image/png", disposition: "inline", filename: "picture.png"
222
+ end
223
+ end
224
+
225
+ #Yields every cache-file to the block. If the block returns true, then the cache-file will be deleted. If no block is given all the cache will be deleted.
226
+ def clear_cache(&blk)
227
+ Dir.foreach(@args[:cache_dir]) do |file|
228
+ next if file == "." || file == ".."
229
+ fn = "#{@args[:cache_dir]}/#{file}"
230
+ next if !File.file?(fn)
231
+
232
+ if blk == nil
233
+ res = true
234
+ else
235
+ res = yield(fn)
236
+ end
237
+
238
+ File.unlink(fn) if res == true
239
+ end
240
+ end
241
+ end