Guest User

Untitled

a guest
Dec 1st, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'net/http'
  3.  
  4. HOST = 'gyazo.com'
  5. CGI = '/upload.cgi'
  6. UA = 'Gyazo/1.0'
  7. USER = nil
  8. PASS = nil
  9.  
  10. # get id
  11. user = IO.popen("whoami", "r+").gets.chomp
  12. program = ARGV[0].to_s
  13. idfile = "/Users/#{user}/Library/Gyazo/id"
  14. old_idfile = File.dirname(program) + "/gyazo.app/Contents/Resources/id"
  15.  
  16. id = ''
  17. if File.exist?(idfile) then
  18. id = File.read(idfile).chomp
  19. elsif File.exist?(old_idfile) then
  20. id = File.read(old_idfile).chomp
  21. end
  22.  
  23. # capture png file
  24. tmpfile = "/tmp/image_upload#{$$}.png"
  25. imagefile = ARGV[1]
  26.  
  27. if imagefile && File.exist?(imagefile) then
  28. system "sips -s format png \"#{imagefile}\" --out \"#{tmpfile}\""
  29. else
  30. system "screencapture -i \"#{tmpfile}\""
  31. if File.exist?(tmpfile) then
  32. system "sips -d profile --deleteColorManagementProperties \"#{tmpfile}\""
  33. end
  34. end
  35.  
  36. if !File.exist?(tmpfile) then
  37. exit
  38. end
  39.  
  40. imagedata = File.read(tmpfile)
  41. File.delete(tmpfile)
  42.  
  43. # upload
  44. boundary = '----BOUNDARYBOUNDARY----'
  45.  
  46. data = <<EOF
  47. --#{boundary}\r
  48. content-disposition: form-data; name="id"\r
  49. \r
  50. #{id}\r
  51. --#{boundary}\r
  52. content-disposition: form-data; name="imagedata"; filename="gyazo.com"\r
  53. \r
  54. #{imagedata}\r
  55. --#{boundary}--\r
  56. EOF
  57.  
  58. header ={
  59. 'Content-Length' => data.length.to_s,
  60. 'Content-type' => "multipart/form-data; boundary=#{boundary}",
  61. 'User-Agent' => UA
  62. }
  63.  
  64. Net::HTTP.start(HOST,80){|http|
  65. req = Net::HTTP::Post.new CGI, header
  66. req.basic_auth USER, PASS if USER && PASS
  67.  
  68. res = http.request req, data
  69. url = res.response.to_ary[1]
  70. system "echo -n #{url} | pbcopy"
  71. system "open #{url}"
  72.  
  73. # save id
  74. newid = res.response['X-Gyazo-Id']
  75. if newid and newid != "" then
  76. if !File.exist?(File.dirname(idfile)) then
  77. Dir.mkdir(File.dirname(idfile))
  78. end
  79. if File.exist?(idfile) then
  80. File.rename(idfile, idfile+Time.new.strftime("_%Y%m%d%H%M%S.bak"))
  81. end
  82. File.open(idfile,"w").print(newid)
  83. if File.exist?(old_idfile) then
  84. File.delete(old_idfile)
  85. end
  86. end
  87. }
Add Comment
Please, Sign In to add comment