Files
resty_functions/prod/upload/upload.lua
T
木逸辰 56a03f3cf0 add variable (#11)
* add variable

* add nginx config file

* update path
2021-09-04 12:38:15 +08:00

68 lines
1.7 KiB
Lua

local upload = require "resty.upload"
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
local chunk_size = 4096
local form = upload:new(chunk_size)
uuid.seed()
local task_server = ngx.var.task_server
local file_dir = ngx.var.tmp_file_dir
local file
local file_name
local files = {}
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("%s/%s.jpeg", file_dir, uuid())
ngx.log(ngx.ERR, "file name", file_name)
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)
elseif typ == "eof" then
local http = require "resty.http"
local httpc = http.new()
local remote_url = string.format("%s/api/v1/ts/task_server/create_task", task_server)
local res, err = httpc:request_uri(
remote_url,
{
method = "POST",
body = {
cjson.encode({task_type = 1, data = files, created_by = 'openresty'})
},
}
)
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)
break
else
-- do nothing
end
end