Files
resty_functions/prod/upload/upload.lua
T

69 lines
1.8 KiB
Lua
Raw Normal View History

2021-08-28 22:01:00 +08:00
-- local resty_sha1 = require "resty.sha1"
local upload = require "resty.upload"
-- opm get thibaultcha/lua-resty-jit-uuid
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
local chunk_size = 4096
local form = upload:new(chunk_size)
-- local sha1 = resty_sha1:new()
2021-09-02 12:40:51 +08:00
uuid.seed()
2021-08-28 22:01:00 +08:00
local file
local file_name
local files = {}
2021-08-28 22:01:00 +08:00
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "header" then
file_name = string.format("/tmp/%s.jpeg", uuid())
2021-08-28 22:01:00 +08:00
if file_name then
file = io.open(file_name, "w+")
if not file then
ngx.say("failed to open file ", file_name)
return
end
end
elseif typ == "body" then
if file then
file:write(res)
-- sha1:update(res)
end
elseif typ == "part_end" then
file:close()
file = nil
table.insert(files, file_name)
2021-08-28 22:01:00 +08:00
-- local sha1_sum = sha1:final()
-- sha1:reset()
-- my_save_sha1_sum(sha1_sum)
elseif typ == "eof" then
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri(
"http://127.0.0.1:8080/api/v1/ts/task_server/create_task",
{
method = "POST",
body = {
cjson.encode({task_type = 1, data = files})
},
}
)
if 201 ~= res.status then
ngx.log(ngx.ERR, "create task 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
end