Class: Fastdfs::Client::Storage

Inherits:
Object
  • Object
show all
Extended by:
Hook
Defined in:
lib/fastdfs-client/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hook

add_hook, after, before, ensure_hijacked, hooks, method_added

Constructor Details

#initialize(host, port, store_path = nil, options = {}) ⇒ Storage

Returns a new instance of Storage.



14
15
16
17
18
19
20
21
22
23
# File 'lib/fastdfs-client/storage.rb', line 14

def initialize(host, port, store_path = nil, options = {})
  @host = host
  @port = port
  @options = options || {}
  @options = store_path if store_path.is_a?(Hash)
  @socket = Socket.new(host, port, @options[:socket])
  @extname_len = ProtoCommon::EXTNAME_LEN
  @size_len = ProtoCommon::SIZE_LEN        
  @store_path = store_path || 0
end

Instance Attribute Details

#group_nameObject

Returns the value of attribute group_name.



12
13
14
# File 'lib/fastdfs-client/storage.rb', line 12

def group_name
  @group_name
end

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/fastdfs-client/storage.rb', line 12

def host
  @host
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/fastdfs-client/storage.rb', line 12

def options
  @options
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/fastdfs-client/storage.rb', line 12

def port
  @port
end

#socketObject

Returns the value of attribute socket.



12
13
14
# File 'lib/fastdfs-client/storage.rb', line 12

def socket
  @socket
end

#store_pathObject

Returns the value of attribute store_path.



12
13
14
# File 'lib/fastdfs-client/storage.rb', line 12

def store_path
  @store_path
end

Instance Method Details

#delete(path, group_name = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fastdfs-client/storage.rb', line 29

def delete(path, group_name = nil)
  cmd = CMD::DELETE_FILE
  raise "path arguments is empty!" if path.blank?
  if group_name.blank?
    group_name = /^\/?(\w+)/.match(path)[1]
    path = path.gsub("/#{group_name}")
  end
  raise "group_name arguments is empty!" if group_name.blank?
  group_bytes = group_name.bytes.fill(0, group_name.length...ProtoCommon::GROUP_NAME_MAX_LEN)
  path_length = (group_bytes.length + path.bytes.length)

  @socket.write(cmd, (ProtoCommon.header_bytes(cmd, path_length) + group_bytes + path.bytes))
  @socket.receive
end

#upload(file) ⇒ Object



25
26
27
# File 'lib/fastdfs-client/storage.rb', line 25

def upload(file)  
  _upload(file)
end