-
Notifications
You must be signed in to change notification settings - Fork 0
/
GyazoController.rb
104 lines (89 loc) · 2.96 KB
/
GyazoController.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
#
# GyazoController.rb
# SuperIME
#
# Created by 中園 翔 on 2012/12/17.
# Copyright 2012年 nikezono.net. All rights reserved.
#
# Gyazoからブラウザをopenする処理を省いたもの
# URLをテキストボックスにinsertする処理が追加されている
# http://gyazo.com
require 'SuperIMEController'
class GyazoController
def Gyazo
# get id
user = IO.popen("whoami", "r+").gets.chomp
program = ARGV[0].to_s
idfile = "/Users/#{user}/Library/Gyazo/id"
old_idfile = File.dirname(program) + "/gyazo.app/Contents/Resources/id"
id = ''
if File.exist?(idfile) then
id = File.read(idfile).chomp
elsif File.exist?(old_idfile) then
id = File.read(old_idfile).chomp
end
# capture png file
tmpfile = "/tmp/image_upload#{$$}.png"
imagefile = ARGV[1]
if imagefile && File.exist?(imagefile) then
system "sips -s format png \"#{imagefile}\" --out \"#{tmpfile}\""
else
system "screencapture -i \"#{tmpfile}\""
if File.exist?(tmpfile) then
system "sips -d profile --deleteColorManagementProperties \"#{tmpfile}\""
end
end
if !File.exist?(tmpfile) then
exit
end
imagedata = File.read(tmpfile)
File.delete(tmpfile)
# upload
boundary = '----BOUNDARYBOUNDARY----'
host = 'gyazo.com'
cgi = '/upload.cgi'
ua = 'Gyazo/1.0.1'
data = <<EOF
--#{boundary}\r
content-disposition: form-data; name="id"\r
\r
#{id}\r
--#{boundary}\r
content-disposition: form-data; name="imagedata"; filename="gyazo.com"\r
\r
#{imagedata}\r
--#{boundary}--\r
EOF
header ={
'Content-Length' => data.length.to_s,
'Content-type' => "multipart/form-data; boundary=#{boundary}",
'User-Agent' => ua
}
Net::HTTP.start(host,80){|http|
res = http.post(cgi,data,header)
#puts "cgi:#{cgi} data:#{data} header:#{header}"
url = res.response.body
IO.popen("pbcopy","r+"){|io|
io.write url
io.close
}
#system "open #{url}"
ime = SuperIMEController.new
ime.insert(url)
# save id
newid = res.response['X-Gyazo-Id']
if newid and newid != "" then
if !File.exist?(File.dirname(idfile)) then
Dir.mkdir(File.dirname(idfile))
end
if File.exist?(idfile) then
File.rename(idfile, idfile+Time.new.strftime("_%Y%m%d%H%M%S.bak"))
end
File.open(idfile,"w").print(newid)
if File.exist?(old_idfile) then
File.delete(old_idfile)
end
end
}
end
end