fastdfs-client 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 564b4fc72139a1d9bc1faeba24ecf21394bf9e4b
4
- data.tar.gz: ef2717e16860af59cfaeccc0b99d1ad0284d1972
3
+ metadata.gz: 1e0b3352eb6ff6a305d8083b19000e950632c6db
4
+ data.tar.gz: 80788d168fa8726c71cf8da1f9ef325e145da3c1
5
5
  SHA512:
6
- metadata.gz: afec497233fb8ecc7736dff565ea90db045376381d012cbede8f7f47e69f156410ab0fb87e017085066f03e55be2b049b41304fb105e6efd48dfc78745157a15
7
- data.tar.gz: fc7979eb1bf964b0bc5ee5942535f264d4518453191ab76d9eecb73e7b56cafcbb508e464be17ad55337cb1cd5bd7391c3ac8ace7120d998c3ca08d23bf827a0
6
+ metadata.gz: 4e4a2a249cd266bcac2fe453f7a0120f53589a006328a355b78475016ec91facb337c4d965164f872bcd188744c09dde55474d82810c26f400eaf57760fc0368
7
+ data.tar.gz: e555aba138f34c8088a3709e5426426a79bab48fea3edba8b033d89418bf1e2c9c477300ff1caafe5c7815f1ef21b6acc83bdf666fabd965c6799d71647f5b13
data/.DS_Store CHANGED
Binary file
data/README.md CHANGED
@@ -9,6 +9,8 @@ fastdfs client for ruby
9
9
  ### Using
10
10
 
11
11
  ```RUBY
12
+
13
+ require 'fastdfs-client'
12
14
 
13
15
  # return the result format
14
16
  # {status: true, err_msg: "", result: ...}
@@ -22,7 +22,7 @@ module Fastdfs
22
22
  @socket.connection do
23
23
  full_header = ProtoCommon.header_bytes(cmd, content_len) + header
24
24
  @socket.write(cmd, full_header)
25
- Array(@content).each do |c|
25
+ Array(content).each do |c|
26
26
  @socket.write(cmd, c)
27
27
  end
28
28
  @socket.receive &block
@@ -7,4 +7,32 @@ class Array
7
7
  def full_fill(val, len)
8
8
  self.fill(val, self.length...len)
9
9
  end
10
+ end
11
+
12
+
13
+ class NilClass
14
+ def blank?
15
+ true
16
+ end
17
+ end
18
+
19
+ class String
20
+ def blank?
21
+ self.strip.empty?
22
+ end
23
+ end
24
+
25
+ class Object
26
+ def blank?
27
+ respond_to?(:empty?) ? empty? : !self
28
+ end
29
+ end
30
+
31
+
32
+ class Hash
33
+
34
+ def fs_symbolize_keys
35
+ defined?(super) ? super : self.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
36
+ end
37
+
10
38
  end
@@ -19,8 +19,10 @@ module Fastdfs
19
19
  end
20
20
 
21
21
  def write(*args)
22
+ debugger if args[1] == 119
22
23
  @cmd = args.shift
23
24
  pkg = args.shift
25
+
24
26
  pkg = pkg.pack("C*") if pkg.is_a?(Array)
25
27
  @socket.write pkg
26
28
  end
@@ -89,7 +91,7 @@ module Fastdfs
89
91
  body_len -= len
90
92
  end
91
93
  end
92
- @content = nil if Utils.is_blank? @content
94
+ @content = nil if @content.blank?
93
95
  end
94
96
 
95
97
  end
@@ -18,7 +18,7 @@ module Fastdfs
18
18
  end
19
19
 
20
20
  def upload(file, options = {})
21
- ext_name_bs = File.extname(file)[1..-1].to_s.bytes.full_fill(0, @extname_len)
21
+ ext_name_bs = File.extname(file)[1..@extname_len].to_s.bytes.full_fill(0, @extname_len)
22
22
  size_byte = ([@store_path] + Utils.number_to_buffer(file.size)).full_fill(0, @size_len)
23
23
  content_len = (@size_len + @extname_len + file.size)
24
24
 
@@ -26,7 +26,7 @@ module Fastdfs
26
26
  group_name_max_len = ProtoCommon::GROUP_NAME_MAX_LEN
27
27
 
28
28
  res = {group_name: body[0...group_name_max_len].strip, path: body[group_name_max_len..-1]}
29
- _set_metadata(res[:path], res[:group_name], options) unless Utils.is_blank?(options)
29
+ _set_metadata(res[:path], res[:group_name], options) unless options.blank?
30
30
  res
31
31
  end
32
32
  end
@@ -42,7 +42,7 @@ module Fastdfs
42
42
  res = body.split(ProtoCommon::RECORD_SEPERATOR).map do |c|
43
43
  c.split(ProtoCommon::FILE_SEPERATOR)
44
44
  end.flatten
45
- Utils.symbolize_keys(Hash[*res])
45
+ Hash[*res].fs_symbolize_keys
46
46
  end
47
47
  end
48
48
 
@@ -74,8 +74,8 @@ module Fastdfs
74
74
  end
75
75
 
76
76
  def extract_path!(path, group_name = nil)
77
- raise "path arguments is empty!" if Utils.is_blank? path
78
- if Utils.is_blank? group_name
77
+ raise "path arguments is empty!" if path.blank?
78
+ if group_name.blank?
79
79
  group_name = /^\/?(\w+)/.match(path)[1]
80
80
  path = path.gsub(Regexp.new("/?#{group_name}/?"), "")
81
81
  end
@@ -90,8 +90,7 @@ module Fastdfs
90
90
  size_bytes = Utils.number_to_buffer(path_bytes.length) + Utils.number_to_buffer(meta_bytes.length)
91
91
  size_bytes = (size_bytes).full_fill(0, 16)
92
92
  total = size_bytes.length + flag.length + group_bytes.length + path_bytes.length + meta_bytes.length
93
-
94
- @proxy.dispose(CMD::SET_METADATA, total, (size_bytes + flag.bytes + group_bytes + path_bytes), meta_bytes)
93
+ @proxy.dispose(CMD::SET_METADATA, total, (size_bytes + flag.bytes + group_bytes + path_bytes), meta_bytes.pack("C*"))
95
94
  end
96
95
 
97
96
  def convert_meta_flag(flag)
@@ -10,11 +10,4 @@ module Utils
10
10
  arr1
11
11
  end
12
12
 
13
- def self.symbolize_keys(obj)
14
- obj.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
15
- end
16
-
17
- def self.is_blank?(obj)
18
- obj.nil? || obj.empty?
19
- end
20
13
  end
@@ -1,5 +1,5 @@
1
1
  module Fastdfs
2
2
  module Client
3
- VERSION = '1.2.0'
3
+ VERSION = '1.3.0'
4
4
  end
5
5
  end
Binary file
@@ -13,12 +13,13 @@ class MockTCPSocket
13
13
  end
14
14
 
15
15
  def write(*args)
16
- sleep(0..7)
17
16
  pkg = args[0].unpack("C*")
18
17
  @cmd ||= pkg[8]
18
+ sleep(rand(0..4))
19
19
  end
20
20
 
21
21
  def recv(len)
22
+ sleep(rand(0..3))
22
23
  data = case @cmd
23
24
  when 101
24
25
  gate_tracker(len)
@@ -25,38 +25,53 @@ describe Fastdfs::Client::Storage do
25
25
  storage.upload(TestConfig::FILE, {author: "kaka", width: "800"})
26
26
  end
27
27
 
28
- it "should the result attributes group_name and path" do
29
- res = storage.upload(TestConfig::FILE)
30
- expect(res[:status]).to be_truthy
31
- expect(res[:result]).to include(:group_name)
32
- expect(res[:result]).to include(:path)
28
+ it "tempfile upload " do
29
+ file = Tempfile.new("1.txt")
30
+ file.write("testtest")
31
+ file.close
32
+ expect(storage.upload(file)[:status]).to be_truthy
33
33
  end
34
34
 
35
- it "can delete file by group and path" do
36
- res = storage.upload(TestConfig::FILE)[:result]
37
- storage.delete(res[:path], res[:group_name])
38
- end
35
+ describe "upload file test " do
39
36
 
40
- it "can delete file raise exception" do
41
- res = storage.upload(TestConfig::FILE)[:result]
42
- result = FC::ProtoCommon.header_bytes(FC::CMD::RESP_CODE, 0, 22)
43
- MockTCPSocket.any_instance.stub("recv").and_return(result.pack("C*"))
44
- expect( storage.delete("fdsaf", res[:group_name])[:status] ).to be_falsey
45
- end
37
+ before(:each) do
38
+ @res_body = storage.upload(TestConfig::FILE)
39
+ end
46
40
 
47
- it "can get metadata results" do
48
- res = storage.get_metadata("#{TestConfig::GROUP_NAME}/#{TestConfig::FILE_NAME}")
49
- expect(res[:result]).to eq(TestConfig::METADATA)
50
- end
41
+ it "should the result attributes group_name and path" do
42
+ expect(@res_body[:status]).to be_truthy
43
+ expect(@res_body[:result]).to include(:group_name)
44
+ expect(@res_body[:result]).to include(:path)
45
+ end
51
46
 
52
- it "can set metadata" do
53
- expect(storage.set_metadata(TestConfig::FILE_NAME, TestConfig::GROUP_NAME, TestConfig::METADATA)).to be_truthy
54
- end
47
+ it "can delete file by group and path" do
48
+ res = @res_body[:result]
49
+ storage.delete(res[:path], res[:group_name])
50
+ end
51
+
52
+ it "can delete file raise exception" do
53
+ res = @res_body[:result]
54
+ result = FC::ProtoCommon.header_bytes(FC::CMD::RESP_CODE, 0, 22)
55
+ MockTCPSocket.any_instance.stub("recv").and_return(result.pack("C*"))
56
+ expect( storage.delete("fdsaf", res[:group_name])[:status] ).to be_falsey
57
+ end
58
+
59
+ it "can get metadata results" do
60
+ res = @res_body[:result]
61
+ storage.set_metadata(res[:path], res[:group_name], TestConfig::METADATA)
62
+ res = storage.get_metadata("#{res[:group_name]}/#{res[:path]}")
63
+ expect(res[:result]).to eq(TestConfig::METADATA)
64
+ end
65
+
66
+ it "can set metadata" do
67
+ expect(storage.set_metadata(TestConfig::FILE_NAME, TestConfig::GROUP_NAME, TestConfig::METADATA)).to be_truthy
68
+ end
55
69
 
56
- it "download the file to the local" do
57
- res = storage.download(TestConfig::FILE_NAME, TestConfig::GROUP_NAME)
58
- expect(res[:status]).to be_truthy
59
- expect(res[:result]).to be_an_instance_of(Tempfile)
60
- expect(IO.read(res[:result])).to eq(IO.read(TestConfig::FILE))
70
+ it "download the file to the local" do
71
+ res = storage.download(TestConfig::FILE_NAME, TestConfig::GROUP_NAME)
72
+ expect(res[:status]).to be_truthy
73
+ expect(res[:result]).to be_an_instance_of(Tempfile)
74
+ expect(IO.read(res[:result])).to eq(IO.read(TestConfig::FILE))
75
+ end
61
76
  end
62
77
  end
@@ -24,7 +24,7 @@ describe Fastdfs::Client::Tracker do
24
24
 
25
25
  it "verify the server address and port" do
26
26
  expect(tracker.get_storage.socket.host).to eq(TestConfig::STORAGE_IP)
27
- #[0, 0, 0, 0, 0, 89, 216, 0]
27
+
28
28
  expect(tracker.get_storage.socket.port.to_s).to eq(TestConfig::STORAGE_PORT)
29
29
  expect(tracker.get_storage.store_path).to eq(TestConfig::STORE_PATH)
30
30
  end
@@ -36,22 +36,19 @@ describe Fastdfs::Client::Tracker do
36
36
  expect(tracker.get_storage[:status]).to be_falsey
37
37
  end
38
38
 
39
- it "run server flow" do
40
- items = 5.times.map do
39
+ it "multi thread upload" do
40
+ items = 6.times.map do
41
41
  Thread.new do
42
42
  storage = tracker.get_storage
43
- results = storage.upload(File.open(File.expand_path("../page.png", __FILE__)))[:result]
44
- puts results
45
- puts storage.delete(results[:path], results[:group_name])
43
+ res = storage.upload(File.open(File.expand_path("../page.png", __FILE__)))
44
+ expect(res[:status]).to be_truthy
45
+ results = res[:result]
46
+ results = storage.delete(results[:path], results[:group_name])
47
+ expect(res[:status]).to be_truthy
46
48
  end
47
49
  end
48
50
 
49
- items.each{|item| item.join }
50
-
51
- # storage = tracker.get_storage
52
- # puts "#{storage.host}, #{storage.port}"
53
- # results = storage.upload(File.open("page.png"))
54
- # puts results
55
- # puts storage.delete(results[:path], results[:group_name])
51
+ items.map{|item| item.join }
56
52
  end
53
+
57
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastdfs-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ka Ka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-21 00:00:00.000000000 Z
11
+ date: 2016-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler