-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcroppi.rb
63 lines (48 loc) · 1.5 KB
/
croppi.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
#!/usr/local/bin/ruby
require 'rubygems'
require 'sinatra'
require 'base64'
require 'kconv'
require 'net/http'
require 'uri'
#require 'sinatra/base'
#require 'sinatra/reloader' if development?
before do
if request.path_info == '/' && params[:url] then
uri = URI.parse(URI.encode(params[:url]))
begin
Net::HTTP.start(uri.host) {|http|
res = http.request_get(uri.path)
redirect "/" unless res['content-type'].include?("image/")
@api_dataURL = "data:" + res['content-type'] + ";base64," + Base64.encode64(res.body).gsub(/\n/,"")
}
@api_fileName = File.basename(uri.path).tosjis
rescue
redirect '/'
end
end
end
get '/' do
erb :index
end
post '/' do
redirect "/" unless params[:imagedata] && params[:filename]
#dataURIをバイナリに変換
imagedata = Base64.decode64(params[:imagedata].gsub(/^data:image\/png;base64,/, ""))
#拡張子を除いたファイル名取得し、_c.pngを付け足す。日本語ファイル名文字化け対策にtosjis。
filename = (File.basename(params[:filename], ".*") + "_c.png").tosjis
#tmp/imagesフォルダに保存
open("tmp/#{filename}","wb") do |fh|
fh.write imagedata
end
#?秒後に削除。
Thread.new do
sleep(5);
File.delete("tmp/#{filename}")
end
#send_fileは内部でhaltするので最後に書く。
send_file("tmp/#{filename}", :disposition => "attachment")
end
not_found do
redirect '/'
end