rails_imager 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd9b2fe3759e88819d0db1f9b3af5fba8e6d576a
4
- data.tar.gz: 39f8ba07520ec430d733ddfefedf5342707ec07c
3
+ metadata.gz: d8756a05e1d9e4fded390426bb42a4ff85573e86
4
+ data.tar.gz: 3f9c1935bed9712e465dd604d3f954023ba23150
5
5
  SHA512:
6
- metadata.gz: 4f1569d9140d97e751f842f2d74e03088c49d2a07e723238b1264c4a69a4caf390340569998427c1580b7c85a379ce932131a58907c6c22dc2d138ab2578af8c
7
- data.tar.gz: 137034cb9d007fe284496e1b3c55e2506b3cff718dd8a707836cf30686e729c23d7cc983c7917cf9d60ec9f8b96b769e2ea51f58792d095f651d6a541ee865e8
6
+ metadata.gz: 80ee5c64d96a0572b3345504d2510fcdd8ac68a90e16de50338a69aca91c6589cb1a5d3dea3e872331d91d7bcbe77c32c966e3e336f7173a8dd1661129bc7ad7
7
+ data.tar.gz: c4f8bb46f8141d0f14b78908a02b47c2bc61f2d1b450b77701352e5b8755ab0537f2a881d8b1db719818539645d9f8414158629e0da4ba8c6c28c5baa03773d1
@@ -1,6 +1,6 @@
1
1
  class RailsImager::ImagesController < ApplicationController
2
2
  PARAMS_ARGS = [:width, :height, :smartsize, :maxwidth, :maxheight, :rounded_corners, :border, :border_color, :force]
3
-
3
+
4
4
  def show
5
5
  set_and_validate_parameters
6
6
  set_path
@@ -8,29 +8,29 @@ class RailsImager::ImagesController < ApplicationController
8
8
  generate_cache_name
9
9
  generate_cache if should_generate_cache?
10
10
  set_headers
11
-
11
+
12
12
  if not_modified? && !force?
13
13
  render :nothing => true, :status => "304 Not Modified"
14
14
  else
15
15
  send_file @cache_path, :type => "image/png", :disposition => "inline", :filename => "picture.png"
16
16
  end
17
17
  end
18
-
18
+
19
19
  private
20
-
20
+
21
21
  def set_path
22
22
  @path = "#{Rails.public_path}/#{params[:id]}"
23
23
  @full_path = File.realpath(@path)
24
24
  validate_path
25
25
  end
26
-
26
+
27
27
  def set_and_validate_parameters
28
28
  @image_params = params[:image] || {}
29
29
  @image_params.each do |key, val|
30
30
  raise ArgumentError, "Invalid parameter: '#{key}'." unless PARAMS_ARGS.map{ |param| param.to_s }.include?(key)
31
31
  end
32
32
  end
33
-
33
+
34
34
  def force?
35
35
  if @image_params[:force] && @image_params[:force].to_s == "true"
36
36
  return true
@@ -38,58 +38,67 @@ private
38
38
  return false
39
39
  end
40
40
  end
41
-
41
+
42
42
  def not_modified?
43
43
  if request.headers["HTTP_IF_MODIFIED_SINCE"]
44
44
  if_mod_since_time = Datet.in(request.headers["HTTP_IF_MODIFIED_SINCE"]).time
45
45
  else
46
46
  if_mod_since_time = request.if_modified_since
47
47
  end
48
-
48
+
49
49
  if if_mod_since_time && if_mod_since_time.utc.to_s == @mod_time.utc.to_s
50
50
  return true
51
51
  end
52
-
52
+
53
53
  return false
54
54
  end
55
-
55
+
56
56
  def set_headers
57
57
  response.last_modified = @mod_time
58
-
58
+
59
59
  if force?
60
60
  response.headers["Expires"] = Time.now.httpdate
61
61
  else
62
62
  response.headers["Expires"] = 2.hours.from_now.httpdate
63
63
  end
64
64
  end
65
-
65
+
66
66
  def validate_path
67
- raise "No such file: '#{@full_path}'." unless File.exists?(@full_path)
68
- raise "Image wasn't in the public folder: '#{@full_path}'." unless @full_path.start_with?(File.realpath(Rails.public_path.to_s))
67
+ allowed_paths = RailsImager.config.allowed_paths
68
+
69
+ allowed = false
70
+ allowed_paths.each do |allowed_path|
71
+ if @full_path.start_with?(allowed_path)
72
+ allowed = true
73
+ break
74
+ end
75
+ end
76
+
77
+ raise ArgumentError, "Image wasn't in an allowed path: '#{@full_path}'." unless allowed
69
78
  end
70
-
79
+
71
80
  def generate_cache_name
72
81
  @cache_name = ::Knj::Strings.sanitize_filename(@path)
73
82
  @cache_name << "__ARGS_"
74
-
83
+
75
84
  PARAMS_ARGS.each do |val|
76
85
  next if val.to_s == "force"
77
86
  @cache_name << "_#{val}-#{@image_params[val]}"
78
87
  end
79
-
88
+
80
89
  @cache_path = "#{cache_directory}/#{@cache_name}"
81
90
  end
82
-
91
+
83
92
  def cache_directory
84
93
  require "tmpdir"
85
94
  cache_path = "#{Dir.tmpdir}/rails-imager-cache"
86
95
  Dir.mkdir(cache_path) unless Dir.exists?(cache_path)
87
96
  return cache_path
88
97
  end
89
-
98
+
90
99
  def should_generate_cache?
91
100
  return true if force?
92
-
101
+
93
102
  if File.exists?(@cache_path)
94
103
  if File.mtime(@cache_path) < File.mtime(@full_path)
95
104
  return true
@@ -100,36 +109,36 @@ private
100
109
  return true
101
110
  end
102
111
  end
103
-
112
+
104
113
  def generate_cache
105
114
  @image = ::Magick::Image.read(@full_path).first
106
115
  @image.format = "png"
107
116
  apply_image_changes
108
117
  @image.write(@cache_path)
109
118
  end
110
-
119
+
111
120
  #Create a new image-object based on the given image-object and the parameters.
112
121
  def apply_image_changes
113
122
  @image_width = @image.columns
114
123
  @image_height = @image.rows
115
-
124
+
116
125
  if @image_params[:width].to_i > 0
117
126
  @width = @image_params[:width].to_i
118
127
  else
119
128
  @width = @image_width
120
129
  end
121
-
130
+
122
131
  if @image_params[:height].to_i > 0
123
132
  @height = @image_params[:height].to_i
124
133
  else
125
134
  @height = @image_height
126
135
  end
127
-
136
+
128
137
  calcuate_sizes
129
138
  @image = @image.resize(@width, @height) if @width != @image_width || @height != @image_height
130
139
  apply_rounded_corners if @image_params[:rounded_corners]
131
140
  end
132
-
141
+
133
142
  def calcuate_sizes
134
143
  validate_and_set_smartsize if @image_params[:smartsize]
135
144
  validate_and_set_max_width
@@ -137,15 +146,15 @@ private
137
146
  calculate_missing_width if @image_params[:height] && !@image_params[:width]
138
147
  calculate_missing_height if @image_params[:width] && !@image_params[:height]
139
148
  end
140
-
149
+
141
150
  def calculate_missing_height
142
151
  @height = (@image_height.to_f / (@image_width.to_f / @width.to_f)).to_i
143
152
  end
144
-
153
+
145
154
  def calculate_missing_width
146
155
  @width = (@image_width.to_f / (@image_height.to_f / @height.to_f)).to_i
147
156
  end
148
-
157
+
149
158
  def validate_and_set_smartsize
150
159
  if @image_width > @image_height
151
160
  @width = @image_params[:smartsize].to_i
@@ -155,39 +164,39 @@ private
155
164
  calculate_missing_width
156
165
  end
157
166
  end
158
-
167
+
159
168
  def validate_and_set_max_width
160
169
  if @image_params[:maxwidth]
161
170
  maxwidth = @image_params[:maxwidth].to_i
162
-
171
+
163
172
  if @width > maxwidth
164
173
  @width = maxwidth
165
174
  calculate_missing_height
166
175
  end
167
176
  end
168
177
  end
169
-
178
+
170
179
  def validate_and_set_max_height
171
180
  if @image_params[:maxheight]
172
181
  maxheight = @image_params[:maxheight].to_i
173
-
182
+
174
183
  if @height > maxheight
175
184
  @height = maxheight
176
185
  calculate_missing_width
177
186
  end
178
187
  end
179
188
  end
180
-
189
+
181
190
  def apply_rounded_corners
182
191
  @image = @image.clone
183
192
  @image.format = "png" # Needs PNG format for transparency.
184
193
  args = {:img => @image, :radius => @image_params[:rounded_corners].to_i}
185
-
194
+
186
195
  if @image_params[:border] && @image_params[:border_color]
187
196
  args[:border] = @image_params[:border].to_i
188
197
  args[:border_color] = @image_params[:border_color]
189
198
  end
190
-
199
+
191
200
  ::Knj::Image.rounded_corners(args)
192
201
  end
193
202
  end
@@ -3,7 +3,7 @@ require "uri"
3
3
  module RailsImager::ImagesHelper
4
4
  def rails_imager_p(path, args = {})
5
5
  path = path_from_arg(path)
6
-
6
+
7
7
  if args.delete(:url)
8
8
  newpath = "#{request.protocol}#{request.host_with_port}"
9
9
  elsif args.delete(:mailer)
@@ -11,41 +11,41 @@ module RailsImager::ImagesHelper
11
11
  else
12
12
  newpath = ""
13
13
  end
14
-
14
+
15
15
  check_arguments(args)
16
-
16
+
17
17
  newpath << "/rails_imager/images/"
18
18
  newpath << path
19
-
19
+
20
20
  if args && args.any?
21
21
  newpath << "?"
22
22
  newpath << url_encoded_arguments(args)
23
23
  end
24
-
24
+
25
25
  return newpath
26
26
  end
27
-
27
+
28
28
  private
29
-
29
+
30
30
  def mailer_pre_path
31
31
  if ActionMailer::Base.default_url_options[:protocol]
32
32
  pre_path = ActionMailer::Base.default_url_options[:protocol]
33
33
  else
34
34
  pre_path = "http://"
35
35
  end
36
-
36
+
37
37
  pre_path << ActionMailer::Base.default_url_options[:host]
38
-
38
+
39
39
  if ActionMailer::Base.default_url_options[:port]
40
40
  pre_path << ":#{ActionMailer::Base.default_url_options[:port]}"
41
41
  end
42
-
42
+
43
43
  return pre_path
44
44
  end
45
-
45
+
46
46
  def url_encoded_arguments(args)
47
47
  path = ""
48
-
48
+
49
49
  first = true
50
50
  args.each do |key, val|
51
51
  if first
@@ -53,24 +53,24 @@ private
53
53
  else
54
54
  path << "&"
55
55
  end
56
-
56
+
57
57
  realkey = "image[#{key}]"
58
-
58
+
59
59
  path << URI.encode(realkey)
60
60
  path << "="
61
61
  path << URI.encode(val.to_s)
62
62
  end
63
-
63
+
64
64
  return path
65
65
  end
66
-
66
+
67
67
  def check_arguments(args)
68
68
  # Check for invalid parameters.
69
69
  args.each do |key, val|
70
70
  raise ArgumentError, "Invalid parameter: '#{key}'." unless RailsImager::ImagesController::PARAMS_ARGS.include?(key)
71
71
  end
72
72
  end
73
-
73
+
74
74
  def path_from_arg(path)
75
75
  if path.class.name == "Paperclip::Attachment"
76
76
  raise "Paperclip path does not start with public path: #{path.path}" unless path.path.to_s.start_with?(Rails.public_path.to_s)
@@ -0,0 +1,8 @@
1
+ class RailsImager::Config
2
+ attr_reader :allowed_paths
3
+
4
+ def initialize
5
+ @allowed_paths = []
6
+ @allowed_paths << Rails.public_path.to_s if Rails.public_path.to_s.present?
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsImager
2
- VERSION = "0.0.19"
2
+ VERSION = "0.0.20"
3
3
  end
data/lib/rails_imager.rb CHANGED
@@ -14,12 +14,21 @@ module RailsImager
14
14
  else
15
15
  path = "#{File.dirname(__FILE__)}/rails_imager/#{::StringCases.camel_to_snake(name)}.rb"
16
16
  end
17
-
17
+
18
18
  if File.exists?(path)
19
19
  require path
20
20
  return const_get(name) if const_defined?(name)
21
21
  end
22
-
22
+
23
23
  super
24
24
  end
25
+
26
+ @config = RailsImager::Config.new
27
+ def self.config
28
+ if block_given?
29
+ yield @config
30
+ else
31
+ return @config
32
+ end
33
+ end
25
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_imager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-07 00:00:00.000000000 Z
11
+ date: 2014-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -154,6 +154,7 @@ files:
154
154
  - lib/rails_imager.rb
155
155
  - lib/tasks/rails_imager_tasks.rake
156
156
  - lib/rails_imager/engine.rb
157
+ - lib/rails_imager/config.rb
157
158
  - lib/rails_imager/version.rb
158
159
  - MIT-LICENSE
159
160
  - Rakefile