Files
resty_functions/scripts/combine.lua
T

49 lines
1.4 KiB
Lua
Raw Normal View History

-- 指定模块引用目录,否则无法加载同目录下的其他文件
package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
2022-02-08 00:25:20 +08:00
local upyun_upload = require("upyun_upload")
2021-09-06 18:56:59 +08:00
uuid.seed()
local file_dir = ngx.var.tmp_file_dir
ngx.req.read_body()
local req_body = cjson.decode(ngx.req.get_body_data())
2021-09-24 20:18:39 +08:00
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
local data = req_body["data"]["images"]
2021-09-06 18:56:59 +08:00
local combined = file_dir .. "/" .. uuid.generate_v4() .. ".jpeg";
local len = table.getn(data)
2021-09-24 20:18:39 +08:00
if len == 0 then
ngx.status = 500
2021-11-20 03:37:12 +08:00
ngx.log(ngx.ERR, "No images to combine!")
return ;
2021-09-24 20:18:39 +08:00
end
2021-09-06 18:56:59 +08:00
local origin = {}
for i = 1, len do
ngx.log(ngx.INFO, "COMBINE FILE ->", data[i])
2021-09-06 18:56:59 +08:00
origin[i] = file_dir .. "/" .. data[i] .. " "
end
2021-09-12 11:01:41 +08:00
-- 从 nginx 变量取
2021-09-12 11:29:30 +08:00
local convert = ngx.var.convert
2021-09-12 11:01:41 +08:00
if not convert then
ngx.status = 500
ngx.log(ngx.ERR, "ImageMagick NOT FOUND.")
return
2021-09-06 18:56:59 +08:00
end
2021-09-07 21:54:52 +08:00
local cmd = convert .. " -append " .. table.concat(origin, " ") .. " " .. combined
2021-09-24 20:18:39 +08:00
ngx.log(ngx.INFO, 'CMD -> ', cmd)
local result, _, code = os.execute(cmd)
2021-09-07 21:54:52 +08:00
if result and code == 0 then
2021-11-20 03:37:12 +08:00
ngx.log(ngx.INFO, "IMAGE COMBINED -> ", combined)
ngx.log(ngx.INFO, "Upload combined image " .. combined .. " to upyun .")
2022-02-08 00:25:20 +08:00
upyun_upload.upload(combined)
2021-09-07 21:54:52 +08:00
else
ngx.status = 500
2021-11-20 03:37:12 +08:00
ngx.log(ngx.ERR, "IMAGE COMBINE *FAILED*")
2021-09-24 20:18:39 +08:00
end