From e3cdd92cb309db407c36e2ab7d2fc535e07a6330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Tue, 8 Mar 2022 21:59:25 +0800 Subject: [PATCH] separate image combine logic --- conf/local_macos/admin.conf | 11 +++++++++++ conf/local_macos/test.conf | 4 ++++ scripts/check_file.lua | 16 ++++++++++++++++ scripts/combine.lua | 30 ++++++++++++++++++------------ scripts/upload_combined_image.lua | 9 +++++++++ test_scripts/uri.lua | 1 + 6 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 scripts/check_file.lua create mode 100644 scripts/upload_combined_image.lua create mode 100644 test_scripts/uri.lua diff --git a/conf/local_macos/admin.conf b/conf/local_macos/admin.conf index 173ec23..35eb492 100644 --- a/conf/local_macos/admin.conf +++ b/conf/local_macos/admin.conf @@ -42,10 +42,21 @@ server { location /api/ngx/image/upload { content_by_lua_file $resty_loc/nginx/scripts/upload.lua; } + #Combine image files location /api/ngx/image/combine { content_by_lua_file $resty_loc/nginx/scripts/combine.lua; } + + + location /api/ngx/image/check { + content_by_lua_file $resty_loc/nginx/scripts/check_file.lua; + } + + location /api/ngx/image/upload_combined { + content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua; + } + #Audio file download location /api/ngx/audio/upload { content_by_lua_file $resty_loc/nginx/scripts/upload_audio.lua; diff --git a/conf/local_macos/test.conf b/conf/local_macos/test.conf index 3d75b57..bff4743 100644 --- a/conf/local_macos/test.conf +++ b/conf/local_macos/test.conf @@ -13,5 +13,9 @@ server { location /api/ngx/test/popen { content_by_lua_file $resty_loc/nginx/scripts/tests/popen.lua; } + + location /api/ngx/test/uri { + content_by_lua_file $resty_loc/nginx/scripts/tests/uri.lua; + } } diff --git a/scripts/check_file.lua b/scripts/check_file.lua new file mode 100644 index 0000000..112a84f --- /dev/null +++ b/scripts/check_file.lua @@ -0,0 +1,16 @@ +package.path = package.path .. ';/?.lua'; + +-- https://stackoverflow.com/questions/4990990/check-if-a-file-exists-with-lua +local function file_exists(name) + local f = io.open(name,"r") + if f~=nil then io.close(f) return true else return false end +end + + +local file_to_check = string.match(ngx.var.request_uri, ".*/(.*)") +local file_dir = ngx.var.tmp_file_dir + +local fullpath = file_dir .. "/" .. file_to_check .. ".jpeg"; + +ngx.say(file_exists(fullpath)) + diff --git a/scripts/combine.lua b/scripts/combine.lua index fff9236..2a7d40a 100644 --- a/scripts/combine.lua +++ b/scripts/combine.lua @@ -12,7 +12,10 @@ 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"] -local combined = file_dir .. "/" .. uuid.generate_v4() .. ".jpeg"; + +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 @@ -35,16 +38,19 @@ end local cmd = convert .. " -append " .. table.concat(origin, " ") .. " " .. combined ngx.log(ngx.INFO, 'CMD -> ', cmd) -local result, _, code = os.execute(cmd) -if result and code == 0 then +io.popen(cmd) - ngx.log(ngx.INFO, "IMAGE COMBINED -> ", combined) +ngx.say(combined_uuid) - 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 +-- 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 diff --git a/scripts/upload_combined_image.lua b/scripts/upload_combined_image.lua new file mode 100644 index 0000000..1cddb86 --- /dev/null +++ b/scripts/upload_combined_image.lua @@ -0,0 +1,9 @@ +package.path = package.path .. ';/?.lua'; + +local upyun_upload = require("upyun_upload") + +local file_to_upload = string.match(ngx.var.request_uri, ".*/(.*)") +local file_dir = ngx.var.tmp_file_dir +local fullpath = file_dir .. "/" .. file_to_upload .. ".jpeg"; + +upyun_upload.upload(fullpath) \ No newline at end of file diff --git a/test_scripts/uri.lua b/test_scripts/uri.lua new file mode 100644 index 0000000..a49adf8 --- /dev/null +++ b/test_scripts/uri.lua @@ -0,0 +1 @@ +ngx.say(string.match(ngx.var.request_uri, ".*/(.*)"))