Skip to content

Instantly share code, notes, and snippets.

@shiweifu
Created July 27, 2012 02:40
Show Gist options
  • Save shiweifu/3185907 to your computer and use it in GitHub Desktop.
Save shiweifu/3185907 to your computer and use it in GitHub Desktop.
通过openresty 模块上传文件,并在上传的过程中进行md5计算
local upload = require "resty.upload"
local cjson = require "cjson"
local resty_md5 = require "resty.md5"
local chunk_size = 4096 -- should be set to 4096 or 8192
-- for real-world settings
local form = upload:new(chunk_size)
local data = ""
form:set_timeout(1000) -- 1 sec
local md5 = resty_md5:new()
if not md5 then
ngx.say("failed to create md5 object")
return
end
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "body" then
local ok = md5:update(res)
if not ok then
ngx.say("failed to add data")
return
end
ngx.say("read: ", cjson.encode({typ}))
else
ngx.say("read: ", cjson.encode({typ, res}))
end
if typ == "eof" then
break
end
end
local typ, res, err = form:read()
ngx.say("read: ", cjson.encode({typ, res}))
local digest = md5:final()
local str = require "resty.string"
ngx.say("md5: ", str.to_hex(digest))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment