Class: Qinium::PutPolicy

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/qinium/put_policy.rb

Constant Summary collapse

DEFAULT_AUTH_SECONDS =
300
PARAMS =
{
  # 字符串类型参数
  scope: "scope",
  is_prefixal_scope: "isPrefixalScope",
  save_key: "saveKey",
  end_user: "endUser",
  return_url: "returnUrl",
  return_body: "returnBody",
  callback_url: "callbackUrl",
  callback_host: "callbackHost",
  callback_body: "callbackBody",
  callback_body_type: "callbackBodyType",
  persistent_ops: "persistentOps",
  persistent_notify_url: "persistentNotifyUrl",
  persistent_pipeline: "persistentPipeline",

  # 数值类型参数
  deadline: "deadline",
  insert_only: "insertOnly",
  fsize_min: "fsizeMin",
  fsize_limit: "fsizeLimit",
  detect_mime: "detectMime",
  mime_limit: "mimeLimit",
  uphosts: "uphosts",
  global: "global",
  delete_after_days: "deleteAfterDays",
  file_type: "fileType"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank?, #encode_entry_uri, #logger, #to_query_string, #urlsafe_base64_decode, #urlsafe_base64_encode

Constructor Details

#initialize(config, bucket: nil, key: nil, expires_in: DEFAULT_AUTH_SECONDS) ⇒ PutPolicy

Returns a new instance of PutPolicy.



42
43
44
45
46
# File 'lib/qinium/put_policy.rb', line 42

def initialize(config, bucket: nil, key: nil, expires_in: DEFAULT_AUTH_SECONDS)
  @config = config
  scope!(bucket || config.bucket, key)
  expires_in! expires_in
end

Instance Attribute Details

#bucket ⇒ Object (readonly)

Returns the value of attribute bucket.



40
41
42
# File 'lib/qinium/put_policy.rb', line 40

def bucket
  @bucket
end

#config ⇒ Object (readonly)

Returns the value of attribute config.



40
41
42
# File 'lib/qinium/put_policy.rb', line 40

def config
  @config
end

#key ⇒ Object (readonly)

Returns the value of attribute key.



40
41
42
# File 'lib/qinium/put_policy.rb', line 40

def key
  @key
end

Instance Method Details

#expires_in!(seconds) ⇒ Object



61
62
63
# File 'lib/qinium/put_policy.rb', line 61

def expires_in!(seconds)
  @deadline = Time.now.to_i + seconds
end

#scope!(bucket, key = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/qinium/put_policy.rb', line 48

def scope!(bucket, key = nil)
  @bucket = bucket
  @key    = key

  @scope = if key.nil?
             # 新增语义,文件已存在则失败
             bucket
           else
             # 覆盖语义,文件已存在则直接覆盖
             "#{bucket}:#{key}"
           end
end

#to_json(*_args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/qinium/put_policy.rb', line 65

def to_json(*_args)
  args = {}

  PARAMS.each_pair do |key, fld|
    val = __send__(key)
    args[fld] = val unless val.nil?
  end

  args.to_json
end

#to_token ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/qinium/put_policy.rb', line 76

def to_token
  access_key = config.access_key
  secret_key = config.secret_key

  ### 生成待签名字符串
  encoded_put_policy = urlsafe_base64_encode(to_json)

  ### 生成数字签名
  sign = Auth.calculate_hmac_sha1_digest(secret_key, encoded_put_policy)
  encoded_sign = urlsafe_base64_encode(sign)

  ### 生成上传授权凭证
  "#{access_key}:#{encoded_sign}:#{encoded_put_policy}"
end