37 lines
1.5 KiB
Lua
37 lines
1.5 KiB
Lua
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
|
local strip_path = require("lib.strip_path")
|
|
|
|
-- filename eg. /file_upload/xxxx-xxxx-xxxx-xxxx.mp3
|
|
local function calc_token(filename)
|
|
|
|
-- Upyun upload parameters
|
|
local operator = ngx.var.upyun_operator
|
|
local password = ngx.var.upyun_password
|
|
local bucket = ngx.var.upyun_bucket
|
|
local directory = ngx.var.upyun_directory
|
|
|
|
local expiration = 1800
|
|
local method = "POST"
|
|
|
|
local save_key = "/" .. directory .. "/" .. strip_path.strip_path(filename)
|
|
print("save key...", save_key)
|
|
-- Get RFC1123 data
|
|
local date = ngx.http_time(ngx.time()) -- RFC1123 date
|
|
print("date ...", date)
|
|
-- Generate upyun UCT time
|
|
local utc_time = os.time(os.date("!*t"))
|
|
-- Calculate upyun file expiration
|
|
expiration = utc_time + expiration
|
|
print("expiration ...", expiration)
|
|
-- 不能使用cjson,因为lua的table没有顺序,cjson encode出来的顺序会错乱
|
|
local policy = string.format("{\"bucket\":\"%s\",\"save-key\":\"%s\",\"expiration\":\"%s\",\"date\":\"%s\"}", bucket, save_key, expiration, date)
|
|
local to_be_signed = method .. "&" .. "/" .. bucket .. "&" .. date .. "&" .. ngx.encode_base64(policy)
|
|
print("to be signed ...", to_be_signed)
|
|
local hmac_sha1 = ngx.hmac_sha1(ngx.md5(password), to_be_signed)
|
|
local signature = ngx.encode_base64(hmac_sha1)
|
|
|
|
return { token = string.format("UPYUN %s:%s", operator, signature), policy = policy }
|
|
end
|
|
|
|
|
|
return calc_token |