From b4835ef30579d6314bcb3f0a2c657f642b540d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Sun, 12 Nov 2023 02:25:29 +0800 Subject: [PATCH] add file exists checking in my_uuid --- ...eate_combine_wx_images_task.lua.deprecated | 21 -------------- scripts/lib/commons.lua | 13 +++++++++ scripts/lib/my_uuid.lua | 21 ++++++++++---- scripts/upload_audio.lua.deprecated | 21 -------------- scripts/wx_upload_single.lua.deprecated | 29 ------------------- 5 files changed, 28 insertions(+), 77 deletions(-) delete mode 100644 scripts/create_combine_wx_images_task.lua.deprecated create mode 100644 scripts/lib/commons.lua delete mode 100644 scripts/upload_audio.lua.deprecated delete mode 100644 scripts/wx_upload_single.lua.deprecated diff --git a/scripts/create_combine_wx_images_task.lua.deprecated b/scripts/create_combine_wx_images_task.lua.deprecated deleted file mode 100644 index 59fc399..0000000 --- a/scripts/create_combine_wx_images_task.lua.deprecated +++ /dev/null @@ -1,21 +0,0 @@ -package.path = package.path .. ';/?.lua'; -local cjson = require "cjson" -local http = require "resty.http" -local create_task = require "lib.create_task" -local TaskTypes = require 'lib.task_type' -local verify = require('lib.jwt_verify') - -local authHeader = ngx.req.get_headers().Authorization -local sudoerToken = ngx.req.get_headers().HtySudoerToken -verify(httpc, authHeader, sudoerToken) - -local httpc = http.new() -ngx.req.read_body() -local req_body = cjson.decode(ngx.req.get_body_data()) -print('req_body...', ngx.req.get_body_data()) -local media_id = req_body['media_id'] - -local task_text = cjson.encode({ task_type = TaskTypes.CONVERT_AUDIO_FILE, payload = { media_id = media_id } }) -create_task(httpc, task_text, authHeader, sudoerToken) - - diff --git a/scripts/lib/commons.lua b/scripts/lib/commons.lua new file mode 100644 index 0000000..891c5f3 --- /dev/null +++ b/scripts/lib/commons.lua @@ -0,0 +1,13 @@ +local _M = {} + +function _M.file_exists(name) + local f = io.open(name, "r") + if f ~= nil then + io.close(f) + return true + else + return false + end +end + +return _M diff --git a/scripts/lib/my_uuid.lua b/scripts/lib/my_uuid.lua index 4eaaa8b..176f0b2 100644 --- a/scripts/lib/my_uuid.lua +++ b/scripts/lib/my_uuid.lua @@ -1,14 +1,23 @@ +local commons = require("lib.commons") + local _M = {} function _M.uuid() local uuid_cmd = "/usr/bin/uuid" - local handle = io.popen(uuid_cmd) - local result = handle:read("*a") - local r2 = string.gsub(result, "%s+", "") - local r3 = string.gsub(r2,"\n","") - ngx.log(ngx.INFO, "uuid -> ", r3) - return r3 + if commons.file_exists(uuid_cmd) then + local handle = io.popen(uuid_cmd) + local result = handle:read("*a") + local r2 = string.gsub(result, "%s+", "") + local r3 = string.gsub(r2,"\n","") + ngx.log(ngx.INFO, "uuid -> ", r3) + return r3 + else + ngx.status = 500 + ngx.log(ngx.ERR, "my_uuid *FAILED* `/usr/bin/uuid` not exists!") + ngx.say("my_uuid *FAILED* `/usr/bin/uuid` not exists!") + ngx.exit() + end end return _M \ No newline at end of file diff --git a/scripts/upload_audio.lua.deprecated b/scripts/upload_audio.lua.deprecated deleted file mode 100644 index 59fc399..0000000 --- a/scripts/upload_audio.lua.deprecated +++ /dev/null @@ -1,21 +0,0 @@ -package.path = package.path .. ';/?.lua'; -local cjson = require "cjson" -local http = require "resty.http" -local create_task = require "lib.create_task" -local TaskTypes = require 'lib.task_type' -local verify = require('lib.jwt_verify') - -local authHeader = ngx.req.get_headers().Authorization -local sudoerToken = ngx.req.get_headers().HtySudoerToken -verify(httpc, authHeader, sudoerToken) - -local httpc = http.new() -ngx.req.read_body() -local req_body = cjson.decode(ngx.req.get_body_data()) -print('req_body...', ngx.req.get_body_data()) -local media_id = req_body['media_id'] - -local task_text = cjson.encode({ task_type = TaskTypes.CONVERT_AUDIO_FILE, payload = { media_id = media_id } }) -create_task(httpc, task_text, authHeader, sudoerToken) - - diff --git a/scripts/wx_upload_single.lua.deprecated b/scripts/wx_upload_single.lua.deprecated deleted file mode 100644 index 4795bdf..0000000 --- a/scripts/wx_upload_single.lua.deprecated +++ /dev/null @@ -1,29 +0,0 @@ --- 指定模块引用目录,否则无法加载同目录下的其他文件 ---package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; -package.path = package.path .. ';/?.lua'; -local http = require "resty.http" -local cjson = require "cjson" -local pl = require "pl.pretty" -local uuid = require("lib.my_uuid") -local upyun_upload = require("lib.upyun_upload") -local get_wx_media = require("lib.wx_download") -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) - -local function run() - - 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 downloaded_file = get_wx_media(httpc, req_body["media_id"], cjson, pl); - - upyun_upload.upload(downloaded_file, uuid.uuid() .. ".jpg") -end - -run();