Files
resty_functions/upload.lua
T

212 lines
6.2 KiB
Lua
Raw Normal View History

2021-11-27 09:44:08 +08:00
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"
2021-11-25 00:29:25 +08:00
2021-08-28 22:01:00 +08:00
local chunk_size = 4096
local form = upload:new(chunk_size)
2021-09-02 12:40:51 +08:00
uuid.seed()
2021-08-28 22:01:00 +08:00
2021-11-28 10:52:23 +08:00
local TaskTypes = {
2021-09-21 10:09:25 +08:00
NOOP = 'NOOP',
UPLOAD_PICTURE = 'UPLOAD_PICTURE'
}
2021-09-04 12:38:15 +08:00
local task_server = ngx.var.task_server
2021-11-28 11:09:36 +08:00
local htyuc = ngx.var.htyuc
ngx.log(ngx.INFO, "TASK_SERVER -> ", task_server)
ngx.log(ngx.INFO, "HTYUC -> ", htyuc)
2021-09-04 12:38:15 +08:00
local file_dir = ngx.var.tmp_file_dir
2021-08-28 22:01:00 +08:00
local file
local file_name
local files = {}
2021-09-04 12:38:15 +08:00
2021-11-27 09:07:11 +08:00
local http = require "resty.http"
local httpc = http.new()
2021-11-28 10:52:23 +08:00
local function cleanupString(str, remove)
2021-11-25 00:29:25 +08:00
local lcSubStrTab = {}
while true do
2021-11-27 09:44:08 +08:00
local lcPos = string.find(str, remove)
2021-11-25 00:29:25 +08:00
if not lcPos then
2021-11-27 09:44:08 +08:00
lcSubStrTab[#lcSubStrTab + 1] = str
2021-11-25 00:29:25 +08:00
break
end
2021-11-27 09:44:08 +08:00
local lcSubStr = string.sub(str, 1, lcPos - 1)
lcSubStrTab[#lcSubStrTab + 1] = lcSubStr
str = string.sub(str, lcPos + 1, #str)
2021-11-25 00:29:25 +08:00
end
2021-11-27 09:44:08 +08:00
local lcMergeStr = ""
2021-11-25 00:29:25 +08:00
local lci = 1
while true do
if lcSubStrTab[lci] then
lcMergeStr = lcMergeStr .. lcSubStrTab[lci]
lci = lci + 1
else
break
end
end
return lcMergeStr
end
2021-11-28 15:21:12 +08:00
local function verifyJwtToken(token)
2021-11-27 09:07:11 +08:00
local http = require "resty.http"
local httpc = http.new()
2021-11-28 11:09:36 +08:00
local remote_url = string.format("%s/api/v1/uc/verify_jwt_token", htyuc)
2021-11-27 09:07:11 +08:00
2021-11-28 11:09:36 +08:00
ngx.log(ngx.INFO, 'HTYUC REMOTE_URL -> ', remote_url)
2021-11-28 15:21:12 +08:00
ngx.log(ngx.INFO, 'Authorization -> ', token)
2021-11-27 09:07:11 +08:00
local res, err = httpc:request_uri(
remote_url,
{
method = "POST",
headers = {
["Host"] = "test.localhost",
2021-11-28 15:21:12 +08:00
["Authorization"] = token,
2021-11-27 09:07:11 +08:00
},
}
)
if not res then
ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.header.content_type = "application/json; charset=utf-8"
2021-11-28 14:46:40 +08:00
ngx.log(ngx.ERR, "CAN'T VERIFY JWT TOKEN!")
2021-11-27 09:07:11 +08:00
ngx.exit(ngx.HTTP_UNAUTHORIZED)
else
if 200 ~= res.status then
2021-11-28 14:46:40 +08:00
ngx.log(ngx.ERR, "JWT TOKEN VERIFICATION ERROR!", err)
2021-11-27 09:07:11 +08:00
ngx.exit(res.status)
end
end
2021-11-28 14:46:40 +08:00
ngx.log(ngx.INFO, "JWT TOKEN VERIFICATION PASSED.")
2021-11-27 09:07:11 +08:00
end
2021-11-28 15:21:12 +08:00
local function decodeJwtClaim(jwtClaim)
local jwtKey = "0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE"
local jwtObj = jwt:verify(jwtKey, jwtClaim)
if jwtObj.verified == false then
ngx.log(ngx.WARN, "INVALID TOKEN -> " .. jwtObj.reason)
2021-11-25 22:14:20 +08:00
ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.header.content_type = "application/json; charset=utf-8"
2021-11-28 15:21:12 +08:00
ngx.say(cjson.encode(jwtObj))
2021-11-25 22:14:20 +08:00
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
2021-11-28 15:21:12 +08:00
ngx.log(ngx.INFO, "DECODED JWT CLAIM -> " .. jwtObj)
2021-11-28 10:52:23 +08:00
2021-11-28 15:21:12 +08:00
local htyToken = cjson.decode(jwtObj.payload.sub)
ngx.log(ngx.INFO, "DECODED JWT TOKEN -> " .. htyToken)
return htyToken
2021-11-25 22:14:20 +08:00
end
2021-11-25 00:29:25 +08:00
2021-11-28 15:21:12 +08:00
local authHeader = ngx.req.get_headers().Authorization
local sudoerToken = ngx.req.get_headers().HtySudoerToken
-- if authHeader then
-- ngx.log(ngx.INFO, 'CHECK_AUTH_HEADER -> ', authHeader)
-- local token = decodeJwtClaim(authHeader)
-- verifyJwtToken(token)
-- else
-- ngx.log(ngx.ERR, 'Request header no authorization ! ')
-- ngx.status = 500
-- ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
-- end
--
-- 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
2021-08-28 22:01:00 +08:00
while true do
2021-11-28 10:40:18 +08:00
local type, res, err = form:read()
2021-08-28 22:01:00 +08:00
2021-11-28 10:40:18 +08:00
if not type then
2021-11-18 16:57:52 +08:00
ngx.say("FAILED TO READ *UPLOAD IMAGE* -> ", err)
2021-09-04 12:38:15 +08:00
return
2021-08-28 22:01:00 +08:00
end
2021-11-28 10:40:18 +08:00
if type == "header" then
2021-09-06 18:56:59 +08:00
--"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
2021-09-06 18:56:59 +08:00
end
2021-08-28 22:01:00 +08:00
if file_name then
file = io.open(file_dir .. "/" .. file_name, "w+")
2021-11-27 09:44:08 +08:00
ngx.log(ngx.INFO, "FILENAME -> ", file_name)
2021-08-28 22:01:00 +08:00
if not file then
ngx.say("failed to open file ", file_name)
return
end
end
2021-11-28 10:40:18 +08:00
elseif type == "body" then
2021-08-28 22:01:00 +08:00
if file then
file:write(res)
-- sha1:update(res)
end
2021-11-28 10:40:18 +08:00
elseif type == "part_end" then
if file then
file:close()
table.insert(files, file_name)
end
-- 这里要重置一下file_name,否则后面的文件保存时会导致前面已保存的文件变成空文件
2021-09-12 11:01:41 +08:00
-- file:flush() 和 io.flush() 都没效果
file_name = nil
2021-08-28 22:01:00 +08:00
file = nil
2021-11-28 10:40:18 +08:00
elseif type == "eof" then
2021-09-06 18:56:59 +08:00
local remote_url = string.format("%s/api/v1/ts/create_task", task_server)
2021-09-12 11:49:55 +08:00
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
2021-11-28 10:52:23 +08:00
ngx.log(ngx.INFO, 'Authorization -> ', authHeader)
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
2021-11-24 20:08:31 +08:00
2021-11-27 09:44:08 +08:00
local body_text = cjson.encode({ task_type = TaskTypes.UPLOAD_PICTURE, data = { images = files } })
2021-11-08 16:49:40 +08:00
2021-11-18 16:57:52 +08:00
ngx.log(ngx.INFO, 'UPLOAD_PICTURE *body_text* ->', body_text)
local res, err = httpc:request_uri(
2021-09-04 12:38:15 +08:00
remote_url,
{
method = "POST",
2021-09-06 18:56:59 +08:00
headers = {
2021-11-27 09:44:08 +08:00
["Content-Type"] = "application/json",
2021-11-28 10:52:23 +08:00
["Authorization"] = authHeader,
["HtySudoerToken"] = sudoerToken,
2021-09-06 18:56:59 +08:00
},
2021-11-08 16:49:40 +08:00
body = body_text,
}
)
2021-11-28 11:09:36 +08:00
if res == nil then
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *TASK_SERVER*", err)
end
if 201 ~= res.status then
2021-11-18 16:57:52 +08:00
ngx.log(ngx.ERR, "TASK CREATE *FAILED*", err)
ngx.say(err)
ngx.exit(res.status)
end
ngx.say(res.body)
2021-08-28 22:01:00 +08:00
break
else
-- do nothing
end
2021-09-24 20:18:39 +08:00
end