fix(ngx): form_upload_to_compress errors; moicen music-room nginx vars for Lua uploads

Made-with: Cursor
This commit is contained in:
2026-04-28 08:25:09 +08:00
parent e8898efc0c
commit e053cab89e
2 changed files with 200 additions and 64 deletions
+89 -36
View File
@@ -17,6 +17,34 @@ ngx.log(ngx.INFO, "sudoerToken ", sudoerToken)
verify(httpc, authHeader, sudoerToken)
--- @param disposition_val string raw Content-Disposition header value
local function filename_from_disposition(disposition_val)
if not disposition_val then
return nil
end
local m = ngx.re.match(disposition_val, [[filename\*=UTF-8''([^;\s]+)]], "ijo")
if m and m[1] then
return ngx.unescape_uri(m[1])
end
m = ngx.re.match(disposition_val, [[filename="([^"]+)"]], "ijo")
if m and m[1] then
return m[1]
end
m = ngx.re.match(disposition_val, [[filename=([^;\s]+)]], "ijo")
if m and m[1] then
return m[1]
end
return nil
end
local function extension_from_filename(fn)
if not fn then
return nil
end
local em = ngx.re.match(fn, [[\.([A-Za-z0-9]+)$]], "ijo")
return em and em[1] or nil
end
local function read_form_file()
local chunk_size = 4096
local form, err = upload:new(chunk_size)
@@ -24,77 +52,107 @@ local function read_form_file()
ngx.log(ngx.ERR, "failed to new upload: ", err)
ngx.exit(500)
end
--uuid.seed()
local file_dir = ngx.var.tmp_file_dir
local convert = ngx.var.convert
if not file_dir or file_dir == "" then
ngx.status = 500
ngx.log(ngx.ERR, "tmp_file_dir is not set in nginx (set $tmp_file_dir).")
ngx.say("server misconfiguration: tmp_file_dir")
return
end
if not convert or convert == "" then
ngx.status = 500
ngx.log(ngx.ERR, "ImageMagick NOT FOUND (set $convert).")
ngx.say("server misconfiguration: convert")
return
end
-- 确保临时目录存在(避免 worker 无权限写根路径等导致 io.open 失败)
os.execute("mkdir -p " .. file_dir)
local file
local file_name
local files = {}
local output_file = {}
local convert = ngx.var.convert
if not convert then
ngx.status = 500
ngx.log(ngx.ERR, "ImageMagick NOT FOUND.")
return
end
--- 当前 multipart 部件里 Content-Disposition 里的文件名(用于扩展名回退)
local pending_disp_filename
while true do
local type, res, err = form:read()
local typ, res, read_err = form:read()
if not type then
ngx.say("FAILED TO READ *UPLOAD IMAGE* -> ", err)
if not typ then
ngx.status = 500
ngx.log(ngx.ERR, "FAILED TO READ upload: ", read_err)
ngx.say("FAILED TO READ *UPLOAD IMAGE* -> ", read_err)
return
end
if type == "header" then
--"Content-Disposition","form-data; name=\"files[]\"; filename=\"Song-of-joy.png\""
--"Content-Type","image\/png"
if typ == "header" then
local key = res[1]
local val = res[2]
if key == "Content-Type" then
local ext = ngx.re.match(val, [[(\w+)\/(\w+)]], "jo")[2]
if key == "Content-Disposition" then
pending_disp_filename = filename_from_disposition(val)
elseif key == "Content-Type" then
local mt = ngx.re.match(val, [[(\w+)/([\w\-\+\.]+)]], "jo")
local ext = mt and mt[2] or nil
if ext then
ext = string.lower(ext)
if ext == "jpeg" then
ext = "jpg"
end
end
if not ext and pending_disp_filename then
ext = extension_from_filename(pending_disp_filename)
if ext then
ext = string.lower(ext)
end
end
if not ext then
ngx.status = 400
ngx.log(ngx.ERR, "cannot derive extension; Content-Type=", val)
ngx.say("cannot derive image file extension")
return
end
file_name = uuid.uuid() .. "." .. ext
end
if file_name then
file = io.open(file_dir .. "/" .. file_name, "w+")
ngx.log(ngx.INFO, "FILENAME -> ", file_name)
if not file then
ngx.status = 500
ngx.log(ngx.ERR, "failed to open temp file under ", file_dir, " name=", file_name)
ngx.say("failed to open file ", file_name)
return
end
pending_disp_filename = nil
end
elseif type == "body" then
elseif typ == "body" then
if file then
file:write(res)
-- sha1:update(res)
end
elseif type == "part_end" then
elseif typ == "part_end" then
if file then
file:close()
table.insert(files, file_name)
end
-- 这里要重置一下file_name,否则后面的文件保存时会导致前面已保存的文件变成空文件
-- file:flush() 和 io.flush() 都没效果
file_name = nil
file = nil
elseif type == "eof" then
-- TODO : Compress image and upload to upyun
local len = table.getn(files)
pending_disp_filename = nil
elseif typ == "eof" then
local len = #files
if len == 0 then
ngx.status = 500
ngx.log(ngx.ERR, "No images to compress!")
return ;
ngx.say("no image data received")
return
end
for i = 1, len do
ngx.log(ngx.INFO, 'COMPRESS FILE -> ', files[i])
-- Compress image
local incoming = file_dir .. "/" .. files[i]
output_file[i] = file_dir .. "/" .. uuid.uuid() .. ".jpg"
@@ -108,22 +166,17 @@ local function read_form_file()
ngx.log(ngx.INFO, 'CMD RESIZE -> RESULT ', pl.dump(result))
handle:close()
-- Upload compressed file to upyun
local upyun_upload = require("lib.upyun_upload")
ngx.log(ngx.INFO, 'UPLOAD COMPRESSED IMAGE -> fullpath -> ', output_file[i])
upyun_upload.upload(output_file[i], nil)
upyun_upload.upload(output_file[i], nil)
end
break
else
-- do nothing
end
end
end
read_form_file()