worker_processes 1;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
server {
listen 8083;
location /upload {
default_type multipart/form-data;
content_by_lua_block {
local resty_md5 = require "resty.md5"
local upload = require "resty.upload"
local cjson = require "cjson"
local chunk_size = 4096
local form = upload:new(chunk_size)
if not form then
ngx.log(ngx.ERR, "failed to new upload: ", err)
ngx.exit(500)
end
local md5 = resty_md5:new()
local file
local upload_path = '/home/suman/uploads/'
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 = upload_path .. ngx.time()
local file_header = res[2]
local i, j = string.find(file_header,"filename=")
if j then
local file_name = string.sub(file_header,j+2,string.len(file_header)-1)
ngx.log(ngx.INFO,"file name is = ",file_name)
file_name = upload_path..ngx.time()..'.'..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
end
elseif typ == "body" then
if file then
file:write(res)
md5:update(res)
end
elseif typ == "part_end" then
file:close()
file = nil
startUpload = false
elseif typ == "eof" then
break
else
-- nothingman
end
end
local digest = md5:final()
md5:reset()
local str = require "resty.string"
ngx.say("md5: ", str.to_hex(digest))
}
}
}
}