Files
resty_functions/prod/upload/upload.lua
T

56 lines
1.4 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
ngx.say("...FILE UPLOAD...")
local file
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
-- local file_name = my_get_file_name(res)
2021-09-02 12:40:51 +08:00
local file_name = string.format("/tmp/%s.jpeg", uuid())
2021-08-28 22:01:00 +08:00
ngx.say("...CREATE FILE....")
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
ngx.say("...WRITE DATA...")
file:write(res)
-- sha1:update(res)
end
elseif typ == "part_end" then
ngx.say("...FINSIHED WRITTING FILE...")
file:close()
file = nil
-- local sha1_sum = sha1:final()
-- sha1:reset()
-- my_save_sha1_sum(sha1_sum)
elseif typ == "eof" then
2021-08-28 22:03:32 +08:00
-- todo weli : create task -> task_server -> generate_task()
2021-08-28 22:01:00 +08:00
ngx.say("...NO MORE FILE...")
break
else
-- do nothing
end
end