Class: RTFS::Client
- Inherits:
-
Object
- Object
- RTFS::Client
- Defined in:
- lib/rtfs/client.rb
Instance Attribute Summary collapse
-
#access_count ⇒ Object
Returns the value of attribute access_count.
-
#appkey ⇒ Object
Returns the value of attribute appkey.
-
#nameservers ⇒ Object
Returns the value of attribute nameservers.
-
#uid ⇒ Object
Returns the value of attribute uid.
Class Method Summary collapse
-
.tfs(options) ⇒ Object
参数: :root TFS WebService Root 服务器地址,如 127.0.0.1:3100.
Instance Method Summary collapse
- #appid ⇒ Object
- #create(path, options = {}) ⇒ Object
- #del(path, options = {}) ⇒ Object
- #finger(path, options = {}) ⇒ Object
-
#get(tfs_name) ⇒ Object
获取文件.
- #get_nameservers ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
-
#put(path, options = {}) ⇒ Object
上传文件 参数: file_path 需要上传的文件路径 :ext 扩展名,默认会取 file_path 的扩展名, 如: .jpg 返回值 T1lpVcXftHXXaCwpjX.
-
#put_and_get_url(path, options = {}) ⇒ Object
上传文件 并返回完整 url (only for Taobao).
-
#rm(tname, options = {}) ⇒ Object
删除文件, 不能带扩展名.
- #save(path, options = {}) ⇒ Object
-
#stat(tname, options = {}) ⇒ Object
文件信息查看, 不能带扩展名.
- #write(path, options = {}) ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rtfs/client.rb', line 24 def initialize() @appkey = [:appkey] @ns_addr = [:ns_addr] @basedir = [:basedir] @uid = [:uid] @access_count = 0 get_nameservers if @uid.nil? && !@basedir.nil? (Digest::MD5.hexdigest(@basedir).to_i(16) % 10000) end end |
Instance Attribute Details
#access_count ⇒ Object
Returns the value of attribute access_count.
20 21 22 |
# File 'lib/rtfs/client.rb', line 20 def access_count @access_count end |
#appkey ⇒ Object
Returns the value of attribute appkey.
21 22 23 |
# File 'lib/rtfs/client.rb', line 21 def appkey @appkey end |
#nameservers ⇒ Object
Returns the value of attribute nameservers.
19 20 21 |
# File 'lib/rtfs/client.rb', line 19 def nameservers @nameservers end |
#uid ⇒ Object
Returns the value of attribute uid.
22 23 24 |
# File 'lib/rtfs/client.rb', line 22 def uid @uid end |
Class Method Details
.tfs(options) ⇒ Object
参数:
:root TFS WebService Root 服务器地址,如 127.0.0.1:3100
15 16 17 |
# File 'lib/rtfs/client.rb', line 15 def self.tfs() self.new() if end |
Instance Method Details
#appid ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/rtfs/client.rb', line 144 def appid return @appid unless @appid.nil? resp = http_get("/v2/#{appkey}/appid") if resp && resp.code == 200 @appid = JSON.parse(resp)['APP_ID'] end end |
#create(path, options = {}) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/rtfs/client.rb', line 108 def create(path, = {}) resp = http_post(furl(path), nil, :params => {:recursive => 1}) resp && resp.code == 201 end |
#del(path, options = {}) ⇒ Object
126 127 128 129 130 |
# File 'lib/rtfs/client.rb', line 126 def del(path, = {}) resp = http_delete(furl(path)) resp && resp.code == 200 end |
#finger(path, options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rtfs/client.rb', line 132 def finger(path, = {}) begin resp = http_head(furl(path)) resp && resp.code == 200 rescue RestClient::ResourceNotFound # this rescue is intended. # the purpose is to return nil if the path fingered returned 404. # hence there's nothing more to do. end end |
#get(tfs_name) ⇒ Object
获取文件
42 43 44 |
# File 'lib/rtfs/client.rb', line 42 def get(tfs_name) http_get("/v1/#{appkey}/#{tfs_name}") end |
#get_nameservers ⇒ Object
46 47 48 49 |
# File 'lib/rtfs/client.rb', line 46 def get_nameservers # 通过 ns_addr 的地址获取负载均衡的地址 @nameservers = open("#{@ns_addr}/tfs.list").read.split("\n") end |
#put(path, options = {}) ⇒ Object
上传文件 参数:
file_path 需要上传的文件路径
:ext 扩展名,默认会取 file_path 的扩展名, 如: .jpg
返回值
T1lpVcXftHXXaCwpjX
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rtfs/client.rb', line 57 def put(path, = {}) ext = [:ext] || File.extname(path) path = path.to_s resp = http_post("/v1/#{appkey}", File.open(path.start_with?('/') ? path : fpath(path)).read, :params => { :suffix => ext, :simple_name => [:simple_name] || 0 }, :accept => :json) json = JSON.parse(resp) json && json['TFS_FILE_NAME'] end |
#put_and_get_url(path, options = {}) ⇒ Object
上传文件 并返回完整 url (only for Taobao)
73 74 75 76 77 78 79 |
# File 'lib/rtfs/client.rb', line 73 def put_and_get_url(path, = {}) ext = [:ext] || File.extname(path) path = path.to_s tname = put(path, :ext => ext) "http://img0#{rand(4)+1}.taobaocdn.com/tfscom/#{t}#{ext}" unless tname.nil? end |
#rm(tname, options = {}) ⇒ Object
删除文件, 不能带扩展名
82 83 84 85 86 |
# File 'lib/rtfs/client.rb', line 82 def rm(tname, = {}) resp = http_delete("/v1/#{appkey}/#{tname}", :params => ) resp && resp.code == 200 end |
#save(path, options = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rtfs/client.rb', line 97 def save(path, = {}) path = path.to_s if finger(path) del(path) save(path) elsif create(path) write(path) end end |
#stat(tname, options = {}) ⇒ Object
文件信息查看, 不能带扩展名
89 90 91 92 93 94 95 |
# File 'lib/rtfs/client.rb', line 89 def stat(tname, = {}) resp = http_get("/v1/#{appkey}/metadata/#{tname}", :params => ) if resp && resp.code == 200 JSON.parse(resp) end end |
#write(path, options = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rtfs/client.rb', line 115 def write(path, = {}) data = File.open(fpath(path)).read return if data.length == 0 resp = http_put(furl(path), data, :params => {:offset => 0}) if resp && resp.code == 200 [appid, fuid(path), path].join('/') end end |