Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active August 29, 2015 14:20
Show Gist options
  • Save cowboy/c6a326cd2ac67a183bb0 to your computer and use it in GitHub Desktop.
Save cowboy/c6a326cd2ac67a183bb0 to your computer and use it in GitHub Desktop.
Latest Gyazo.app, modified to upload to my server.
#!/usr/bin/env ruby
require 'net/http'
# 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 -io \"#{tmpfile}\""
if File.exist?(tmpfile) then
system "~/.dotfiles/bin/resample-dpi \"#{tmpfile}\""
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 = 'benalman.com'
CGI = '/code/php/gyazo.php'
UA = 'Gyazo/1.0'
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)
url = res.response.body
IO.popen("pbcopy","r+"){|io|
io.write url
io.close
}
system "open #{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
}
@cowboy
Copy link
Author

cowboy commented Apr 27, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment