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
17
18
19
20
# File 'lib/fastdfs-client/storage.rb', line 9

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

  @proxy = ClientProxy.new(host, port, options)
  @socket = @proxy.socket
end

Instance Attribute Details

#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



36
37
38
39
# File 'lib/fastdfs-client/storage.rb', line 36

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



57
58
59
60
61
62
63
# File 'lib/fastdfs-client/storage.rb', line 57

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



41
42
43
44
45
46
47
48
49
# File 'lib/fastdfs-client/storage.rb', line 41

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



51
52
53
54
55
# File 'lib/fastdfs-client/storage.rb', line 51

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



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastdfs-client/storage.rb', line 22

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)

  byte = size_byte + ext_name_bytes

  @proxy.dispose(CMD::UPLOAD_FILE, byte , 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