2023-01-17 15:41:52 +08:00
|
|
|
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
|
|
|
|
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
|
|
|
|
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
|
|
|
|
|
|
|
|
|
local upload = require "resty.upload"
|
2023-11-06 01:26:09 +08:00
|
|
|
local uuid = require("lib.my_uuid")
|
2023-01-17 15:41:52 +08:00
|
|
|
local http = require "resty.http"
|
|
|
|
|
local pl = require "pl.pretty"
|
|
|
|
|
local verify = require('lib.jwt_verify')
|
|
|
|
|
local authHeader = ngx.req.get_headers().Authorization
|
|
|
|
|
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
|
|
|
|
|
|
|
|
|
local httpc = http:new()
|
|
|
|
|
|
|
|
|
|
ngx.log(ngx.INFO, "authHeader ", authHeader)
|
|
|
|
|
ngx.log(ngx.INFO, "sudoerToken ", sudoerToken)
|
|
|
|
|
|
|
|
|
|
verify(httpc, authHeader, sudoerToken)
|
|
|
|
|
|
|
|
|
|
local function read_form_file()
|
|
|
|
|
local chunk_size = 4096
|
|
|
|
|
local form, err = upload:new(chunk_size)
|
|
|
|
|
if not form then
|
|
|
|
|
ngx.log(ngx.ERR, "failed to new upload: ", err)
|
|
|
|
|
ngx.exit(500)
|
|
|
|
|
end
|
2023-11-06 02:01:00 +08:00
|
|
|
--uuid.seed()
|
2023-01-17 15:41:52 +08:00
|
|
|
|
|
|
|
|
local file_dir = ngx.var.tmp_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
|
|
|
|
|
|
|
|
|
|
while true do
|
|
|
|
|
local type, res, err = form:read()
|
|
|
|
|
|
|
|
|
|
if not type then
|
|
|
|
|
ngx.say("FAILED TO READ *UPLOAD IMAGE* -> ", err)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if type == "header" then
|
|
|
|
|
--"Content-Disposition","form-data; name=\"files[]\"; filename=\"Song-of-joy.png\""
|
|
|
|
|
--"Content-Type","image\/png"
|
|
|
|
|
local key = res[1]
|
|
|
|
|
local val = res[2]
|
|
|
|
|
if key == "Content-Type" then
|
|
|
|
|
local ext = ngx.re.match(val, [[(\w+)\/(\w+)]], "jo")[2]
|
2023-11-06 02:03:54 +08:00
|
|
|
file_name = uuid.uuid() .. "." .. ext
|
2023-01-17 15:41:52 +08:00
|
|
|
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.say("failed to open file ", file_name)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
elseif type == "body" then
|
|
|
|
|
if file then
|
|
|
|
|
file:write(res)
|
|
|
|
|
-- sha1:update(res)
|
|
|
|
|
end
|
|
|
|
|
elseif type == "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)
|
|
|
|
|
if len == 0 then
|
|
|
|
|
ngx.status = 500
|
|
|
|
|
ngx.log(ngx.ERR, "No images to compress!")
|
|
|
|
|
return ;
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
for i = 1, len do
|
|
|
|
|
ngx.log(ngx.INFO, 'COMPRESS FILE -> ', files[i])
|
|
|
|
|
|
|
|
|
|
-- Compress image
|
2023-01-19 21:47:38 +08:00
|
|
|
local incoming = file_dir .. "/" .. files[i]
|
2023-11-06 02:03:54 +08:00
|
|
|
output_file[i] = file_dir .. "/" .. uuid.uuid() .. ".jpg"
|
2023-01-17 15:41:52 +08:00
|
|
|
|
|
|
|
|
ngx.log(ngx.INFO, 'INPUT FILE -> ', incoming)
|
|
|
|
|
ngx.log(ngx.INFO, 'OUTPUT FILE -> ', output_file[i])
|
|
|
|
|
|
|
|
|
|
local cmd = convert .. " -resize 800x600 " .. incoming .. " " .. output_file[i]
|
|
|
|
|
ngx.log(ngx.INFO, 'CMD COMPRESS RESIZE -> ', cmd)
|
|
|
|
|
local handle = io.popen(cmd)
|
|
|
|
|
local result = handle:read("*a")
|
|
|
|
|
ngx.log(ngx.INFO, 'CMD RESIZE -> RESULT ', pl.dump(result))
|
|
|
|
|
handle:close()
|
|
|
|
|
|
|
|
|
|
-- Upload compressed file to upyun
|
2023-01-17 19:39:08 +08:00
|
|
|
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)
|
2023-01-17 15:41:52 +08:00
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
else
|
|
|
|
|
-- do nothing
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
read_form_file()
|
|
|
|
|
|