kanoko 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/README.md +4 -0
- data/kanoko.gemspec +1 -0
- data/lib/kanoko.rb +6 -1
- data/lib/kanoko/application/convert.rb +121 -0
- data/lib/kanoko/configure.rb +3 -3
- data/lib/kanoko/version.rb +1 -1
- data/test/test_kanoko.rb +1 -1
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e42bf64558cec67348c3f8b2793079179c5012c1
|
4
|
+
data.tar.gz: 6148e4976c50a71236ffe7c1ab8b5b2860f438e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3246a4f831cb4355b0f200e52ef608f5c04a79228a7498c225fbb66efb1aed28dac8a74d5de6fe7ba13bb305848935cfe79c94c4637f8cff994223af2d77d25d
|
7
|
+
data.tar.gz: ab2dc0dc148d7a1637b5d6d3ca581b067f68c98778c466d2bb94148a99e7290ae2167ae10e3116887a44d3b0a0db8502a77120ec3daac46bf18dd24b9b1661b2
|
data/.travis.yml
ADDED
data/README.md
ADDED
data/kanoko.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
+
spec.add_dependency "sinatra"
|
20
21
|
spec.add_development_dependency "bundler"
|
21
22
|
spec.add_development_dependency "rake"
|
22
23
|
spec.add_development_dependency "minitest"
|
data/lib/kanoko.rb
CHANGED
@@ -7,6 +7,11 @@ module Kanoko
|
|
7
7
|
# p Kanoko.configure #=> #<Kanoko::Configure ...>
|
8
8
|
def configure
|
9
9
|
@configure ||= Configure.new
|
10
|
+
if block_given?
|
11
|
+
yield @configure
|
12
|
+
else
|
13
|
+
@configure
|
14
|
+
end
|
10
15
|
end
|
11
16
|
module_function :configure
|
12
17
|
|
@@ -31,7 +36,7 @@ module Kanoko
|
|
31
36
|
|
32
37
|
def make_path(func, args, src)
|
33
38
|
hash = make_hash(func, args, src)
|
34
|
-
"/#{hash}/#{[func, args
|
39
|
+
"/#{hash}/#{[func, args].map{|i| URI.encode_www_form_component(i)}.join('/')}/#{src}"
|
35
40
|
end
|
36
41
|
module_function :make_path
|
37
42
|
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
require 'net/http'
|
3
|
+
require 'tempfile'
|
4
|
+
require 'kanoko'
|
5
|
+
|
6
|
+
# This is a experimental implementation.
|
7
|
+
# You can set configure and other.
|
8
|
+
# This application receve url make by Kanoko.url_for().
|
9
|
+
# You can choice function that image processing.
|
10
|
+
# Image resource can get by url,
|
11
|
+
# And that write once to file,
|
12
|
+
# And image processing by imagemagick,
|
13
|
+
# And that read file binary.
|
14
|
+
#
|
15
|
+
# example:
|
16
|
+
# require 'kanoko/application/convert'
|
17
|
+
#
|
18
|
+
# ENV['KANOKO_DIGEST_FUNC'] = "sha1"
|
19
|
+
# ENV['KANOKO_SECRET_KEY'] = "secret"
|
20
|
+
#
|
21
|
+
# class MyApp < Kanoko::Application::Convert
|
22
|
+
# before do
|
23
|
+
# content_type 'image/png'
|
24
|
+
# end
|
25
|
+
# configure :production do
|
26
|
+
# require 'newrelic_rpm'
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# run MyApp
|
31
|
+
module Kanoko
|
32
|
+
module Application
|
33
|
+
class Convert < Sinatra::Application
|
34
|
+
get '/:hash/:func/:args/*' do
|
35
|
+
hash = params[:hash]
|
36
|
+
func = params[:func]
|
37
|
+
args = params[:args]
|
38
|
+
src_path = env["REQUEST_URI"].sub(%r{/.*?/.*?/.*?/(.*)}, '\1')
|
39
|
+
|
40
|
+
unless hash == Kanoko.make_hash(func, args, src_path)
|
41
|
+
logger.error "hash check failed #{[func, args, src_path]}"
|
42
|
+
return 400
|
43
|
+
end
|
44
|
+
|
45
|
+
res = http_get(URI.parse("#{(request.secure? ? 'https' : 'http')}://#{src_path}"))
|
46
|
+
if res.nil?
|
47
|
+
return 404
|
48
|
+
end
|
49
|
+
after_response res
|
50
|
+
|
51
|
+
Tempfile.open("src") do |src_file|
|
52
|
+
src_file.write res.body
|
53
|
+
src_file.fdatasync
|
54
|
+
Tempfile.open("dst") do |dst_file|
|
55
|
+
default_env = {"OMP_NUM_THREADS" => "1"}
|
56
|
+
result = case func.to_sym
|
57
|
+
|
58
|
+
when :crop
|
59
|
+
system(default_env,
|
60
|
+
'convert',
|
61
|
+
'-thumbnail', "#{args}^",
|
62
|
+
'-gravity', 'north',
|
63
|
+
'-extent', args,
|
64
|
+
'-background', 'transparent',
|
65
|
+
'-depth', '8',
|
66
|
+
src_file.path, dst_file.path)
|
67
|
+
|
68
|
+
when :resize
|
69
|
+
system(default_env,
|
70
|
+
'convert',
|
71
|
+
'-thumbnail', args,
|
72
|
+
'-depth', '8',
|
73
|
+
src_file.path, dst_file.path)
|
74
|
+
|
75
|
+
else
|
76
|
+
logger.error "undefined func #{func}"
|
77
|
+
return 400
|
78
|
+
end
|
79
|
+
|
80
|
+
unless result
|
81
|
+
logger.error "command fail $?=#{$?.inspect}"
|
82
|
+
return 500
|
83
|
+
end
|
84
|
+
dst_file.read
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def http_get(uri)
|
92
|
+
retries = 2
|
93
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
94
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
95
|
+
http.read_timeout = 1
|
96
|
+
http.use_ssl = true if uri.scheme == 'https'
|
97
|
+
begin
|
98
|
+
res = http.start do |http|
|
99
|
+
http.request(req)
|
100
|
+
end
|
101
|
+
res.value
|
102
|
+
res
|
103
|
+
rescue => e
|
104
|
+
if 1 < retries
|
105
|
+
retries -= 1
|
106
|
+
sleep rand(0..0.3)
|
107
|
+
retry
|
108
|
+
end
|
109
|
+
logger.error "Can not get image from '#{uri}' with #{e.message}"
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def after_response(res)
|
115
|
+
res.each do |key, value|
|
116
|
+
headers[key] ||= value
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/kanoko/configure.rb
CHANGED
@@ -12,7 +12,7 @@ module Kanoko
|
|
12
12
|
# hash_proc expect Proc
|
13
13
|
#
|
14
14
|
# example:
|
15
|
-
# Kanoko.configure
|
15
|
+
# Kanoko.configure do |c|
|
16
16
|
# c.kanoko_host = "http://example.com"
|
17
17
|
# c.digest_func = "sha1"
|
18
18
|
# c.secret_key = "secret"
|
@@ -20,8 +20,8 @@ module Kanoko
|
|
20
20
|
# Kanoko.url_for(:resize, "100x100") #=> "http://example.com/.../.../..."
|
21
21
|
def initialize
|
22
22
|
@kanoko_host = nil
|
23
|
-
@digest_func =
|
24
|
-
@secret_key =
|
23
|
+
@digest_func = ENV['KANOKO_DIGEST_FUNC']
|
24
|
+
@secret_key = ENV['KANOKO_SECRET_KEY']
|
25
25
|
@hash_proc = ->(*args){
|
26
26
|
if @digest_func.nil? || @secret_key.nil?
|
27
27
|
fail ConfigureError, "`digest_func' and `secret_key' must be set"
|
data/lib/kanoko/version.rb
CHANGED
data/test/test_kanoko.rb
CHANGED
@@ -28,7 +28,7 @@ class TestKanoko < Minitest::Test
|
|
28
28
|
def test_url_for_with_default_hash_proc
|
29
29
|
change_hash_proc(proc{ "aaa" }) do
|
30
30
|
assert_equal "http://example.com/aaa/test_func/test_args/test_path", Kanoko.url_for(:test_func, "test_args", "test_path")
|
31
|
-
assert_equal "http://example.com/aaa/test_func/%2F%3F-_%3D%21%40%23%3C%3E%5C
|
31
|
+
assert_equal "http://example.com/aaa/test_func/%2F%3F-_%3D%21%40%23%3C%3E%5C//?-_=!@#<>\\", Kanoko.url_for(:test_func, "/?-_=!@#<>\\", "/?-_=!@#<>\\")
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kanoko
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,11 +74,14 @@ extensions: []
|
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
62
76
|
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
63
78
|
- Gemfile
|
64
79
|
- LICENSE.txt
|
80
|
+
- README.md
|
65
81
|
- Rakefile
|
66
82
|
- kanoko.gemspec
|
67
83
|
- lib/kanoko.rb
|
84
|
+
- lib/kanoko/application/convert.rb
|
68
85
|
- lib/kanoko/configure.rb
|
69
86
|
- lib/kanoko/errors.rb
|
70
87
|
- lib/kanoko/version.rb
|