Files
resty_functions/scripts/combine.lua
T

65 lines
1.8 KiB
Lua
Raw Normal View History

-- 指定模块引用目录,否则无法加载同目录下的其他文件
2022-02-08 13:57:15 +08:00
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
2022-02-08 14:14:57 +08:00
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
2022-02-08 13:57:15 +08:00
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
2022-08-23 00:32:24 +08:00
local pl = require "pl.pretty"
2022-08-31 23:42:45 +08:00
local http = require "resty.http"
local verify = require('lib.jwt_verify')
local httpc = http:new()
local authHeader = ngx.req.get_headers().Authorization
local sudoerToken = ngx.req.get_headers().HtySudoerToken
verify(httpc, authHeader, sudoerToken)
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())
2023-01-16 22:53:20 +08:00
local images = req_body["payload"]["images"]
2022-03-08 21:59:25 +08:00
2022-03-09 01:57:15 +08:00
local combined_uuid = uuid.generate_v4()
2023-07-28 03:06:30 +08:00
local output_filename = combined_uuid .. ".jpeg";
local output_filepath = file_dir .. "/" .. output_filename
2022-03-08 21:59:25 +08:00
2023-01-16 22:53:20 +08:00
local len = table.getn(images)
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
2022-08-23 00:04:32 +08:00
2021-09-06 18:56:59 +08:00
local origin = {}
2022-08-23 00:04:32 +08:00
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
2022-08-23 00:04:32 +08:00
for i = 1, len do
2023-01-16 22:53:20 +08:00
ngx.log(ngx.INFO, "COMBINE FILE -> ", images[i])
2023-07-22 23:57:54 +08:00
local incoming = file_dir .. "/" .. images[i]
2022-08-23 00:35:33 +08:00
origin[i] = incoming .. ".png "
2023-04-17 20:24:08 +08:00
local cmd = convert .. " -resize 642x1389 -strip -quality 75% " .. incoming .. " " .. origin[i]
2022-08-23 00:32:24 +08:00
ngx.log(ngx.INFO, 'CMD RESIZE -> ', cmd)
local handle = io.popen(cmd)
local result = handle:read("*a")
ngx.log(ngx.INFO, 'CMD RESIZE -> RESULT ', pl.dump(result))
handle:close()
2022-08-23 00:04:32 +08:00
end
2023-07-28 03:06:30 +08:00
local cmd = convert .. " -append " .. table.concat(origin, " ") .. " " .. output_filepath
2022-08-22 23:59:02 +08:00
ngx.log(ngx.INFO, 'CMD APPEND -> ', cmd)
2022-03-08 21:59:25 +08:00
io.popen(cmd)
2023-07-28 03:06:30 +08:00
ngx.say(output_filename)