Files
resty_functions/scripts/combine.lua
T
2022-03-09 02:06:37 +08:00

57 lines
1.6 KiB
Lua

-- 指定模块引用目录,否则无法加载同目录下的其他文件
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
local upyun_upload = require("upyun_upload")
uuid.seed()
local file_dir = ngx.var.tmp_file_dir
ngx.req.read_body()
local req_body = cjson.decode(ngx.req.get_body_data())
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
local data = req_body["payload"]["images"]
let combined_uuid = uuid.generate_v4()
local combined = file_dir .. "/" .. combined_uuid .. ".jpeg";
local len = table.getn(data)
if len == 0 then
ngx.status = 500
ngx.log(ngx.ERR, "No images to combine!")
return ;
end
local origin = {}
for i = 1, len do
ngx.log(ngx.INFO, "COMBINE FILE ->", data[i])
origin[i] = file_dir .. "/" .. data[i] .. " "
end
-- 从 nginx 变量取
local convert = ngx.var.convert
if not convert then
ngx.status = 500
ngx.log(ngx.ERR, "ImageMagick NOT FOUND.")
return
end
local cmd = convert .. " -append " .. table.concat(origin, " ") .. " " .. combined
ngx.log(ngx.INFO, 'CMD -> ', cmd)
io.popen(cmd)
ngx.say(combined_uuid)
-- local result, _, code = os.execute(cmd)
--
-- if result and code == 0 then
-- ngx.log(ngx.INFO, "IMAGE COMBINED -> ", combined)
--
-- ngx.log(ngx.INFO, "Upload combined image " .. combined .. " to upyun .")
--
-- upyun_upload.upload(combined)
-- else
-- ngx.status = 500
-- ngx.log(ngx.ERR, "IMAGE COMBINE *FAILED*")
-- end