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) uuid.seed() TaskTypes = { NOOP = 'NOOP', UPLOAD_PICTURE = 'UPLOAD_PICTURE' } local task_server = ngx.var.task_server local hty_uc_host = ngx.var.hty_uc_host local file_dir = ngx.var.tmp_file_dir local file local file_name local files = {} local http = require "resty.http" local httpc = http.new() function cleanupString(str, remove) local lcSubStrTab = {} while true do local lcPos = string.find(str, remove) if not lcPos then 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) end local lcMergeStr = "" local lci = 1 while true do if lcSubStrTab[lci] then lcMergeStr = lcMergeStr .. lcSubStrTab[lci] lci = lci + 1 else break end end return lcMergeStr end 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) ngx.log(ngx.INFO, 'htyuc remote_url -> ', remote_url) ngx.log(ngx.INFO, 'Authorization -> ', encrypted_token) local res, err = httpc:request_uri( remote_url, { method = "POST", headers = { ["Host"] = "test.localhost", ["Authorization"] = encrypted_token, }, } ) if not res then ngx.status = ngx.HTTP_UNAUTHORIZED ngx.header.content_type = "application/json; charset=utf-8" ngx.log(ngx.ERR, "Jwt token verify request error ! ") ngx.exit(ngx.HTTP_UNAUTHORIZED) else if 200 ~= res.status then ngx.log(ngx.ERR, "Jwt token verify error ! ", err) ngx.exit(res.status) end end ngx.log(ngx.INFO, "Jwt token verify ok .") end 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.status = ngx.HTTP_UNAUTHORIZED ngx.header.content_type = "application/json; charset=utf-8" ngx.say(cjson.encode(jwt_obj)) ngx.exit(ngx.HTTP_UNAUTHORIZED) end ngx.log(ngx.INFO, "JWT: " .. cjson.encode(jwt_obj)) ngx.log(ngx.INFO, "jwt object : payload : sub " .. jwt_obj.payload.sub) local htytoken = cjson.decode(jwt_obj.payload.sub) ngx.log(ngx.INFO, "htytoken : token_id " .. htytoken.token_id) ngx.log(ngx.INFO, "htytoken : hty_id " .. htytoken.hty_id) --ngx.log(ngx.INFO, "htytoken : app_id " .. htytoken.app_id) ngx.log(ngx.INFO, "htytoken : ts " .. htytoken.ts) --ngx.log(ngx.INFO, "htytoken : roles " .. htytoken.roles) return htytoken end while true do local type, res, err = form:read() if not type then ngx.say("FAILED TO READ *UPLOAD IMAGE* -> ", err) return end local authHeader = ngx.req.get_headers().Authorization -- --if authHeader then -- ngx.log(ngx.INFO, 'Check request authorization Authorization -> ', authHeader) -- -- local htytoken = DecoderJwtToken(authorization) -- --verifyJwtToken(authHeader) --else -- ngx.log(ngx.ERR, 'Request header no authorization ! ') -- ngx.status = 500 -- ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --end --local sudoerToken = ngx.req.get_headers().HtySudoerToken -- --if sudoerToken then -- ngx.log(ngx.INFO, 'Check request authorization HtySudoerToken -> ', sudoerToken) -- -- local htytoken = DecoderJwtToken(authorization) -- --verifyJwtToken(sudoerToken) -- --else -- ngx.log(ngx.ERR, 'Request header no hty sudoer token ! ') -- ngx.status = 500 -- ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --end if type == "header" then --"Content-Disposition","form-data; name=\"files[]\"; filename=\"Song-of-joy.png\"" --"Content-Type","image\/png" local key = res[1] local val = res[2] if key == "Content-Type" then local ext = ngx.re.match(val, [[(\w+)\/(\w+)]], "jo")[2] file_name = uuid() .. "." .. ext end if file_name then file = io.open(file_dir .. "/" .. file_name, "w+") ngx.log(ngx.INFO, "FILENAME -> ", file_name) if not file then ngx.say("failed to open file ", file_name) return end end elseif type == "body" then if file then file:write(res) -- sha1:update(res) end elseif type == "part_end" then if file then file:close() table.insert(files, file_name) end -- 这里要重置一下file_name,否则后面的文件保存时会导致前面已保存的文件变成空文件 -- file:flush() 和 io.flush() 都没效果 file_name = nil file = nil elseif type == "eof" then local remote_url = string.format("%s/api/v1/ts/create_task", task_server) ngx.log(ngx.INFO, 'remote_url -> ', remote_url) 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 } }) ngx.log(ngx.INFO, 'UPLOAD_PICTURE *body_text* ->', body_text) local res, err = httpc:request_uri( remote_url, { method = "POST", headers = { ["Content-Type"] = "application/json", ["Authorization"] = ngx.req.get_headers().Authorization, ["HtySudoerToken"] = ngx.req.get_headers().HtySudoerToken }, body = body_text, } ) if 201 ~= res.status then ngx.log(ngx.ERR, "TASK CREATE *FAILED*", err) ngx.say(err) ngx.exit(res.status) end ngx.say(res.body) break else -- do nothing end end