-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_app.rb
executable file
·283 lines (233 loc) · 7.4 KB
/
image_app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env ruby
require 'tilt/string'
require 'rack'
require 'open-uri'
require 'rack/singleshot'
require 'optparse'
require 'stringex/string_extensions'
require 'stringex/localization'
require 'stringex/unidecoder'
require 'cgi'
class ImageApp
attr_reader :template, :counter, :download_dir, :annotate_script
def initialize(opts = {})
@template = Tilt::StringTemplate.new { ::DATA.read }
@download_dir = opts.fetch(:download_dir)
@annotate_script = opts.fetch(:annotate_script)
end
def call(env)
request = Rack::Request.new(env)
response = Rack::Response.new
unless request.path_info == '/'
response.status = 404
return response.finish
end
if request.get?
locals = { 'tags' => nil, 'a' => nil, 'd' => nil, 's' => nil, 't' => nil, 'i' => nil, 'w' => nil, 'h' => nil}.merge(request.GET)
locals['uri'] = uri(request)
set_ratio(locals)
if locals['t'] && !locals['t'].empty?
locals['key'] = locals['t'].downcase.strip.gsub(/\s+/,"_").gsub(/\W+/,'')
else
locals['key'] = 'image'
end
# escape and encode everything!
%w{tags a d s t i w h}.each do |key|
locals[key] = escape(locals[key].to_s) if locals[key]
end
response.write template.render(self, locals)
elsif request.post?
post = request.POST
# just nuke bad params!
post.delete_if { |k, v| v.nil? or v.empty? }
src = post['i']
return bad_request("No image\n#{post.inspect}") unless src
begin
image = open(src,
"User-Agent" => request.env['HTTP_USER_AGENT'],
"Referer" => post['s'] || src,
"Accept-Encoding" => 'identity'
)
img = image.read
rescue OpenURI::HTTPError => e
bad_request(image.read)
end
command = build_command(annotate_script, src, post)
success, err = spawn(command, img, download_dir)
return bad_request("#{src}\n#{err}") unless success
response.write('<html><body><script>window.close();</script></body></html>')
else
response.status = 405
end
response.finish
rescue Exception => e
bad_request("#{e.class} - #{e.inspect} - #{e.backtrace.join("\n")}")
end
def escape(string)
string = Stringex::Unidecoder.decode(string)
string = CGI.escapeHTML(string)
end
def build_command(exec, src, post)
command = [exec]
# set source from the image path
post['s'] ||= src
command = [annotate_script]
%w{a d s t}.each do |param|
if post.has_key?(param)
command << "-#{param}"
command << post[param]
end
end
if post.has_key?('Tags')
post['Tags'].split(',').map(&:to_s).each do |tag|
tag.strip!
command << '-T' << tag unless tag.empty?
end
end
command << '-n' # no gui
command << '-o'
command << filename(src, post['key'] || 'image')
command
end
def spawn(command, image, directory)
r, w = IO.pipe
re, we = IO.pipe
pid = Process.spawn(*command, :in => r, :out => we, :err => we, :chdir => directory)
r.close
we.close
w.write(image)
w.close
err = re.read
pid, status = Process.waitpid2(pid)
if status.exitstatus == 0
[true, nil]
else
[false, err]
end
end
def bad_request(reason="Bad request")
response = Rack::Response.new
response.status = 400
response.write(reason)
response.finish
end
def filename(imagename, key='image')
ext = File.extname(imagename).gsub(/\?\d+$/,'')
date = Time.now.strftime('%Y%m%d_%H%M%S')
type = "I1.3"
key = "#{key}#{ext}"
[date, type, key].join("-")
end
def set_ratio(locals)
locals['h'] = locals['h'].to_i
locals['w'] = locals['w'].to_i
if locals['w'] > 0 and locals['h'] > 0
if locals['w'] > locals['h']
locals['wi'] = 200
locals['hi'] = ((200.to_f/locals['w']) * locals['h']).to_i
else
locals['hi'] = 200
locals['wi'] = ((200.to_f/locals['h']) * locals['w']).to_i
end
else
locals['hi'] = locals['wi'] = 200
end
end
def ratio(size, width, height)
end
# Generates the absolute URI for a given path in the app.
# Takes Rack routers and reverse proxies into account.
def uri(request, addr = nil)
uri = [host = ""]
host << "http#{'s' if request.ssl?}://"
if request.port != (request.ssl? ? 443 : 80)
host << request.host_with_port
else
host << request.host
end
uri << request.script_name.to_s
uri << (addr ? addr : request.path_info).to_s
File.join uri
end
end
Options = Struct.new(:download_dir, :annotate_script, :server, :port, :host)
class Parser
def self.parse(options)
# set up defaults
args = Options.new(
ENV['DOWNLOAD_DIR'] || File.join(ENV['HOME'], 'Downloads'),
ENV['ANNOTATE_SCRIPT'] || 'image_annotate.py',
false,
8765,
'127.0.0.1'
)
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.on("-dDOWNLOAD_DIR", "--download-dir=DOWNLOAD_DIR", "Directory to save files too (defaults to ~/Downloads)") do |d|
args.download_dir = d
end
opts.on("-aANNOTATE_SCRIPT", "--annotate-script=ANNOTATE_SCRIPT", "Location of the annotation script (defaults to assuming the script is on the path)") do |a|
args.annotate_script = a
end
opts.on("-s","--server", "Run as a persistant server, not a one shot process") do
args.server = true
end
opts.on("-pPORT", "--port=PORT", "Port for persistant server to listen on (Defaults to 8765)") do |port|
args.port = port.to_i
end
opts.on("-HHOST", "--host=HOST", "Host for persistant server to listen to (Defaults to localhost)") do |host|
args.host = host
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end
opt_parser.parse!(options)
return args
end
end
if $0 == __FILE__
options = Parser.parse ARGV
handler, handler_opts = nil, {}
builder = Rack::Builder.new
builder.run ImageApp.new(download_dir: options.download_dir, annotate_script: options.annotate_script)
if options.server
handler = Rack::Handler.get('webrick')
handler_opts = {Port: options.port, Host: options.host}
else
handler = Rack::Handler.get('singleshot')
end
handler.run builder, handler_opts do |server|
[:INT, :TERM].each { |sig| trap(sig) { server.respond_to?(:stop!) ? server.stop! : server.stop } }
end
end
__END__
<html>
<head>
<title>Image Server</title>
<style type='text/css'>
label {
display: inline-block;
width: 20%;
}
input[type=text] {
width: 75%;
}
</style>
</head>
<body>
<img src='#{i}' width='#{wi}' height='#{hi}' />
<p>#{w}x#{h}</p>
<form method='POST' action='#{uri}'>
<label for='i'>Image:</label><input type='text' value='#{i}' name='i' /><br />
<label for='t'>Title:</label><input type='text' value='#{t}' name='t' /><br />
<label for='a'>Artist:</label><input type='text' value='#{a}' name='a' /><br />
<label for='d'>Desc:</label><input type='text' value='#{d}' name='d' size='2000' /><br />
<label for='s'>Source:</label><input type='text' value='#{s}' name='s' /><br />
<label for='Tags'>Tags:</label><input type='text' value='#{tags || nil}' name='Tags' /><br />
<label for='key'>Key:</label><input type='text' value='#{key}' name='key' /><br />
<input type='submit' value='Save'/>
</form>
</body>
</html>