kanoko 0.2.0 → 0.3.0

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: c3e0d030893084af61384d4d8b6348a42ccc9a80
4
- data.tar.gz: 826a04fc42395cf02695038c8af3be399fa2de09
3
+ metadata.gz: 1c8ae72b9c1732baa61ae18d0454a30d2a246fcd
4
+ data.tar.gz: 2e1122b7b11dce472368b5e252a25a44aeef57b5
5
5
  SHA512:
6
- metadata.gz: f4c817ced22e73cc3f6bfbaea398a27e7aaf2ae3c09bedb3f972e642340adaf7ab5a61bbd68b38ff26149f766904dc14603697e1bde79f2b3ef839a582e95c97
7
- data.tar.gz: b39a548d60bac3f6abd22af3ab7a987c067245679c7f90ebd84c4c6a62e27d36f866e0b8b8073c17cb6d0f8eb2361bb8933abf911289b2c8c15d65307549db74
6
+ metadata.gz: e8ecc2d16be738fa47c45e2c55c544a5aca200d2184b023bea55ef17441df0eafcf817e701759189c3a27f0b9dcf1f07523ed8a4cc6e5b1e9d1ddda864e8dae1
7
+ data.tar.gz: 3e56068dc8148d8b230bf768b08549561b0da67328a1065d1ed2c011d1dc99cc4f4272bd0be280d9b27f8011b4b70c5281c8cc8c55b95bd6b3a9bbc15bd2e9dc
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.6
4
- - 2.2.2
3
+ - 2.1.10
4
+ - 2.2.4
5
+ - 2.3.0
@@ -15,7 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
16
  spec.require_paths = ["lib"]
17
17
 
18
- spec.add_dependency "sinatra"
18
+ spec.add_development_dependency "sinatra"
19
+ spec.add_development_dependency "mime-types"
19
20
  spec.add_development_dependency "bundler"
20
21
  spec.add_development_dependency "rake"
21
22
  spec.add_development_dependency "minitest"
@@ -24,7 +24,7 @@ module Kanoko
24
24
 
25
25
  def path_for(*function, src)
26
26
  hash = make_hash(*function, src)
27
- "/#{hash}/#{function.map{ |i| URI.encode_www_form_component(i) }.join('/')}/#{src}"
27
+ "/#{hash}/#{function.map { |i| URI.encode_www_form_component(i) }.join('/')}/#{src}"
28
28
  end
29
29
  module_function :path_for
30
30
 
@@ -2,6 +2,7 @@ require 'sinatra'
2
2
  require 'net/http'
3
3
  require 'tempfile'
4
4
  require 'kanoko'
5
+ require 'mime/types'
5
6
 
6
7
  # This is an experimental implementation.
7
8
  # You can set configure and other.
@@ -33,6 +34,18 @@ module Kanoko
33
34
  class Convert < Sinatra::Application
34
35
  require 'kanoko/application/convert/function'
35
36
 
37
+ IMAGE_TYPES = MIME::Types.select do |m|
38
+ m.media_type == 'image'
39
+ end
40
+ TYPE_MAP = IMAGE_TYPES.map { |i|
41
+ [i.to_s, i.preferred_extension]
42
+ }.to_h
43
+ EXT_MAP = IMAGE_TYPES.each_with_object({}) do |i, h|
44
+ i.extensions.each do |ext|
45
+ h[ext] = i.to_s
46
+ end
47
+ end
48
+
36
49
  # /123abc456def=/resize/200x200/crop/100x100/path/to/src
37
50
  get '/:hash/*' do
38
51
  # REQUEST_URI dependent on unicorn.
@@ -48,13 +61,16 @@ module Kanoko
48
61
  list = Kanoko::Application::Convert::Function.list
49
62
  convert_options = []
50
63
  arguments = []
51
-
64
+ to_ext = File.extname(request_params.last)[1..-1]
52
65
  while id = request_params.shift.to_sym
53
- if list.include?(id)
66
+ if id == :to
67
+ to_ext = request_params.shift
68
+ arguments << id << to_ext
69
+ elsif list.include?(id)
54
70
  arguments << id
55
71
  method = Function.new.method(id)
56
72
  arg = request_params.shift(method.arity)
57
- arg.map!{|i| URI.decode_www_form_component i}
73
+ arg.map! { |i| URI.decode_www_form_component i }
58
74
  arguments.concat arg if 0 < arg.length
59
75
  convert_options.concat method.call(*arg)
60
76
  else
@@ -63,7 +79,7 @@ module Kanoko
63
79
  end
64
80
  end
65
81
 
66
- check_path = request_params.map{ |i| URI.decode_www_form_component(i) }.join('/')
82
+ check_path = request_params.map { |i| URI.decode_www_form_component(i) }.join('/')
67
83
  unless hash == Kanoko.make_hash(*arguments, check_path)
68
84
  logger.error "hash check failed #{[*arguments, check_path]}"
69
85
  return 400
@@ -80,22 +96,35 @@ module Kanoko
80
96
  src_file.write res.body
81
97
  src_file.fdatasync
82
98
 
83
- Tempfile.create("dst") do |dst_file|
99
+ t = TYPE_MAP[res.content_type]
100
+ src_type = if t
101
+ "#{t}:"
102
+ else
103
+ ""
104
+ end
105
+
106
+ dst_name = if to_ext.nil?
107
+ "dst"
108
+ else
109
+ ["dst", ".#{to_ext}"]
110
+ end
111
+ Tempfile.create(dst_name) do |dst_file|
84
112
  system_command = [
85
- {"OMP_NUM_THREADS" => "1"},
113
+ { "OMP_NUM_THREADS" => "1" },
86
114
  'convert',
87
115
  '-depth', '8',
88
116
  convert_options,
89
- src_file.path,
90
- dst_file.path
117
+ "#{src_type}#{src_file.path}",
118
+ dst_file.path,
91
119
  ].flatten
92
120
  result = system *system_command
93
121
 
94
122
  unless result
95
- logger.error "command fail $?=#{$?.inspect}"
123
+ logger.error "command fail $?=#{$CHILD_STATUS.inspect}"
96
124
  return 500
97
125
  end
98
126
 
127
+ content_type EXT_MAP[to_ext]
99
128
  dst_file.read
100
129
  end
101
130
  end
@@ -42,7 +42,13 @@ module Kanoko
42
42
 
43
43
  def auto_orient
44
44
  [
45
- '-auto-orient'
45
+ '-auto-orient',
46
+ ]
47
+ end
48
+
49
+ def strip
50
+ [
51
+ '-strip',
46
52
  ]
47
53
  end
48
54
  end
@@ -19,16 +19,18 @@ module Kanoko
19
19
  def initialize
20
20
  @digest_func = ENV['KANOKO_DIGEST_FUNC']
21
21
  @secret_key = ENV['KANOKO_SECRET_KEY']
22
- @hash_proc = ->(*args){
22
+ @hash_proc = lambda do |*args|
23
23
  if @digest_func.nil? || @secret_key.nil?
24
24
  fail ConfigureError, "`digest_func' and `secret_key' must be set"
25
25
  end
26
26
  Base64.urlsafe_encode64(
27
- OpenSSL::HMAC.digest @digest_func,
28
- @secret_key,
29
- args.map(&:to_s).join(',')
27
+ OpenSSL::HMAC.digest(
28
+ @digest_func,
29
+ @secret_key,
30
+ args.map(&:to_s).join(','),
31
+ ),
30
32
  )
31
- }
33
+ end
32
34
  end
33
35
  end
34
36
  end
@@ -1,3 +1,3 @@
1
1
  module Kanoko
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanoko
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-11 00:00:00.000000000 Z
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -17,7 +17,21 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :runtime
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mime-types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
@@ -135,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
149
  version: '0'
136
150
  requirements: []
137
151
  rubyforge_project:
138
- rubygems_version: 2.4.5
152
+ rubygems_version: 2.5.1
139
153
  signing_key:
140
154
  specification_version: 4
141
155
  summary: kanoko is a active image generater library.