kanoko 0.1.3 → 0.2.0
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.
- checksums.yaml +4 -4
- data/kanoko.gemspec +1 -3
- data/lib/kanoko/application/convert.rb +28 -17
- data/lib/kanoko/version.rb +1 -1
- metadata +2 -19
- data/lib/kanoko/application/convert/argument_parser.rb +0 -45
- data/test/helper.rb +0 -7
- data/test/resize.jpg +0 -0
- data/test/resize_and_crop.jpg +0 -0
- data/test/src.jpg +0 -0
- data/test/test_application_convert.rb +0 -99
- data/test/test_application_convert_argumentparser.rb +0 -58
- data/test/test_configure.rb +0 -27
- data/test/test_kanoko.rb +0 -50
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3e0d030893084af61384d4d8b6348a42ccc9a80
|
4
|
+
data.tar.gz: 826a04fc42395cf02695038c8af3be399fa2de09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4c817ced22e73cc3f6bfbaea398a27e7aaf2ae3c09bedb3f972e642340adaf7ab5a61bbd68b38ff26149f766904dc14603697e1bde79f2b3ef839a582e95c97
|
7
|
+
data.tar.gz: b39a548d60bac3f6abd22af3ab7a987c067245679c7f90ebd84c4c6a62e27d36f866e0b8b8073c17cb6d0f8eb2361bb8933abf911289b2c8c15d65307549db74
|
data/kanoko.gemspec
CHANGED
@@ -12,9 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.homepage = "https://github.com/spice-life/kanoko"
|
13
13
|
spec.license = "MIT"
|
14
14
|
|
15
|
-
spec.files = `git ls-files`.split(
|
16
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
16
|
spec.require_paths = ["lib"]
|
19
17
|
|
20
18
|
spec.add_dependency "sinatra"
|
@@ -31,32 +31,45 @@ require 'kanoko'
|
|
31
31
|
module Kanoko
|
32
32
|
module Application
|
33
33
|
class Convert < Sinatra::Application
|
34
|
+
require 'kanoko/application/convert/function'
|
34
35
|
|
35
36
|
# /123abc456def=/resize/200x200/crop/100x100/path/to/src
|
36
37
|
get '/:hash/*' do
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
# REQUEST_URI dependent on unicorn.
|
39
|
+
# request.path should be use only testing
|
40
|
+
raw_request_uri = env["REQUEST_URI"] || request.path
|
41
|
+
request_params = raw_request_uri.split('/').tap(&:shift)
|
42
|
+
hash = request_params.shift
|
43
|
+
unless 0 < request_params.length
|
40
44
|
logger.error "invalid url #{request_uri}"
|
41
45
|
return 400
|
42
46
|
end
|
43
47
|
|
44
|
-
|
45
|
-
|
48
|
+
list = Kanoko::Application::Convert::Function.list
|
49
|
+
convert_options = []
|
50
|
+
arguments = []
|
46
51
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
52
|
+
while id = request_params.shift.to_sym
|
53
|
+
if list.include?(id)
|
54
|
+
arguments << id
|
55
|
+
method = Function.new.method(id)
|
56
|
+
arg = request_params.shift(method.arity)
|
57
|
+
arg.map!{|i| URI.decode_www_form_component i}
|
58
|
+
arguments.concat arg if 0 < arg.length
|
59
|
+
convert_options.concat method.call(*arg)
|
60
|
+
else
|
61
|
+
request_params.unshift(id.to_s)
|
62
|
+
break
|
63
|
+
end
|
51
64
|
end
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
logger.error "hash check failed #{[*(argument.to_a.flatten), src_path]}"
|
65
|
+
|
66
|
+
check_path = request_params.map{ |i| URI.decode_www_form_component(i) }.join('/')
|
67
|
+
unless hash == Kanoko.make_hash(*arguments, check_path)
|
68
|
+
logger.error "hash check failed #{[*arguments, check_path]}"
|
57
69
|
return 400
|
58
70
|
end
|
59
71
|
|
72
|
+
src_path = request_params.join('/')
|
60
73
|
res = http_get(URI.parse("#{(request.secure? ? 'https' : 'http')}://#{src_path}"))
|
61
74
|
if res.nil?
|
62
75
|
return 404
|
@@ -72,7 +85,7 @@ module Kanoko
|
|
72
85
|
{"OMP_NUM_THREADS" => "1"},
|
73
86
|
'convert',
|
74
87
|
'-depth', '8',
|
75
|
-
|
88
|
+
convert_options,
|
76
89
|
src_file.path,
|
77
90
|
dst_file.path
|
78
91
|
].flatten
|
@@ -130,5 +143,3 @@ module Kanoko
|
|
130
143
|
end
|
131
144
|
end
|
132
145
|
end
|
133
|
-
|
134
|
-
require 'kanoko/application/convert/argument_parser'
|
data/lib/kanoko/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kanoko
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
@@ -110,20 +110,11 @@ files:
|
|
110
110
|
- kanoko.gemspec
|
111
111
|
- lib/kanoko.rb
|
112
112
|
- lib/kanoko/application/convert.rb
|
113
|
-
- lib/kanoko/application/convert/argument_parser.rb
|
114
113
|
- lib/kanoko/application/convert/function.rb
|
115
114
|
- lib/kanoko/configure.rb
|
116
115
|
- lib/kanoko/errors.rb
|
117
116
|
- lib/kanoko/version.rb
|
118
117
|
- logo.png
|
119
|
-
- test/helper.rb
|
120
|
-
- test/resize.jpg
|
121
|
-
- test/resize_and_crop.jpg
|
122
|
-
- test/src.jpg
|
123
|
-
- test/test_application_convert.rb
|
124
|
-
- test/test_application_convert_argumentparser.rb
|
125
|
-
- test/test_configure.rb
|
126
|
-
- test/test_kanoko.rb
|
127
118
|
homepage: https://github.com/spice-life/kanoko
|
128
119
|
licenses:
|
129
120
|
- MIT
|
@@ -148,12 +139,4 @@ rubygems_version: 2.4.5
|
|
148
139
|
signing_key:
|
149
140
|
specification_version: 4
|
150
141
|
summary: kanoko is a active image generater library.
|
151
|
-
test_files:
|
152
|
-
- test/helper.rb
|
153
|
-
- test/resize.jpg
|
154
|
-
- test/resize_and_crop.jpg
|
155
|
-
- test/src.jpg
|
156
|
-
- test/test_application_convert.rb
|
157
|
-
- test/test_application_convert_argumentparser.rb
|
158
|
-
- test/test_configure.rb
|
159
|
-
- test/test_kanoko.rb
|
142
|
+
test_files: []
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'kanoko/application/convert/function'
|
2
|
-
|
3
|
-
module Kanoko
|
4
|
-
module Application
|
5
|
-
class Convert
|
6
|
-
class ArgumentParser
|
7
|
-
include Enumerable
|
8
|
-
extend Forwardable
|
9
|
-
def_delegators :@args, :each, :to_a
|
10
|
-
|
11
|
-
attr_reader :options
|
12
|
-
|
13
|
-
def initialize(args)
|
14
|
-
@args, @options = ArgumentParser.parse args
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_path
|
18
|
-
map { |i| "/#{i.join('/')}" }.join
|
19
|
-
end
|
20
|
-
alias path to_path
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
class << self
|
25
|
-
def parse(path)
|
26
|
-
args = []
|
27
|
-
options = []
|
28
|
-
items = path.split('/')
|
29
|
-
while item = items.shift
|
30
|
-
id = item.to_sym
|
31
|
-
if Function.list.include?(id)
|
32
|
-
function = Function.new
|
33
|
-
method = function.method(id)
|
34
|
-
arg = items.shift(method.arity)
|
35
|
-
options.concat method.call(*arg)
|
36
|
-
args.push [id, *arg]
|
37
|
-
end
|
38
|
-
end
|
39
|
-
[args, options]
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
data/test/helper.rb
DELETED
data/test/resize.jpg
DELETED
Binary file
|
data/test/resize_and_crop.jpg
DELETED
Binary file
|
data/test/src.jpg
DELETED
Binary file
|
@@ -1,99 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestKanokoApplicationConvert < Minitest::Test
|
4
|
-
include Rack::Test::Methods
|
5
|
-
|
6
|
-
class TestApp < Kanoko::Application::Convert
|
7
|
-
class ResponseMock < Struct.new(:body)
|
8
|
-
end
|
9
|
-
def http_get(uri)
|
10
|
-
ResponseMock.new(File.read "test/src.jpg")
|
11
|
-
end
|
12
|
-
def after_response(res)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def app
|
17
|
-
TestApp.new
|
18
|
-
end
|
19
|
-
|
20
|
-
def setup
|
21
|
-
Kanoko.configure.digest_func = "sha1"
|
22
|
-
Kanoko.configure.secret_key = "test"
|
23
|
-
end
|
24
|
-
|
25
|
-
def assert_jpeg(expected, actual)
|
26
|
-
expected_exif = EXIFR::JPEG.new(StringIO.new(expected))
|
27
|
-
actual_exif = EXIFR::JPEG.new(StringIO.new(actual))
|
28
|
-
assert_equal expected_exif.to_hash, actual_exif.to_hash
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_resize
|
32
|
-
path = Kanoko.path_for(:resize, "10x10", "src.jpg")
|
33
|
-
get path
|
34
|
-
assert last_response.ok?
|
35
|
-
assert 0 < last_response.body.length
|
36
|
-
assert_jpeg File.read("test/resize.jpg"), last_response.body
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_resize_and_crop
|
40
|
-
path = Kanoko.path_for(:resize, "10x10", :crop, "5x5+1+1", "src.jpg")
|
41
|
-
get path
|
42
|
-
assert last_response.ok?
|
43
|
-
assert 0 < last_response.body.length
|
44
|
-
assert_jpeg File.read("test/resize_and_crop.jpg"), last_response.body
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_auto_orient
|
48
|
-
path = Kanoko.path_for(:auto_orient, "src.jpg")
|
49
|
-
get path
|
50
|
-
assert last_response.ok?
|
51
|
-
assert 0 < last_response.body.length
|
52
|
-
assert_jpeg File.read("test/src.jpg"), last_response.body
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_resize_and_auto_orient
|
56
|
-
path = Kanoko.path_for(:resize, "10x10", :auto_orient, "src.jpg")
|
57
|
-
get path
|
58
|
-
assert last_response.ok?
|
59
|
-
assert 0 < last_response.body.length
|
60
|
-
assert_jpeg File.read("test/resize.jpg"), last_response.body
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_undefined_func
|
64
|
-
path = Kanoko.path_for(:undefined, "10x10", "src.jpg")
|
65
|
-
get path
|
66
|
-
assert_equal 400, last_response.status
|
67
|
-
assert_equal "", last_response.body
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_root_not_found
|
71
|
-
path = "/"
|
72
|
-
get path
|
73
|
-
assert_equal 404, last_response.status
|
74
|
-
assert_equal "", last_response.body
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_invalid_path
|
78
|
-
path = "/invalid/path/to/src"
|
79
|
-
get path
|
80
|
-
assert_equal 400, last_response.status
|
81
|
-
assert_equal "", last_response.body
|
82
|
-
end
|
83
|
-
|
84
|
-
class Kanoko::Application::Convert::Function
|
85
|
-
def method_for_500
|
86
|
-
['-undefined-imagemagick-option']
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_500
|
91
|
-
path = Kanoko.path_for(:method_for_500, "src.jpg")
|
92
|
-
out, err = capture_subprocess_io do
|
93
|
-
get path
|
94
|
-
end
|
95
|
-
assert_equal 500, last_response.status
|
96
|
-
assert_equal "", last_response.body
|
97
|
-
assert_match /convert: unrecognized option `-undefined-imagemagick-option'/, err
|
98
|
-
end
|
99
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestKanokoApplicationConvertArgumentParser < Minitest::Test
|
4
|
-
include Kanoko::Application
|
5
|
-
class Kanoko::Application::Convert::Function
|
6
|
-
def empty_option
|
7
|
-
[]
|
8
|
-
end
|
9
|
-
def one_option
|
10
|
-
['func1']
|
11
|
-
end
|
12
|
-
def one_arg_and_one_option(arg)
|
13
|
-
['func2', arg]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_empty
|
18
|
-
a = Convert::ArgumentParser.new("")
|
19
|
-
assert_equal [], a.to_a
|
20
|
-
assert_equal [], a.options
|
21
|
-
assert_equal "", a.path
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_empty_option
|
25
|
-
a = Convert::ArgumentParser.new("empty_option/10x10")
|
26
|
-
assert_equal [[:empty_option]], a.to_a
|
27
|
-
assert_equal [], a.options
|
28
|
-
assert_equal "/empty_option", a.path
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_one_option
|
32
|
-
a = Convert::ArgumentParser.new("one_option/10x10")
|
33
|
-
assert_equal [[:one_option]], a.to_a
|
34
|
-
assert_equal "/one_option", a.path
|
35
|
-
assert_equal ['func1'], a.options
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_one_arg_and_one_option
|
39
|
-
a = Convert::ArgumentParser.new("one_arg_and_one_option/10x10")
|
40
|
-
assert_equal [[:one_arg_and_one_option, '10x10']], a.to_a
|
41
|
-
assert_equal "/one_arg_and_one_option/10x10", a.path
|
42
|
-
assert_equal ['func2', '10x10'], a.options
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_one_option_and_one_arg_and_one_option
|
46
|
-
a = Convert::ArgumentParser.new("one_option/one_arg_and_one_option/10x10")
|
47
|
-
assert_equal [[:one_option], [:one_arg_and_one_option, '10x10']], a.to_a
|
48
|
-
assert_equal "/one_option/one_arg_and_one_option/10x10", a.path
|
49
|
-
assert_equal ['func1', 'func2', '10x10'], a.options
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_nothing
|
53
|
-
a = Convert::ArgumentParser.new("nothing/10x10")
|
54
|
-
assert_equal [], a.to_a
|
55
|
-
assert_equal "", a.path
|
56
|
-
assert_equal [], a.options
|
57
|
-
end
|
58
|
-
end
|
data/test/test_configure.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestKanokoConfigure < Minitest::Test
|
4
|
-
def setup
|
5
|
-
@config = Kanoko::Configure.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_hash_proc_by_default_error
|
9
|
-
@config.digest_func = nil
|
10
|
-
@config.secret_key = nil
|
11
|
-
assert_raises(Kanoko::ConfigureError){ @config.hash_proc.call }
|
12
|
-
|
13
|
-
@config.digest_func = "sha1"
|
14
|
-
@config.secret_key = nil
|
15
|
-
assert_raises(Kanoko::ConfigureError){ @config.hash_proc.call }
|
16
|
-
|
17
|
-
@config.digest_func = nil
|
18
|
-
@config.secret_key = "test"
|
19
|
-
assert_raises(Kanoko::ConfigureError){ @config.hash_proc.call }
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_hash_proc_by_default
|
23
|
-
@config.digest_func = "sha1"
|
24
|
-
@config.secret_key = "test"
|
25
|
-
assert_equal "yrYrwA2D_XJwEyaWOr3S8GPWtd8=", @config.hash_proc.call("aaa")
|
26
|
-
end
|
27
|
-
end
|
data/test/test_kanoko.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestKanoko < Minitest::Test
|
4
|
-
def setup
|
5
|
-
Kanoko.configure.digest_func = "sha1"
|
6
|
-
Kanoko.configure.secret_key = "test"
|
7
|
-
end
|
8
|
-
|
9
|
-
def change_hash_proc(hash_proc)
|
10
|
-
before = Kanoko.configure.hash_proc
|
11
|
-
Kanoko.configure.hash_proc = hash_proc
|
12
|
-
yield
|
13
|
-
Kanoko.configure.hash_proc = before
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_configure
|
17
|
-
assert_kind_of Kanoko::Configure, Kanoko.configure
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_configure_with_block
|
21
|
-
assert_equal "sha1", Kanoko.configure.digest_func
|
22
|
-
Kanoko.configure.digest_func = "ok"
|
23
|
-
assert_equal "ok", Kanoko.configure.digest_func
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_path_for_with_default_hash_proc
|
27
|
-
change_hash_proc(proc{ "aaa" }) do
|
28
|
-
path = Kanoko.path_for(:test_func, "test_args", "test_path")
|
29
|
-
assert_equal "/aaa/test_func/test_args/test_path", path
|
30
|
-
|
31
|
-
path = Kanoko.path_for(:test_func, "/?-_=!@#<>\\", "/?-_=!@#<>\\")
|
32
|
-
assert_equal "/aaa/test_func/%2F%3F-_%3D%21%40%23%3C%3E%5C//?-_=!@#<>\\", path
|
33
|
-
|
34
|
-
path = Kanoko.path_for(:test_func, "test_args", :test_func2, "test_args2", "test_path")
|
35
|
-
assert_equal "/aaa/test_func/test_args/test_func2/test_args2/test_path", path
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_make_hash
|
40
|
-
change_hash_proc(proc{ "bbb" }) do
|
41
|
-
assert_equal "bbb", Kanoko.make_hash(:test_func, "test_args", "test_path")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_make_hash_custom_hash_proc
|
46
|
-
change_hash_proc(proc{ "ccc" }) do
|
47
|
-
assert_equal "ccc", Kanoko.make_hash(:test_func, "test_args", "test_path")
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|