-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
71 lines (63 loc) · 1.55 KB
/
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
require 'sinatra'
require 'bundler/setup'
require 'erb'
require 'model'
require 'logger'
require 'json'
class FurueruApp < Sinatra::Base
set :root, File.dirname(__FILE__)
set :show_exceptions, false
set :logging, true
helpers do
alias_method :h, :escape_html
def uri_encode(str)
URI.encode str
end
end
error do
status 500
Model.logger.warn request.env['sinatra.error'].message
'error...'
end
get '/' do
erb :index
end
post '/' do
if request.content_length.to_i > 1024 * 30
Model.logger.warn "file size: #{request.content_length}"
halt 500, "Capacity Over"
end
if params.empty? || params[:upfile].empty?
halt 500, "File is Empty"
end
file = params[:upfile]
type = file[:type]
Model.logger.info "file type: #{type}"
halt 500, "Unsupport File" unless ["png", "x-png", "gif", "jpeg", "pjpeg"].map{|s|
"image/".concat(s)
}.include? type
dst = Model::Image.save_file(file[:tempfile], File.basename(file[:filename]))
JSON.unparse({
:filename => dst,
:type => file[:type]
})
end
post '/furueru' do
data = {}
data[:image] = Model::Image.vibrate(
params[:src],
params[:width].to_i,
params[:delay].to_i
)
content_type :json
JSON.unparse(data)
end
post '/download' do
src = "public/#{params[:src]}"
dst = "public/history/#{File.basename params[:src]}"
FileUtils.cp src, dst
Model.logger.info "copy file #{src} to #{dst}"
content_type :gif
send_file dst
end
end