Class: Fastdfs::Client::Storage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Storage.



9
10
11
12
13
14
15
16
# File 'lib/fastdfs-client/storage.rb', line 9

def initialize(host, port, store_path = nil, options = {})
  @options = options || {}
  @options = store_path if store_path.is_a?(Hash)

  @proxy = ClientProxy.new(host, port, @options[:socket])
  @socket = @proxy.socket
  @store_path = store_path || 0
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/fastdfs-client/storage.rb', line 7

def options
  @options
end

#proxyObject

Returns the value of attribute proxy.



7
8
9
# File 'lib/fastdfs-client/storage.rb', line 7

def proxy
  @proxy
end

#socketObject

Returns the value of attribute socket.



7
8
9
# File 'lib/fastdfs-client/storage.rb', line 7

def socket
  @socket
end

#store_pathObject

Returns the value of attribute store_path.



7
8
9
# File 'lib/fastdfs-client/storage.rb', line 7

def store_path
  @store_path
end

Instance Method Details

#delete(path, group_name = nil) ⇒ Object



32
33
34
35
# File 'lib/fastdfs-client/storage.rb', line 32

def delete(path, group_name = nil)
  header_bytes = group_path_bytes(path, group_name).flatten
  @proxy.dispose(CMD::DELETE_FILE, header_bytes)
end

#download(path, group_name = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/fastdfs-client/storage.rb', line 53

def download(path, group_name = nil)
  path_bytes = group_path_bytes(path, group_name).flatten
  header_bytes = 0.to_eight_buffer.concat(0.to_eight_buffer).concat(path_bytes)
  @proxy.dispose(CMD::DOWNLOAD_FILE, header_bytes) do |body|
    create_tempfile(path, body) if body
  end
end

#get_metadata(path, group_name = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/fastdfs-client/storage.rb', line 37

def (path, group_name = nil)
  header_bytes = group_path_bytes(path, group_name).flatten
  @proxy.dispose(CMD::GET_METADATA, header_bytes) do |body|
    res = body.split(ProtoCommon::RECORD_SEPERATOR).map do |c| 
      c.split(ProtoCommon::FILE_SEPERATOR) 
    end.flatten
    Hash[*res].fs_symbolize_keys
  end
end

#set_metadata(path, group_name = nil, options = {}, flag = :cover) ⇒ Object



47
48
49
50
51
# File 'lib/fastdfs-client/storage.rb', line 47

def (path, group_name = nil, options = {}, flag = :cover)
  flag, options = options, {} unless options.is_a?(Hash)
  options, group_name = group_name, nil if group_name.is_a?(Hash)
  (path, group_name, options, flag)
end

#upload(_file, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fastdfs-client/storage.rb', line 18

def upload(_file, options = {})  
  file, ext_name_bytes = convert_file_info(_file)
  size_byte = [@store_path].concat(file.size.to_eight_buffer).full_fill(0, file_size_len)
  content_len = file_size_len + extname_len + file.size

  @proxy.dispose(CMD::UPLOAD_FILE, size_byte + ext_name_bytes, IO.read(file)) do |body|
    group_name_max_len = ProtoCommon::GROUP_NAME_MAX_LEN
    
    res = {group_name: body[0...group_name_max_len].strip, path: body[group_name_max_len..-1]}
    (res[:path], res[:group_name], options) unless options.blank?
    res
  end
end