rtfs 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.0 / 2013-03-15
2
+
3
+ * Now can custom upload filename;
4
+
1
5
  ## 0.1.0 / 2012-04-25
2
6
 
3
7
  * Add WebService interface.
@@ -5,4 +9,4 @@
5
9
 
6
10
  ## 0.0.1 / 2011-08-02
7
11
 
8
- * Birthday!
12
+ * Birthday!
data/README.md CHANGED
@@ -124,14 +124,48 @@ tfs.save('foo.jpg') # ==> 133/7463/foo.jpg
124
124
 
125
125
  # remove named file.
126
126
  tfs.del('foo.jpg') # ==> true
127
+ ```
128
+
129
+
130
+ Custom Path
131
+ -----------
132
+
133
+ 自定义文件名的完整路径处理逻辑如下:
127
134
 
128
- # put files under public folder, and preserve its name.
129
- # will prepend the basedir in front of the file path passed in.
130
- tfs.save('jquery.js')
135
+ ```ruby
136
+ tfs = RTFS::Client.new(:ns_addr => 'http://127.0.0.1:3800',
137
+ :appkey => 'myprecious',
138
+ :basedir => Rails.root.join('public'))
139
+ tfs.save('foo/ham.jpg')
131
140
  ```
132
141
 
142
+ 将会做如下步骤:
143
+
144
+ 1. 判断你的 TFS 中是否已有这个文件,
145
+ 2. 如果有,先删掉,
146
+ 3. 创建这个文件,相当于 UNIX 中的 `touch` 命令,不写入实际内容,文件内容为空,
147
+ 4. 尝试打开 `basedir` 与传入的 `foo/ham.jpg` 拼起来的路径,在此例中,即:
148
+ `Rails.root.join('public', 'foo/ham.jpg')`
149
+ 5. 将该文件数据写入 TFS
150
+
151
+ 在这中间,有个概念需要说明,自定义路径中,有一层叫做 uid(User ID),目的是分散文件位置,
152
+ 避免数据读写过度集中。如果希望把 TFS 当 CDN 用,则需要固定这个 uid,可以在实例化 `RTFS::Client`
153
+ 时传入,也可以实例化之后赋值:
154
+
155
+ ```ruby
156
+ tfs = RTFS::Client.new(:uid => 10001,
157
+ # other options...
158
+ )
159
+
160
+ tfs.uid = 10001
161
+ ```
162
+
163
+ 这里选择 10001 是有原因的。在 `RTFS::Client` 的实现中,如果用户没有指定 uid,
164
+ 则它会根据传入的文件名自动计算,算该名称的哈希,然后取余,范围是 10000 之内。
165
+ 而 10000 本身,则为 RTFS 的测试所用,所以,自定义的 uid,从 10001 开始,比较妥。
166
+
133
167
 
134
168
  Executabes?
135
169
  -----------
136
170
 
137
- RTFS will be available as a executable binary soon. Stay tuned...
171
+ RTFS will be available as an executable binary soon. Stay tuned...
@@ -17,17 +17,22 @@ module RTFS
17
17
  end
18
18
 
19
19
  attr_accessor :nameservers
20
+ attr_accessor :access_count
20
21
  attr_accessor :appkey
21
22
  attr_accessor :uid
22
23
 
23
24
  def initialize(options)
24
- # 通过 ns_addr 的地址获取负载均衡的地址
25
- @nameservers = open("#{options[:ns_addr]}/tfs.list").read.split("\n")
26
- @appkey = options[:appkey]
27
25
 
26
+
27
+ @appkey = options[:appkey]
28
+ @ns_addr = options[:ns_addr]
28
29
  @basedir = options[:basedir]
29
30
  @uid = options[:uid]
30
31
 
32
+ @access_count = 0
33
+
34
+ get_nameservers
35
+
31
36
  if @uid.nil? && !@basedir.nil?
32
37
  (Digest::MD5.hexdigest(@basedir).to_i(16) % 10000)
33
38
  end
@@ -38,6 +43,11 @@ module RTFS
38
43
  http_get("/v1/#{appkey}/#{tfs_name}")
39
44
  end
40
45
 
46
+ def get_nameservers
47
+ # 通过 ns_addr 的地址获取负载均衡的地址
48
+ @nameservers = open("#{@ns_addr}/tfs.list").read.split("\n")
49
+ end
50
+
41
51
  # 上传文件
42
52
  # 参数:
43
53
  # file_path 需要上传的文件路径
@@ -177,6 +187,11 @@ module RTFS
177
187
  #
178
188
  def nameserver
179
189
  # 现在第一行是 50,表示频次,访问 50 次就要更新
190
+ @access_count += 1
191
+ if(@access_count > @nameservers[0].to_i)
192
+ get_nameservers
193
+ @access_count = 0
194
+ end
180
195
  @nameservers[rand(@nameservers.size - 1) + 1]
181
196
  end
182
197
 
@@ -5,7 +5,7 @@
5
5
  module RTFS::Version
6
6
  MAJOR = 0
7
7
  MINOR = 2
8
- REVISION = 0
8
+ REVISION = 1
9
9
 
10
10
  class << self
11
11
  # Returns X.Y.Z formatted version string
@@ -3,12 +3,17 @@ require 'open-uri'
3
3
 
4
4
  describe 'Basic operations' do
5
5
  it 'should put and remove file' do
6
+ rcount = $tfs.access_count
7
+
6
8
  fname = $tfs.put('spec/a.jpg')
9
+ $tfs.access_count.should eq(rcount + 1)
7
10
 
8
11
  f = open("http://img01.daily.taobaocdn.net/tfscom/#{fname}.jpg")
9
12
  f.read.length.should eq(6511)
10
13
 
14
+ $tfs.access_count = $tfs.nameservers[0].to_i
11
15
  $tfs.rm(fname).should eq(true)
16
+ $tfs.access_count.should eq(0)
12
17
  end
13
18
 
14
19
  it 'should stat file' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rtfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-03-15 00:00:00.000000000 Z
15
+ date: 2013-06-04 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rest-client