rtfs 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,123 @@
1
+ RTFS
2
+ ====
3
+
4
+ RTFS is a Ruby Client for TFS.
5
+
6
+ [About TFS](http://code.taobao.org/project/view/366/)
7
+
8
+ Requirements
9
+ ------------
10
+
11
+ * Linux System (TFS only success build in Linux system.)
12
+ * TFS
13
+
14
+ Configure
15
+ ---------
16
+
17
+ create this files before using.
18
+
19
+ config/tfs.yml
20
+
21
+ defaults: &defaults
22
+ host: '127.0.0.1:3100'
23
+
24
+ development:
25
+ <<: *defaults
26
+
27
+ test:
28
+ <<: *defaults
29
+
30
+ production:
31
+ <<: *defaults
32
+
33
+ config/initialize/tfs.rb
34
+
35
+ require 'rtfs'
36
+ tfs_config = YAML.load_file("#{Rails.root}/config/tfs.yml")[Rails.env]
37
+ $tfs = RTFS::Client.new(:ns_addr => tfs_config['host'])
38
+
39
+ Configure without TFS install
40
+ -----------------------------
41
+
42
+ If you development environment is Mac, or your developemnt environment not install TFS, you need use this file to hack upload file as FileSystem.
43
+
44
+ lib/fake_tfs.rb
45
+
46
+ module FakeTfs
47
+ class Client
48
+ def initialize(options = {})
49
+ @ns_addr = options[:ns_addr] || "uploads/tfs/"
50
+ if @ns_addr[0] != "/"
51
+ @ns_addr = [Rails.root,"public",@ns_addr].join("/")
52
+ end
53
+ end
54
+
55
+ def put_file(file_path, options = {})
56
+ ext = options[:ext] || File.extname(file_path)
57
+ ext = ".jpg" if ext.blank?
58
+ file_name = [Digest::SHA1.hexdigest(file_path),ext].join("")
59
+
60
+ new_path = [@ns_addr,file_name].join("/")
61
+
62
+ FileUtils.cp(file_path, new_path)
63
+ File.basename(new_path)
64
+ end
65
+
66
+ def rm_file(file_id)
67
+ puts "FakeTFS: removing #{file_id}\n"
68
+ Dir.glob(File.join(@ns_addr, "#{file_id}.*"))[0]
69
+ end
70
+ end
71
+ end
72
+
73
+
74
+ config/tfs.yml
75
+
76
+ defaults: &defaults
77
+ host: 'uploads/tfs/'
78
+
79
+ development:
80
+ <<: *defaults
81
+
82
+ test:
83
+ <<: *defaults
84
+
85
+ production:
86
+ <<: *defaults
87
+ host: '127.0.0.1:3100'
88
+
89
+ config/initialize/tfs.rb
90
+
91
+ tfs_config = YAML.load_file("#{Rails.root}/config/tfs.yml")[Rails.env]
92
+ if Rails.env == "development"
93
+ $tfs = FakeTfs::Client.new(:ns_addr => tfs_config['host'])
94
+ else
95
+ require 'rtfs'
96
+ $tfs = RTFS::Client.new(:ns_addr => tfs_config['host'])
97
+ end
98
+
99
+
100
+ Usage
101
+ -----
102
+
103
+ class UsersController < ApplicationController
104
+ def save
105
+ @user = User.new
106
+ ext = File.extname(params[:Filedata].original_filename)
107
+ file_name = $tfs.put(params[:Filedata].tempfile.path, :ext => ext)
108
+ @user.avatar_path = [file_name,ext].join("")
109
+ end
110
+ end
111
+
112
+
113
+ class User < ActiveRecord::Base
114
+ def avatar_url
115
+ server_id = self.id % 4 + 1
116
+ "http://img#{server_id}.tbcdn.com/#{self.avatar_path}"
117
+ end
118
+ end
119
+
120
+ Put local file to TFS
121
+
122
+ $ $tfs.put("~/Downloads/a.jpg")
123
+ T1Ub1XXeFBXXb1upjX
data/lib/rtfs.rb CHANGED
@@ -20,7 +20,7 @@ require_local 'rtfs/client.rb'
20
20
  require_local 'rtfs/meta.rb'
21
21
  require_local 'rtfs/version.rb'
22
22
 
23
- # tfs = RTFS::Client.new({:ns_addr => '10.246.65.210:3100'})
23
+ tfs = RTFS::Client.new({:ns_addr => '10.246.65.210:3100'})
24
24
  # puts "tfsname: '#{tfs.put_file_and_get_url("/home/xifeng/1.png")}'"
25
25
  # puts "#{tfs.get_file('T1H9t5XeRwXXaCwpjX')}"
26
26
  # puts "#{tfs.file_stat('T1H9t5XeRwXXaCwpjX').inspect}"
data/lib/rtfs/client.rb CHANGED
@@ -1,80 +1,86 @@
1
1
  # Code for client
2
2
  # by nowa<nowazhu@gmail.com> 2011-07-23
3
-
4
3
  module RTFS
5
- TFSTOOL_PATH = "/home/admin/tfs/bin/tfstool"
4
+ TFSTOOL_PATH = "/home/admin/tfs/bin/tfstool"
6
5
 
7
- class Client
8
- attr_accessor :ns_addr
9
- attr_accessor :tfstool_path
10
-
11
- def initialize(options)
12
- return nil unless options
13
- @ns_addr = options[:ns_addr]
14
- @tfstool_path = options[:tfstool_path] ? options[:tfstool_path] : TFSTOOL_PATH
15
- raise NoTFSToolError.new unless File.exist?(@tfstool_path)
16
- end
17
-
18
- # ��tfs��ȡ�ļ�
19
- def get_file(tfs_name, local_file=nil)
20
- local_file ||= tfs_name
21
- `#{tfstool_cmd} -i "get #{tfs_name} #{local_file}"`
22
- end
23
-
24
- # �����ļ���tfs�������ļ���tfsname
25
- # tfsname example: T1lpVcXftHXXaCwpjX
26
- def put_file(local_file, tfs_name="NULL", suffix=nil)
27
- suffix ||= ".#{local_file.split(".").last}"
28
- result = `#{tfstool_cmd} -i "put #{local_file} #{tfs_name} #{suffix}"`
29
- # puts "result: #{result}"
30
- t = nil
31
- if result.include?("=> ")
32
- result = result.split("=> ").last
33
- if result.include?(",")
34
- t = result.split(",").first
35
- end
36
- end
37
- t.nil? ? nil : t
38
- end
39
-
40
- # �����ļ���tfs�������ļ��Ŀɷ���cdn url
41
- def put_file_and_get_url(local_file, tfs_name="NULL", suffix=nil)
42
- suffix ||= ".#{local_file.split(".").last}"
43
- t = put_file(local_file, tfs_name, suffix)
44
- t.nil? ? nil : "http://img03.taobaocdn.com/tfscom/#{t}#{suffix}"
45
- end
46
-
47
- # ɾ��tfs file
48
- def rm_file(tfs_name)
49
- `#{tfstool_cmd} -i "rm #{tfs_name}"`
50
- end
51
-
52
- # ������tfs file�����Ƽ�ʹ��
53
- # new name ����Ҳ�� tfsname ��ʽ
54
- def rename_file(tfs_name, new_name)
55
- `#{tfstool_cmd} -i "rename #{tfs_name} #{new_name}"`
56
- end
57
-
58
- # ��ȡtfs file��stat
59
- def file_stat(tfs_name)
60
- result = `#{tfstool_cmd} -i "stat #{tfs_name}"`
61
- stat = {}
62
- result.split("\n").each do |line|
63
- line = line.split(':').map {|i| i.gsub!(" ", "")}
64
- stat["#{line.first}"] = line.last
65
- end
66
- stat
67
- end
68
-
69
- protected
70
- def tfstool_cmd
71
- "#{@tfstool_path} -n -s #{@ns_addr}"
72
- end
73
- end
74
-
75
- class NoTFSToolError < RuntimeError
76
- def to_s
77
- "You must install tfs from yum or source first!"
78
- end
79
- end # NoTFSToolError
6
+ class Client
7
+ attr_accessor :ns_addr
8
+ attr_accessor :tfstool_path
9
+
10
+ # 参数:
11
+ # :ns_addr TFS 服务器地址,如 127.0.0.1:3100
12
+ # :tfstool_path tfstool 安装路径,默认 /home/admin/tfs/bin/tfstool
13
+ def initialize(options)
14
+ return nil unless options
15
+ @ns_addr = options[:ns_addr]
16
+ @tfstool_path = options[:tfstool_path] ? options[:tfstool_path] : TFSTOOL_PATH
17
+ raise NoTFSToolError.new unless File.exist?(@tfstool_path)
18
+ end
19
+
20
+ # 获取文件
21
+ def get(tfs_name, local_file=nil)
22
+ local_file ||= tfs_name
23
+ `#{tfstool_cmd} -i "get #{tfs_name} #{local_file}"`
24
+ end
25
+
26
+ # 上传文件
27
+ # 参数:
28
+ # file_path 需要上传的文件路径
29
+ # :ext 扩展名,默认会取 file_path 的扩展名, 如: .jpg
30
+ # 返回值
31
+ # T1lpVcXftHXXaCwpjX
32
+ def put(file_path, options = {})
33
+ ext = options[:ext] || File.extname(file_path)
34
+ tfs_name = "NULL"
35
+ result = `#{tfstool_cmd} -i "put #{file_path} #{tfs_name} #{ext}"`
36
+ # puts "result: #{result}"
37
+ t = nil
38
+ if result.include?("=> ")
39
+ result = result.split("=> ").last
40
+ if result.include?(",")
41
+ t = result.split(",").first
42
+ end
43
+ end
44
+ t.nil? ? nil : t
45
+ end
46
+
47
+ # 上传文件 并返回完整 url (only for Taobao)
48
+ def put_and_get_url(file_path, options = {})
49
+ ext = options[:ext] || File.extname(file_path)
50
+ t = put(file_path, :ext => ext)
51
+ t.nil? ? nil : "http://img03.taobaocdn.com/tfscom/#{t}#{ext}"
52
+ end
53
+
54
+ # 删除文件, 不能带扩展名
55
+ def rm(tfs_name)
56
+ `#{tfstool_cmd} -i "rm #{tfs_name}"`
57
+ end
58
+
59
+ # 改名, 不能带扩展名
60
+ def rename(tfs_name, new_name)
61
+ `#{tfstool_cmd} -i "rename #{tfs_name} #{new_name}"`
62
+ end
63
+
64
+ # 文件信息查看, 不能带扩展名
65
+ def stat(tfs_name)
66
+ result = `#{tfstool_cmd} -i "stat #{tfs_name}"`
67
+ stat = {}
68
+ result.split("\n").each do |line|
69
+ line = line.split(':').map {|i| i.gsub!(" ", "")}
70
+ stat["#{line.first}"] = line.last
71
+ end
72
+ stat
73
+ end
74
+
75
+ protected
76
+ def tfstool_cmd
77
+ "#{@tfstool_path} -n -s #{@ns_addr}"
78
+ end
79
+ end
80
+
81
+ class NoTFSToolError < RuntimeError
82
+ def to_s
83
+ "You must install tfs from yum or source first!"
84
+ end
85
+ end # NoTFSToolError
80
86
  end
data/lib/rtfs/version.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  module RTFS::Version
5
5
  MAJOR = 0
6
6
  MINOR = 0
7
- REVISION = 2
7
+ REVISION = 3
8
8
 
9
9
  class << self
10
10
  # Returns X.Y.Z formatted version string
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rtfs
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nowa Zhu
@@ -15,7 +15,7 @@ autorequire: rtfs
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-02 00:00:00 +08:00
18
+ date: 2011-08-24 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -40,7 +40,7 @@ executables: []
40
40
  extensions: []
41
41
 
42
42
  extra_rdoc_files:
43
- - README
43
+ - README.markdown
44
44
  - CHANGES
45
45
  - MIT-LICENSE
46
46
  files:
@@ -49,7 +49,7 @@ files:
49
49
  - lib/rtfs/client.rb
50
50
  - lib/rtfs/wrapper.rb
51
51
  - lib/rtfs.rb
52
- - README
52
+ - README.markdown
53
53
  - CHANGES
54
54
  - MIT-LICENSE
55
55
  has_rdoc: true
data/README DELETED
@@ -1,3 +0,0 @@
1
- RTFS is a Ruby Client for TFS.
2
-
3
- What's TFS: http://code.taobao.org/project/view/366/