Skip to content

Instantly share code, notes, and snippets.

@benbonnet
Created May 27, 2009 08:16
Show Gist options
  • Save benbonnet/118517 to your computer and use it in GitHub Desktop.
Save benbonnet/118517 to your computer and use it in GitHub Desktop.
require 'mime/types'
api = Api.find(:first, :conditions => {:user_id => 2, :api_type_id => 11})
#file = "#{RAILS_ROOT}/public/404.html" # WRONG
#file = "#{RAILS_ROOT}/public/assets/0000/0000/20090601cover_schlemiels.jpg" # WRONG
#file = "/Users/benjaminbonnet/Desktop/today.txt" # OK
#file = "/Users/benjaminbonnet/Desktop/Owasp-rails-security.pdf" # Content-Type application/pdf is not a valid input type.
#file = "/Users/benjaminbonnet/Desktop/proxylist_us_may_22_16_08_44_d.csv"
#file = "/Users/benjaminbonnet/Desktop/Hello_doc.doc" # OK
#file = "/Users/benjaminbonnet/Desktop/Hello_doc.rtf" # WRONG
file = "/Users/benjaminbonnet/Desktop/Hello_doc.zip"
puts "Fichier : #{file}"
data = File.open(file, "r" ) {|io| io.read}
@mime_type = MIME::Types.of(file)[0].content_type
puts "Mime-type : #{@mime_type}"
boundary = "f93dcbA3"
atom_string = ""
# atom_string << "--#{boundary}\r\n"
# atom_string << "Content-Type: application/atom+xml; charset=UTF-8\r\n\r\n"
# atom_string = <<-"EOS"
# <?xml version='1.0' encoding='UTF-8'?><atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#document" label="document"/><atom:title>test.doc</atom:title></atom:entry>
# EOS
# atom_string << "\r\n--#{boundary}\r\n"
# atom_string << "Content-Type: #{@mime_type}\r\nContent-Transfer-Encoding: binary\r\n\r\n"
atom_string << data
# atom_string << "\r\n--#{boundary}--\r\n"
headers = {}
headers["POST"] = "/feeds/documents/private/full HTTP/1.1"
headers["Host"] = "docs.google.com"
headers["Authorization"] = "AuthSub token=\"#{api.token}\""
headers["Slug"] = "Hello.doc"
# headers["Content-Type"] = "multipart/related; boundary=\"#{boundary}\""
headers["Content-Length"] = "#{atom_string.length}"
headers["Content-Type"] = "#{@mime_type}"
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6"
http = Net::HTTP.new('docs.google.com')
path = "/feeds/documents/private/full"
response = http.post(path, atom_string, headers)
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment