ko1 / simplepaste

SimplePaste web application

This URL has Read+Write access

simplepaste / bin / fpc.rb
100644 110 lines (90 sloc) 2.204 kb
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
105
106
107
108
109
110
#!/usr/bin/ruby
# fpc.rb
 
FilePasteURL = 'http://www.atdot.net/fp/commit/auto'
 
#####################################################################
require 'optparse'
require 'net/http'
require 'uri'
 
auth_id = nil
auth_passwd = nil
passwd = nil
comment = nil
name = ENV['USER']
 
opt = OptionParser.new{|o|
  o.banner = "Usage: #{$0} [options] files..."
  o.separator ''
 
  o.on('--set-auth [ID:Password]', "Specify Download ID and Passowrd."){|a|
    auth_id, auth_passwd = a.split(/:/)
  }
  o.on('--name [NAME]', "Uploader name."){|n|
    name = n
  }
  o.on('--comment [COMMENT]', "Comment of Files."){|c|
    comment = c
  }
  o.on('--password [Password]', "Upload password."){|pass|
    passwd = pass
  }
  o.on('-h', '--help', 'Show this help.'){
    puts o
    exit
  }
}
 
opt.parse!(ARGV)
if ARGV.empty?
  puts opt
  exit 1
end
 
Net::HTTP.version_1_2
 
def upload uri, files, rest = {}
  uri = URI.parse(uri)
 
  if block_given?
    boundary = yield
  else
    boundary = '--------------------------' +
    Time.now.to_i.to_s(36) + '--' + rand(10000).to_s(36)
  end
 
  data = []
 
  files.each{|file|
    data << '--' + boundary
    data << %Q(content-disposition: form-data; name="paste_body"; filename="#{file}")
    data << 'content-type: application/octet-stream'
    data << ''
    data << open(file, 'rb'){|f| f.read}
    data << "--#{boundary}"
  }
 
  # data.pop
  rest.each{|k, v|
    next unless v
    data << "content-disposition: form-data; name=\"#{k}\""
    data << ''
    data << v
    data << "--#{boundary}"
  }
 
  data.last << '--' #
 
  #puts data.join("\n")
  #exit
  Net::HTTP.start(uri.host){|http|
    header = {
      'content-type' => "multipart/form-data; boundary=#{boundary}"
    }
    res = http.post(uri.path, data.join("\r\n"), header)
    res.body
  }
end
 
uri = upload(FilePasteURL, ARGV, {
  :auth_id => auth_id,
  :auth_passwd => auth_passwd,
  :user_name => name,
  :user_comment => comment,
  :user_password => passwd,
})
 
# for Windows
if uri
  if uri.map.size > 1
    puts uri
  else
    puts uri
    system "cmd /c start #{uri}"
  end
end