3 require 'active_support/all'
\r
4 require 'rest_client'
\r
10 puts "uploader.rb host dirname license_name email token"
\r
11 puts "upload on child dirs"
\r
22 def initialize host, dirname, lsname, email, auth
\r
28 @licenses = self.licenses
\r
29 @license_groups = self.license_groups
\r
34 u = @host + 'licenses.json'
\r
35 r = RestClient.get(u, {email: @email, token: @auth})
\r
37 ary['list'].each do |l|
\r
45 u = @host + 'license_groups.json'
\r
46 r = RestClient.get(u, {email: @email, token: @auth})
\r
48 ary['list'].each do |l|
\r
49 res[l['id'].to_i] = l
\r
55 Dir.glob File.expand_path(@dirname) + '/*' do |fn|
\r
56 ext = File.extname(fn).downcase
\r
57 bn = File.basename(fn, ".*")
\r
59 when '.png', '.gif', '.jpeg'
\r
60 custom_attr_file = File.dirname(fn) + '/' + bn + '.json'
\r
61 attr = JSON.parse(File.open(custom_attr_file).read)
\r
69 b = Base64.encode64(File.open(fn, 'rb').read)
\r
70 u = @host + 'original_pictures'
\r
72 r = RestClient.post(u,
\r
73 {:original_picture => {:file => b}}.to_json,
\r
74 :content_type => :json, :accept => :json, :email => @email, :token => @auth
\r
79 puts ' failed: ' + r.to_s
\r
84 def original_picture_license oid, lgid, lsid, en, attr
\r
86 u = @host + '/original_picture_licenses'
\r
88 :original_picture_id => oid,
\r
89 :license_group_id => lgid,
\r
90 :license_id => lsid,
\r
91 :license_group_module_name => en,
\r
92 :license_settings => attr,
\r
95 r = RestClient.post(u,
\r
97 :content_type => :json, :accept => :json, :email => @email, :token => @auth
\r
102 puts ' failed: ' + u.to_s
\r
109 def publish oid, lgid, rp
\r
111 u = @host + 'resource_pictures'
\r
113 :original_picture_id => oid,
\r
114 :original_picture_license_group => {
\r
115 :original_picture_id => oid, :license_group_id => lgid
\r
117 :resource_picture => rp,
\r
120 r = RestClient.post(u,
\r
122 :content_type => :json, :accept => :json, :email => @email, :token => @auth
\r
126 puts ' failed: ' + r.to_s
\r
132 each_op do |imgfile, attr|
\r
133 puts 'uploading: ' + imgfile
\r
134 oid = self.upload imgfile
\r
136 puts 'success: ' + oid.to_s
\r
137 ls = @licenses[@lsname]
\r
139 puts 'not detect licenses: ' + @lsname.to_s
\r
143 lgid = ls['license_group_id'].to_i
\r
144 lg = @license_groups[lgid]
\r
145 en = lg['module_name']
\r
146 confirm = self.original_picture_license oid, lgid, lsid, en, attr
\r
147 puts 'publishing: ' + @lsname.to_s
\r
148 r = self.publish oid, lgid, confirm
\r
149 puts 'success: ' + r['id'].to_s
\r
155 u = Uploader.new host, dirname, lsname, email, auth
\r