imageproxy 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/imageproxy.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{imageproxy}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Erik Hanson"]
12
- s.date = %q{2011-06-08}
12
+ s.date = %q{2011-06-09}
13
13
  s.description = %q{A image processing proxy server, written in Ruby as a Rack application. Requires ImageMagick.}
14
14
  s.email = %q{erik@eahanson.com}
15
15
  s.extra_rdoc_files = [
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/imageproxy/compare.rb",
32
32
  "lib/imageproxy/convert.rb",
33
33
  "lib/imageproxy/identify.rb",
34
+ "lib/imageproxy/identify_format.rb",
34
35
  "lib/imageproxy/options.rb",
35
36
  "lib/imageproxy/selftest.rb",
36
37
  "lib/imageproxy/server.rb",
@@ -40,6 +41,7 @@ Gem::Specification.new do |s|
40
41
  "public/sample_10x20.png",
41
42
  "spec/command_spec.rb",
42
43
  "spec/convert_spec.rb",
44
+ "spec/identify_format_spec.rb",
43
45
  "spec/options_spec.rb",
44
46
  "spec/server_spec.rb",
45
47
  "spec/signature_spec.rb",
@@ -9,7 +9,7 @@ module Imageproxy
9
9
 
10
10
  def curl(url, options={})
11
11
  user_agent = options[:user_agent] || "imageproxy"
12
- %|curl -s -A "#{user_agent}" "#{url}"|
12
+ %|curl -L -s -A "#{user_agent}" "#{url}"|
13
13
  end
14
14
 
15
15
  def to_path(obj)
@@ -0,0 +1,14 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "command")
2
+
3
+ module Imageproxy
4
+ class IdentifyFormat < Imageproxy::Command
5
+ def initialize(file)
6
+ @file = file
7
+ end
8
+
9
+ def execute
10
+ result = execute_command %'identify -format "%m" #{@file.path}'
11
+ result.start_with?("identify:") ? nil : result
12
+ end
13
+ end
14
+ end
@@ -42,10 +42,6 @@ module Imageproxy
42
42
  @hash[symbol.to_s] || @hash[symbol]
43
43
  end
44
44
 
45
- def content_type
46
- MIME::Types.of(@hash['source']).first.content_type
47
- end
48
-
49
45
  private
50
46
 
51
47
  def unescape_source
@@ -1,6 +1,7 @@
1
1
  require File.join(File.expand_path(File.dirname(__FILE__)), "options")
2
2
  require File.join(File.expand_path(File.dirname(__FILE__)), "convert")
3
3
  require File.join(File.expand_path(File.dirname(__FILE__)), "identify")
4
+ require File.join(File.expand_path(File.dirname(__FILE__)), "identify_format")
4
5
  require File.join(File.expand_path(File.dirname(__FILE__)), "selftest")
5
6
  require File.join(File.expand_path(File.dirname(__FILE__)), "signature")
6
7
  require 'uri'
@@ -29,7 +30,7 @@ module Imageproxy
29
30
  end
30
31
 
31
32
  file.open
32
- [200, {"Content-Type" => options.content_type, "Cache-Control" => "max-age=#{cachetime}, must-revalidate"}, file]
33
+ [200, {"Cache-Control" => "max-age=#{cachetime}, must-revalidate"}.merge(content_type(file, options)), file]
33
34
  when "identify"
34
35
  check_signature request, options
35
36
  check_domain options
@@ -103,5 +104,12 @@ module Imageproxy
103
104
  sizes[0].to_i
104
105
  end
105
106
  end
107
+
108
+ def content_type(file, options)
109
+ format = options.format
110
+ format = Imageproxy::IdentifyFormat.new(file).execute unless format
111
+ format = options.source unless format
112
+ format ? { "Content-Type" => MIME::Types.of(format).first.content_type } : {}
113
+ end
106
114
  end
107
115
  end
data/spec/command_spec.rb CHANGED
@@ -5,14 +5,14 @@ describe Imageproxy::Command do
5
5
  context "when a user agent is supplied" do
6
6
  it "should send that user agent" do
7
7
  Imageproxy::Command.new.send(:curl, "http://example.com/dog.jpg", :user_agent => "some user agent").should ==
8
- %|curl -s -A "some user agent" "http://example.com/dog.jpg"|
8
+ %|curl -L -s -A "some user agent" "http://example.com/dog.jpg"|
9
9
  end
10
10
  end
11
11
 
12
12
  context "when no user agent is supplied" do
13
13
  it "should send a default user agent" do
14
14
  Imageproxy::Command.new.send(:curl, "http://example.com/dog.jpg").should ==
15
- %|curl -s -A "imageproxy" "http://example.com/dog.jpg"|
15
+ %|curl -L -s -A "imageproxy" "http://example.com/dog.jpg"|
16
16
  end
17
17
  end
18
18
  end
data/spec/convert_spec.rb CHANGED
@@ -21,7 +21,7 @@ describe Imageproxy::Convert do
21
21
  end
22
22
 
23
23
  it "should generate the proper command-line" do
24
- @command.should_receive(:execute_command).with(%'curl -s -A "imageproxy" "http://example.com/dog.jpg" | convert - -resize 10x20 png:/mock/file/path')
24
+ @command.should_receive(:execute_command).with(%'curl -L -s -A "imageproxy" "http://example.com/dog.jpg" | convert - -resize 10x20 png:/mock/file/path')
25
25
  @command.execute
26
26
  end
27
27
 
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imageproxy::IdentifyFormat do
4
+ before do
5
+ @mock_file = mock("file")
6
+ @mock_file.stub!(:path).and_return("/tmp/foo")
7
+ end
8
+
9
+ context "when the format looks valid" do
10
+ it "should return the format" do
11
+ command = Imageproxy::IdentifyFormat.new(@mock_file)
12
+ command.stub!(:execute_command).and_return("JPEG")
13
+ command.execute.should == "JPEG"
14
+ end
15
+ end
16
+
17
+ context "when the format looks like an error message" do
18
+ it "should return nil" do
19
+ command = Imageproxy::IdentifyFormat.new(@mock_file)
20
+ command.stub!(:execute_command).and_return("identify: some error message")
21
+ command.execute.should == nil
22
+ end
23
+ end
24
+ end
data/spec/options_spec.rb CHANGED
@@ -46,14 +46,6 @@ describe Imageproxy::Options do
46
46
  it("should keep params from path") { subject.source.should == "foo" }
47
47
  end
48
48
 
49
- describe "content type" do
50
- context "when guessing based on source filename" do
51
- it("should understand .png") { Imageproxy::Options.new("/convert", "source" => "foo.png").content_type.should == "image/png" }
52
- it("should understand .jpg") { Imageproxy::Options.new("/convert", "source" => "foo.jpg").content_type.should == "image/jpeg" }
53
- it("should understand .JPEG") { Imageproxy::Options.new("/convert", "source" => "foo.JPEG").content_type.should == "image/jpeg" }
54
- end
55
- end
56
-
57
49
  describe "obfuscation" do
58
50
  it "should allow the query string to be encoded in base64" do
59
51
  encoded = CGI.escape(Base64.encode64("resize=20x20&source=http://example.com/dog.jpg"))
data/spec/server_spec.rb CHANGED
@@ -118,5 +118,46 @@ describe "Server" do
118
118
  end
119
119
 
120
120
  end
121
+
122
+ describe "#content_type" do
123
+ before do
124
+ @options = Imageproxy::Options.new("/", {})
125
+
126
+ @mock_file = mock("file")
127
+ @mock_file.stub!(:path).and_return("/tmp/foo")
128
+
129
+ @mock_identify_format = Imageproxy::IdentifyFormat.new(@mock_file)
130
+ @mock_identify_format.stub!(:execute_command).and_return("identify: some error message\n")
131
+ Imageproxy::IdentifyFormat.stub!(:new).and_return(@mock_identify_format)
132
+ end
133
+
134
+ context "when the output format is specified" do
135
+ it "should return that format's mime type" do
136
+ @options = Imageproxy::Options.new("/", :format => "jpg")
137
+ app.send(:content_type, @mock_file, @options).should == { "Content-Type" => "image/jpeg" }
138
+ end
139
+ end
140
+
141
+ context "when 'identify' knows the format" do
142
+ it "should return that format's mime type" do
143
+ @mock_identify_format.stub!(:execute_command).and_return("JPEG\n")
144
+ app.send(:content_type, @mock_file, @options).should == { "Content-Type" => "image/jpeg" }
145
+ end
146
+ end
147
+
148
+ context "when the input source has a file extension" do
149
+ it "should return that format's mime type" do
150
+ @options = Imageproxy::Options.new("/", :source => "foo.jpg")
151
+ app.send(:content_type, @mock_file, @options).should == { "Content-Type" => "image/jpeg" }
152
+ end
153
+ end
154
+
155
+ context "when nothing is known about the format" do
156
+ it "should not return a mime type" do
157
+ app.send(:content_type, @mock_file, @options).should == {}
158
+ end
159
+ end
160
+
161
+ end
121
162
  end
122
163
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: imageproxy
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Erik Hanson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-08 00:00:00 -07:00
13
+ date: 2011-06-09 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -136,6 +136,7 @@ files:
136
136
  - lib/imageproxy/compare.rb
137
137
  - lib/imageproxy/convert.rb
138
138
  - lib/imageproxy/identify.rb
139
+ - lib/imageproxy/identify_format.rb
139
140
  - lib/imageproxy/options.rb
140
141
  - lib/imageproxy/selftest.rb
141
142
  - lib/imageproxy/server.rb
@@ -145,6 +146,7 @@ files:
145
146
  - public/sample_10x20.png
146
147
  - spec/command_spec.rb
147
148
  - spec/convert_spec.rb
149
+ - spec/identify_format_spec.rb
148
150
  - spec/options_spec.rb
149
151
  - spec/server_spec.rb
150
152
  - spec/signature_spec.rb
@@ -163,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
165
  requirements:
164
166
  - - ">="
165
167
  - !ruby/object:Gem::Version
166
- hash: 3737548046343188877
168
+ hash: 4082526096266678674
167
169
  segments:
168
170
  - 0
169
171
  version: "0"