Guest User

Untitled

a guest
Jun 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # Settings
  4. browser_cmd = 'firefox'
  5. clipboard_cmd = 'xclip'
  6. proxy_addr = nil
  7. proxy_port = nil
  8.  
  9. require 'net/http'
  10.  
  11. idfile = ENV['HOME'] + "/.gyazo.id"
  12.  
  13. if (`which import`).strip.size == 0
  14. $stderr.puts "Please install imagemagick to use gyazo utility"
  15. Process.exit
  16. end
  17.  
  18. id = ''
  19. if File.exist?(idfile) then
  20. id = File.read(idfile).chomp
  21. else
  22. id = Time.new.strftime("%Y%m%d%H%M%S")
  23. File.open(idfile,"w").print(id+"\n")
  24. end
  25.  
  26. tmpfile = "/tmp/image_upload#{$$}.png"
  27. imagefile = ARGV[0]
  28.  
  29. if imagefile && File.exist?(imagefile) then
  30. system "convert #{imagefile} #{tmpfile}"
  31. else
  32. system "import #{tmpfile}"
  33. end
  34.  
  35. imagedata = File.read(tmpfile)
  36. File.delete(tmpfile)
  37.  
  38. boundary = '----BOUNDARYBOUNDARY----'
  39.  
  40. HOST = 'gyazo.com'
  41. CGI = '/upload.cgi'
  42.  
  43. data = <<EOF
  44. --#{boundary}\r
  45. content-disposition: form-data; name="id"\r
  46. \r
  47. #{id}\r
  48. --#{boundary}\r
  49. content-disposition: form-data; name="imagedata"\r
  50. \r
  51. #{imagedata}\r
  52. \r
  53. --#{boundary}--\r
  54. EOF
  55.  
  56. header ={
  57. 'Content-Length' => data.length.to_s,
  58. 'Content-type' => "multipart/form-data; boundary=#{boundary}"
  59. }
  60.  
  61. Net::HTTP::Proxy(proxy_addr, proxy_port).start(HOST,80) {|http|
  62. res = http.post(CGI,data,header)
  63. url = res.response.to_ary[1]
  64. puts url
  65. if system "which #{clipboard_cmd} >/dev/null 2>&1" then
  66. system "echo #{url} | #{clipboard_cmd}"
  67. end
  68. system "#{browser_cmd} #{url}"
  69. }
Add Comment
Please, Sign In to add comment