Class: RTFS::Client
- Inherits:
-
Object
- Object
- RTFS::Client
- Defined in:
- lib/rtfs/client.rb
Instance Attribute Summary collapse
-
#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
获取文件.
-
#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.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rtfs/client.rb', line 23 def initialize() # 通过 ns_addr 的地址获取负载均衡的地址 @nameservers = open("#{[:ns_addr]}/tfs.list").read.split("\n") @appkey = [:appkey] @basedir = [:basedir] @uid = [:uid] if @uid.nil? && !@basedir.nil? (Digest::MD5.hexdigest(@basedir).to_i(16) % 10000) end end |
Instance Attribute Details
#appkey ⇒ Object
Returns the value of attribute appkey.
20 21 22 |
# File 'lib/rtfs/client.rb', line 20 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.
21 22 23 |
# File 'lib/rtfs/client.rb', line 21 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
134 135 136 137 138 139 140 141 142 |
# File 'lib/rtfs/client.rb', line 134 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
98 99 100 101 102 103 |
# File 'lib/rtfs/client.rb', line 98 def create(path, = {}) resp = http_post(furl(path), nil, :params => {:recursive => 1}) resp && resp.code == 201 end |
#del(path, options = {}) ⇒ Object
116 117 118 119 120 |
# File 'lib/rtfs/client.rb', line 116 def del(path, = {}) resp = http_delete(furl(path)) resp && resp.code == 200 end |
#finger(path, options = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rtfs/client.rb', line 122 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
获取文件
37 38 39 |
# File 'lib/rtfs/client.rb', line 37 def get(tfs_name) http_get("/v1/#{appkey}/#{tfs_name}") end |
#put(path, options = {}) ⇒ Object
上传文件 参数:
file_path 需要上传的文件路径
:ext 扩展名,默认会取 file_path 的扩展名, 如: .jpg
返回值
T1lpVcXftHXXaCwpjX
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rtfs/client.rb', line 47 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)
63 64 65 66 67 68 69 |
# File 'lib/rtfs/client.rb', line 63 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
删除文件, 不能带扩展名
72 73 74 75 76 |
# File 'lib/rtfs/client.rb', line 72 def rm(tname, = {}) resp = http_delete("/v1/#{appkey}/#{tname}", :params => ) resp && resp.code == 200 end |
#save(path, options = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rtfs/client.rb', line 87 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
文件信息查看, 不能带扩展名
79 80 81 82 83 84 85 |
# File 'lib/rtfs/client.rb', line 79 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
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rtfs/client.rb', line 105 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 |