2022-02-07 19:04:21 +08:00
|
|
|
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
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
|
|
|
|
2023-08-09 00:15:43 +08:00
|
|
|
-- Requires
|
2023-11-06 01:26:09 +08:00
|
|
|
local uuid = require("lib.my_uuid")
|
2021-12-30 00:46:59 +08:00
|
|
|
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')
|
2023-08-09 15:53:03 +08:00
|
|
|
local get_wx_media = require("lib.wx_download")
|
2022-08-31 23:42:45 +08:00
|
|
|
|
2023-08-09 00:15:43 +08:00
|
|
|
-- Verify token
|
2022-08-31 23:42:45 +08:00
|
|
|
local httpc = http:new()
|
|
|
|
|
local authHeader = ngx.req.get_headers().Authorization
|
|
|
|
|
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
|
|
|
|
verify(httpc, authHeader, sudoerToken)
|
2023-11-06 02:01:00 +08:00
|
|
|
--uuid.seed()
|
2021-09-06 18:56:59 +08:00
|
|
|
|
2023-08-09 00:15:43 +08:00
|
|
|
-- Get ngx var
|
2021-09-06 18:56:59 +08:00
|
|
|
local file_dir = ngx.var.tmp_file_dir
|
2023-08-09 00:15:43 +08:00
|
|
|
|
|
|
|
|
-- Read request body
|
2021-09-06 18:56:59 +08:00
|
|
|
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-08-09 00:15:43 +08:00
|
|
|
|
2023-01-16 22:53:20 +08:00
|
|
|
local images = req_body["payload"]["images"]
|
2022-03-08 21:59:25 +08:00
|
|
|
|
2023-08-09 15:51:16 +08:00
|
|
|
local image_origin = req_body["payload"]["image_origin"]
|
2023-08-07 21:39:12 +08:00
|
|
|
|
2023-08-09 00:15:43 +08:00
|
|
|
-- Judge payload image origin
|
|
|
|
|
if image_origin == "WX" then
|
|
|
|
|
ngx.log(ngx.INFO, "IMAGE ORIGIN -> ", image_origin)
|
|
|
|
|
elseif image_origin == "LOCAL" then
|
|
|
|
|
ngx.log(ngx.INFO, "IMAGE ORIGIN -> ", image_origin)
|
|
|
|
|
else
|
2023-08-10 16:29:46 +08:00
|
|
|
image_origin = "LOCAL"
|
|
|
|
|
ngx.log(ngx.INFO, "IMAGE ORIGIN default to LOCAL")
|
2023-08-09 00:15:43 +08:00
|
|
|
end
|
2022-03-08 21:59:25 +08:00
|
|
|
|
2023-08-09 00:15:43 +08:00
|
|
|
-- Judge payload images para length
|
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!")
|
2021-12-30 00:46:59 +08:00
|
|
|
return ;
|
2021-09-24 20:18:39 +08:00
|
|
|
end
|
2022-08-23 00:04:32 +08:00
|
|
|
|
2023-08-09 00:15:43 +08:00
|
|
|
-- If combine images from WX
|
|
|
|
|
local wx_download_images = {}
|
|
|
|
|
if image_origin == "WX" then
|
|
|
|
|
for i = 1, len do
|
|
|
|
|
ngx.log(ngx.INFO, "DOWNLOAD WX URL -> ", images[i])
|
|
|
|
|
wx_download_images[i] = get_wx_media(httpc, images[i], cjson, pl);
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Prepare combined file name and path
|
2023-11-06 01:22:29 +08:00
|
|
|
local combined_uuid = uuid.uuid()
|
2023-08-09 00:15:43 +08:00
|
|
|
local output_filename = combined_uuid .. ".jpeg";
|
|
|
|
|
local output_filepath = file_dir .. "/" .. output_filename
|
|
|
|
|
|
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
|
|
|
|
2021-12-30 00:46:59 +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-08-07 21:39:12 +08:00
|
|
|
|
|
|
|
|
local incoming
|
|
|
|
|
|
|
|
|
|
if image_origin == "WX" then
|
2023-08-09 00:15:43 +08:00
|
|
|
ngx.log(ngx.INFO, "DOWNLOAD WX FILE -> ", wx_download_images[i])
|
|
|
|
|
incoming = wx_download_images[i]
|
|
|
|
|
elseif image_origin == "LOCAL" then
|
|
|
|
|
ngx.log(ngx.INFO, "LOCAL FILE -> ", images[i])
|
2023-08-07 21:39:12 +08:00
|
|
|
incoming = file_dir .. "/" .. images[i]
|
2023-08-09 00:15:43 +08:00
|
|
|
else
|
|
|
|
|
ngx.status = 400
|
|
|
|
|
ngx.log(ngx.ERR, "COMBINE REQUEST PAYLOAD IMAGES ORIGIN NOT FOUND.")
|
|
|
|
|
return
|
2023-08-07 21:39:12 +08:00
|
|
|
end
|
|
|
|
|
|
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)
|