module COS::Util

Public Class Methods

file_sha1(file) click to toggle source

文件sha1

# 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) click to toggle source

解析list时的path

# 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) click to toggle source

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

# 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) click to toggle source

获取resource_path

# 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) click to toggle source

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

# 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) click to toggle source

字符串sha1

# File lib/cos/util.rb, line 17
def string_sha1(string)
  Digest::SHA1.hexdigest(string)
end