-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathline_service_upload.rb
68 lines (58 loc) · 1.71 KB
/
line_service_upload.rb
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
$:.push('.')
require 'json'
require 'net/http'
require 'fileutils'
class LineServiceUpload
LINE_UPLOAD_URI = URI.parse("https://obs.line-apps.com:443/talk/m/upload.nhn")
DELIMITER = "----wpYGYCMYYCW--"
class OBSParam
attr_accessor :ver, :oid, :type, :name, :size, :range
def to_json
{'ver' => @ver, 'oid' => @oid, 'type' => @type, 'name' => @name, 'size' => @size}.to_json
# 'range' => @range
end
end
def initialize(token, client)
@token = token
@client = client
end
def create_header(id, filename, size)
obs = OBSParam.new
obs.ver = "1.0"
obs.oid = id.to_s
obs.type = "image"
obs.name = filename
#obs.range = "bytes 0-#{size - 1}/#{size}"
obs.size = size
puts obs.to_json
header = "\r\n--#{DELIMITER}\r\nContent-Disposition: form-data; name=\"params\"\r\n\r\n#{obs.to_json}\r\n"
header += "\r\n--#{DELIMITER}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"#{filename}\"\r\nContent-Type: application/octet-stream\r\n\r\n"
return header
end
def create_footer
return "\r\n--#{DELIMITER}--\r\n"
end
def create_data(id, filename)
image = open(filename, "rb") {|io| io.read}
data = create_header(id, File.basename(filename), image.length)
data += image
data += create_footer
return data
end
def upload(id, filename)
puts "Start upload"
http = Net::HTTP.new(LINE_UPLOAD_URI.host, LINE_UPLOAD_URI.port)
res = http.start do |s|
begin
header = {"Content-Type" => "multipart/form-data; boundary=#{DELIMITER}",
"X-Line-Access" => @token,
"X-Line-Application" => @client,
"User-Agent" => @client}
data = create_data(id, filename)
s.post(LINE_UPLOAD_URI.path, data, header)
rescue Exception => e
p e
end
end
end
end