Module: COS::Util

Defined in:
lib/cos/util.rb

Class Method Summary collapse

Class Method Details

.file_sha1(file) ⇒ Object

文件sha1



12
13
14
# File 'lib/cos/util.rb', line 12

def file_sha1(file)
  Digest::SHA1.file(file).hexdigest
end

.get_list_path(path, name = '', is_file = false) ⇒ Object

解析list时的path



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cos/util.rb', line 37

def get_list_path(path, name = '', is_file = false)
  # 目录必须带"/"
  path = "/#{path}" unless path.start_with?('/')

  if is_file
    # 文件
    if path.end_with?('/')
      "#{path}#{name}"
    else
      "#{path}/#{name}"
    end
  else
    # 目录
    if path.end_with?('/')
      "#{path}#{name}/"
    else
      "#{path}/#{name}/"
    end
  end
end

.get_local_path(path, disable_mkdir = false) ⇒ Object

获取本地目录路径, 不存在会创建



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cos/util.rb', line 22

def get_local_path(path, disable_mkdir = false)
  local = File.expand_path(path)
  unless File.exist?(local) and File.directory?(local)
    # 创建目录
    if disable_mkdir
      raise LocalPathNotExist, "Local path #{local} not exist!"
    else
      FileUtils::mkdir_p(local)
    end
  end

  local
end

.get_resource_path(app_id, bucket, path, file = nil) ⇒ Object

获取resource_path



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cos/util.rb', line 59

def get_resource_path(app_id, bucket, path, file = nil)
  # file_name检测
  if file and (file.end_with?('/') or file.start_with?('/'))
    raise ClientError, "File name can't start or end with '/'"
  end

  # 目录必须带"/"
  path = "/#{path}" unless path.start_with?('/')
  path = "#{path}/" unless path.end_with?('/')

  "/#{app_id}/#{bucket}#{path}#{file}"
end

.get_resource_path_or_file(app_id, bucket, path) ⇒ Object

获取resource_path, 不自动添加‘/’



73
74
75
76
77
# File 'lib/cos/util.rb', line 73

def get_resource_path_or_file(app_id, bucket, path)
  # 目录必须带"/"
  path = "/#{path}" unless path.start_with?('/')
  "/#{app_id}/#{bucket}#{path}"
end

.string_sha1(string) ⇒ Object

字符串sha1



17
18
19
# File 'lib/cos/util.rb', line 17

def string_sha1(string)
  Digest::SHA1.hexdigest(string)
end