From ca287ebc1607b33d10a8b7fc7247531abcfed097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Sat, 27 Nov 2021 09:44:08 +0800 Subject: [PATCH] cleanup --- upload.lua | 74 +++++++++++++++++++++++------------------------------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/upload.lua b/upload.lua index a80fa8b..6d44844 100644 --- a/upload.lua +++ b/upload.lua @@ -1,8 +1,9 @@ -local upload = require "resty.upload" -local uuid = require "resty.jit-uuid" -local cjson = require "cjson" -local jwt = require "resty.jwt" -local redis = require "resty.redis" +local upload = require "resty.upload" +local uuid = require "resty.jit-uuid" +local cjson = require "cjson" +--# https://github.com/SkyLothar/lua-resty-jwt +--$ opm get SkyLothar/lua-resty-jwt +local jwt = require "resty.jwt" local chunk_size = 4096 local form = upload:new(chunk_size) @@ -23,31 +24,19 @@ local files = {} local http = require "resty.http" local httpc = http.new() ----- Redis init ------- Connect to redis --- local redis_task_database = redis:new() - --- redis_task_database:set_timeouts(1000, 1000, 1000) - --- local ok, error = redis_task_database:connect("127.0.0.1", 6379) --- if not ok then --- ngx.log(ngx.ERR, "Fail to connect redis: ", error) --- ngx.exit(500) --- end - -function LuaReomve(str,remove) +function cleanupString(str, remove) local lcSubStrTab = {} while true do - local lcPos = string.find(str,remove) + local lcPos = string.find(str, remove) if not lcPos then - lcSubStrTab[#lcSubStrTab+1] = str + lcSubStrTab[#lcSubStrTab + 1] = str break end - local lcSubStr = string.sub(str,1,lcPos-1) - lcSubStrTab[#lcSubStrTab+1] = lcSubStr - str = string.sub(str,lcPos+1,#str) + local lcSubStr = string.sub(str, 1, lcPos - 1) + lcSubStrTab[#lcSubStrTab + 1] = lcSubStr + str = string.sub(str, lcPos + 1, #str) end - local lcMergeStr ="" + local lcMergeStr = "" local lci = 1 while true do if lcSubStrTab[lci] then @@ -60,15 +49,14 @@ function LuaReomve(str,remove) return lcMergeStr end - -function VerifyJwtToken(encrypted_token) +function verifyJwtToken(encrypted_token) local http = require "resty.http" local httpc = http.new() - local remote_url = string.format( "%s/api/v1/uc/verify_jwt_token", hty_uc_host) + local remote_url = string.format("%s/api/v1/uc/verify_jwt_token", hty_uc_host) ngx.log(ngx.INFO, 'htyuc remote_url -> ', remote_url) - -- ngx.log(ngx.INFO, 'Authorization -> ', encrypted_token) + ngx.log(ngx.INFO, 'Authorization -> ', encrypted_token) local res, err = httpc:request_uri( remote_url, @@ -97,12 +85,12 @@ function VerifyJwtToken(encrypted_token) end -function DecoderJwtToken(encrypted_token) +function decodeJwtToken(encrypted_token) local jwt_key = "0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE" local jwt_obj = jwt:verify(jwt_key, encrypted_token) if jwt_obj.verified == false then - ngx.log(ngx.WARN, "Invalid token: ".. jwt_obj.reason) + ngx.log(ngx.WARN, "Invalid token: " .. jwt_obj.reason) ngx.status = ngx.HTTP_UNAUTHORIZED ngx.header.content_type = "application/json; charset=utf-8" ngx.say(cjson.encode(jwt_obj)) @@ -130,22 +118,22 @@ while true do return end - local authorization = ngx.req.get_headers().Authorization - if authorization then - ngx.log(ngx.INFO, 'Check request authorization Authorization -> ', authorization) + local authHeader = ngx.req.get_headers().Authorization + if authHeader then + ngx.log(ngx.INFO, 'Check request authorization Authorization -> ', authHeader) -- local htytoken = DecoderJwtToken(authorization) - VerifyJwtToken(authorization) + verifyJwtToken(authHeader) else ngx.log(ngx.ERR, 'Request header no authorization ! ') ngx.status = 500 ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end - local authorization = ngx.req.get_headers().HtySudoerToken - if authorization then - ngx.log(ngx.INFO, 'Check request authorization HtySudoerToken -> ', authorization) + local sudoerToken = ngx.req.get_headers().HtySudoerToken + if sudoerToken then + ngx.log(ngx.INFO, 'Check request authorization HtySudoerToken -> ', sudoerToken) -- local htytoken = DecoderJwtToken(authorization) - VerifyJwtToken(authorization) + verifyJwtToken(sudoerToken) else ngx.log(ngx.ERR, 'Request header no hty sudoer token ! ') @@ -166,7 +154,7 @@ while true do if file_name then file = io.open(file_dir .. "/" .. file_name, "w+") - ngx.log(ngx.INFO, "FILENAME -> ", file_name) + ngx.log(ngx.INFO, "FILENAME -> ", file_name) if not file then ngx.say("failed to open file ", file_name) return @@ -195,7 +183,7 @@ while true do ngx.log(ngx.INFO, 'Authorization -> ', ngx.req.get_headers().Authorization) ngx.log(ngx.INFO, 'HtySudoerToken -> ', ngx.req.get_headers().HtySudoerToken) - local body_text = cjson.encode({task_type = TaskTypes.UPLOAD_PICTURE, data = {images = files}}) + local body_text = cjson.encode({ task_type = TaskTypes.UPLOAD_PICTURE, data = { images = files } }) ngx.log(ngx.INFO, 'UPLOAD_PICTURE *body_text* ->', body_text) local res, err = httpc:request_uri( @@ -203,9 +191,9 @@ while true do { method = "POST", headers = { - ["Content-Type"] = "application/json", - ["Authorization"] = ngx.req.get_headers().Authorization, - ["HtySudoerToken"] = ngx.req.get_headers().HtySudoerToken + ["Content-Type"] = "application/json", + ["Authorization"] = ngx.req.get_headers().Authorization, + ["HtySudoerToken"] = ngx.req.get_headers().HtySudoerToken }, body = body_text, }