Skip to content

Instantly share code, notes, and snippets.

@benjaminbarbe
Created March 20, 2013 16:25
Show Gist options
  • Save benjaminbarbe/5206051 to your computer and use it in GitHub Desktop.
Save benjaminbarbe/5206051 to your computer and use it in GitHub Desktop.
local resty_sha1 = require "resty.sha1"
local upload = require "resty.upload"
local chunk_size = 4096
local form = upload:new(chunk_size)
local sha1 = resty_sha1:new()
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)
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
local sha1_sum = sha1:final()
sha1:reset()
my_save_sha1_sum(sha1_sum)
elseif typ == "eof" then
break
else
-- do nothing
end
end
@pgaertig
Copy link

Damn, I started digging into lua mod and it seems very straightforward. Implementing resumable upload should be quite easy. ngx-up-mod gives my users ability to upload 50GB file via browser on shaky wifi. It is just POST/PUT with 2 headers and raw body in several requests, no multi-part stuff at all.

@pgaertig
Copy link

Hah! I would like to thank you for encouraging me, so I had few evenings with Lua and here is the result: https://github.com/pgaertig/nginx-big-upload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment