diff --git a/README.md b/README.md index 624eefc..0eb255f 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,12 @@ weli@ovpn-12-178:/u/l/o/o/n/resty_funcs ➤ ``` +Pre-Install lua third module +```bash +luarocks install lua-resty-jit-uuid +luarocks install lua-resty-jwt +``` + diff --git a/music-room-dev.conf b/music-room-dev.conf index 8376426..787d10d 100644 --- a/music-room-dev.conf +++ b/music-room-dev.conf @@ -50,6 +50,10 @@ server { location /api/ngx/audio/convert { content_by_lua_file $resty_loc/resty_funcs/convert_audio.lua; } + #Upyun token + location /api/ngx/upyun_token{ + content_by_lua_file $resty_loc/resty_funcs/upyun_token.lua; + } #Static file server location /file_upload { default_type ""; diff --git a/music-room-test.conf b/music-room-test.conf index 8d3bcd3..01b827d 100644 --- a/music-room-test.conf +++ b/music-room-test.conf @@ -49,7 +49,11 @@ server { set $task_server "http://127.0.0.1:8080"; # task server host set $htyuc "http://127.0.0.1:3000"; #htyuc host - set $resty_loc "/usr/local/opt/openresty"; # MacOS + set $upyun_operator "moicen"; + set $upyun_passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN"; + + #set $resty_loc "/usr/local/opt/openresty"; # MacOS + set $resty_loc "/usr/local/openresty"; # CentOS set $convert "/usr/local/bin/convert"; location / { @@ -87,6 +91,10 @@ server { location /api/ngx/audio/convert { content_by_lua_file $resty_loc/resty_funcs/convert_audio.lua; } + #Upyun token + location /api/ngx/upyun_token{ + content_by_lua_file $resty_loc/resty_funcs/upyun_token.lua; + } #Static file server location /file_upload { default_type ""; diff --git a/strip_path.lua b/strip_path.lua new file mode 100644 index 0000000..81761bf --- /dev/null +++ b/strip_path.lua @@ -0,0 +1,37 @@ +local strip_path = {} + +--获取路径 +function strip_path.strip_filename(filename) + return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system + --return string.match(filename, “(.+)\\[^\\]*%.%w+$”) — windows +end + +--获取文件名 +function strip_path.strip_path(filename) + return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system + --return string.match(filename, “.+\\([^\\]*%.%w+)$”) — *nix system +end + +--去除扩展名 +function strip_path.strip_extension(filename) + local idx = filename:match(".+()%.%w+$") + if(idx) then + return filename:sub(1, idx-1) + else + return filename + end +end + +--获取扩展名 +function strip_path.get_extension(filename) + return filename:match(".+%.(%w+)$") +end + +-- Test strip_path module +--local paths = "/use/local/openresty/nginx/movies/fffff.tar.gz" +--print (strip_path.strip_filename(paths)) +--print (strip_path.strip_path(paths)) +--print (strip_path.strip_extension(paths)) +--print (strip_path.get_extension(paths)) + +return strip_path \ No newline at end of file diff --git a/upyun.lua b/upyun.lua new file mode 100644 index 0000000..f95c030 --- /dev/null +++ b/upyun.lua @@ -0,0 +1,69 @@ +strip_path = require("strip_path") + +local uuid = require "resty.jit-uuid" +uuid.seed() + +local cjson = require "cjson" + +-- Upyun upload parameters +local upyun_operator = ngx.var.$upyun_operator +local upyun_passwd = ngx.var.upyun_passwd + +local upyun_expiration = 1800 +local upyun_method = "POST" + +local function calculate_authorization(bucket, remote_dir, local_filename_with_path) + + -- Get filename with extension without path + local upyun_filename = strip_path.strip_path(local_filename_with_path) + ngx.log(ngx.INFO, "UPYUN FILENAME -> ", upyun_filename) + + -- Connect upyun save key + local upyun_save_key = "/" .. remote_dir .. "/" .. upyun_filename + ngx.log(ngx.INFO, "UPYUN SAVE KEY -> ", upyun_save_key) + + -- Get RFC1123 data + local upyun_date = ngx.http_time(ngx.time()) -- RFC1123 date + ngx.log(ngx.INFO, "UPYUN DATE RFC1123 FORMAT -> ", upyun_date) + + -- Generate upyun UCT time + local upyun_utc_time = os.time(os.date("!*t")) + ngx.log(ngx.INFO, "UPYUN UTC TIME -> ", upyun_utc_time) + + -- Calculate upyun file expiration + local upyun_file_expiration = upyun_utc_time + upyun_expiration + ngx.log(ngx.INFO, "UPYUN FILE EXPIRATION -> ", upyun_file_expiration) + + local upyun_policy_json = string.format("{\"bucket\":\"%s\",\"save-key\":\"%s\",\"expiration\":\"%s\",\"date\":\"%s\"}", bucket, upyun_save_key,upyun_file_expiration,upyun_date) + + ngx.log(ngx.INFO, "UPYUN POLICY JSON -> ", upyun_policy_json) + + -- Calculate upyun policy + local upyun_policy = ngx.encode_base64(upyun_policy_json) + ngx.say("UPYUN POLICY -> ", upyun_policy) + + ---- Calculate MD5 passwd + local upyun_md5_passwd = ngx.md5(upyun_passwd) + ngx.log(ngx.INFO, "UPYUN MD5 PASSWD -> ", upyun_md5_passwd) + + ---- Connect to be signed string + local upyun_to_be_signed_string = upyun_method .. "&" .. "/" .. bucket .. "&" .. upyun_date .. "&" .. upyun_policy + ngx.log(ngx.INFO, "UPYUN TO BE SIGNED STRING -> ", upyun_to_be_signed_string) + + -- Calculate upyun token hmac sha1 + local upyun_token_hmac_sha1 = ngx.hmac_sha1(upyun_md5_passwd, upyun_to_be_signed_string) + --ngx.log(ngx.INFO, "UPYUN TOKEN HMAC SHA1 -> ", upyun_token_hmac_sha1) + + -- Calculate upyun signature + local upyun_signature = ngx.encode_base64(upyun_token_hmac_sha1) + ngx.log(ngx.INFO, "UPYUN SIGNATURE -> ", upyun_signature) + + -- Connect upyun upload file authorization + local upyun_upload_file_authorization = string.format("UPYUN %s:%s", upyun_operator, upyun_signature) + ngx.log(ngx.INFO, "UPYUN UPLOAD FILE AUTHORIZATION -> ", upyun_upload_file_authorization) + + return upyun_upload_file_authorization +end + + +