diff --git a/.gitignore b/.gitignore index 128c89e..996be6f 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,7 @@ luac.out .DS_Store # IDEA dir -.idea/ \ No newline at end of file +.idea/ + +# Test medie files +*.mp3 \ No newline at end of file diff --git a/conf/huiwings/admin.conf b/conf/huiwings/admin.conf index 22e55d7..fd3520e 100644 --- a/conf/huiwings/admin.conf +++ b/conf/huiwings/admin.conf @@ -55,6 +55,11 @@ server { location /api/ngx/image/upload_combined { content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua; } + + location /api/ngx/image/upyun_remove { + content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua; + } + #Upload image files location /api/ngx/image/upload { content_by_lua_file $resty_loc/nginx/scripts/old_upload.lua; diff --git a/conf/local_linux/admin.conf b/conf/local_linux/admin.conf index 3b5208d..749c89d 100644 --- a/conf/local_linux/admin.conf +++ b/conf/local_linux/admin.conf @@ -50,6 +50,10 @@ server { content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua; } + location /api/ngx/image/upyun_remove { + content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua; + } + #Upload image files location /api/ngx/image/upload { content_by_lua_file $resty_loc/nginx/scripts/old_upload.lua; diff --git a/conf/local_macos/admin.conf b/conf/local_macos/admin.conf index 6db986e..4334611 100644 --- a/conf/local_macos/admin.conf +++ b/conf/local_macos/admin.conf @@ -7,8 +7,8 @@ server { set $tmp_file_dir "/usr/local/file_upload"; # 文件存储路径 set $task_server "http://127.0.0.1:8080"; # task server host - set $htyuc "http://127.0.0.1:3000"; #htyuc host -# set $htyuc "https://admin.alchemy-studio.cn"; # Verified by admin.alchemy-studio.cn +# set $htyuc "http://127.0.0.1:3000"; #htyuc host + set $htyuc "https://admin.moicen.com"; #Verify jwt token set $resty_loc "/usr/local/opt/openresty"; set $convert "/usr/local/bin/convert"; set $upyun_operator "moicen"; @@ -49,6 +49,11 @@ server { location /api/ngx/image/upload_combined { content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua; } + + location /api/ngx/image/upyun_remove { + content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua; + } + #Upload image files location /api/ngx/image/upload { content_by_lua_file $resty_loc/nginx/scripts/old_upload.lua; diff --git a/conf/local_macos/nginx.conf b/conf/local_macos/nginx.conf index 0f83ad2..bb05f51 100644 --- a/conf/local_macos/nginx.conf +++ b/conf/local_macos/nginx.conf @@ -2,9 +2,9 @@ #user nobody; worker_processes 1; -error_log /usr/local/etc/openresty/logs/error.log debug; +# error_log /usr/local/etc/openresty/logs/error.log debug; #error_log logs/error.log notice; -#error_log logs/error.log info; +error_log logs/error.log info; #pid logs/nginx.pid; diff --git a/conf/moicen/admin.conf b/conf/moicen/admin.conf index 29a05a5..96ad9a0 100644 --- a/conf/moicen/admin.conf +++ b/conf/moicen/admin.conf @@ -103,6 +103,10 @@ server { content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua; } + location /api/ngx/image/upyun_remove { + content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua; + } + #Audio file download location /api/ngx/audio/upload { content_by_lua_file $resty_loc/nginx/scripts/upload_audio.lua; diff --git a/scripts/lib/upyun_remove.lua b/scripts/lib/upyun_remove.lua new file mode 100644 index 0000000..82788bc --- /dev/null +++ b/scripts/lib/upyun_remove.lua @@ -0,0 +1,94 @@ +-- 指定模块引用目录,否则无法加载同目录下的其他文件 +--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; +package.path = package.path .. ';/?.lua'; + +local upyun_remove = {} + +local json = require("cjson") + +local strip_path = require("lib.strip_path") +local Upyun = require('lib.upyun') + +function upyun_remove.remove(filepath, filename, retry) + + if retry == nil then + retry = 1 + end + + if retry > 3 then + ngx.log(ngx.ERR, "failed to remove file : reach max retries") + else + + -- 使用 os.tmpname 获取临时文件名 + local tempFileName = "upyun_remove_temp_file" + -- 拼接路径 + local tempFilePath = "/tmp/" .. tempFileName + -- 使用 io.open 创建临时文件 + local tempFile = io.open(tempFilePath, "w") + -- 写入内容到临时文件 + tempFile:write("upyun new config local temp file") + -- 关闭文件 + tempFile:close() + -- 使用 os.execute 执行 chmod 命令 + os.execute("chmod 777 " .. tempFilePath) + --print("Upyun new config local temp file path : ", tempFilePath) + + + local upyun, err = Upyun:new({ + user = ngx.var.upyun_operator, + passwd = ngx.var.upyun_password, + --localFilePath = filepath -- TODO : Add a fake file for create new upyun entry + --localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir" + localFilePath = tempFilePath + }) + + if not upyun then + ngx.status = 500 + ngx.log(ngx.ERR, "failed to initialize upyun: " .. err) + return + end + + local bucket = ngx.var.upyun_bucket + local directory = ngx.var.upyun_directory + if not filename then + filename = strip_path.strip_path(filepath) + end + + ngx.log(ngx.INFO, 'bucket -> ', bucket) + ngx.log(ngx.INFO, 'directory -> ', directory) + + local removePath = bucket .. "/" .. directory .. "/" .. filename + ngx.log(ngx.INFO, " removePath " , removePath) + + --local options = { + -- md5 = true + --} + + ngx.log(ngx.INFO, ' ========>>>>> ', removePath) + + local info, err = upyun:remove_file(removePath) + if not info then + local error_table = json.decode(err) + ngx.log(ngx.INFO, "Upyun Remove File Error: " .. err) + ngx.log(ngx.ERR, '[' .. error_table["code"] .. ']') + + if (error_table["code"] == 40000006) + then + ngx.status = 400 + ngx.log(ngx.ERR, "Retry remove file : " .. '[' .. err .. ']') + upyun_remove.remove(filepath, filename, retry+1) + else + ngx.status = 400 + ngx.log(ngx.ERR, "failed to remove file : " .. '[' .. err .. ']') + end + else + ngx.status = 200 + local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename + ngx.log(ngx.INFO, "SUCCESS REMOVE -> UPYUN URL -> ", fullpath) + ngx.say(fullpath) + end + end + +end + +return upyun_remove \ No newline at end of file diff --git a/scripts/test_upyun_upload.lua b/scripts/test_upyun_upload.lua index e4fa166..c88e5cc 100644 --- a/scripts/test_upyun_upload.lua +++ b/scripts/test_upyun_upload.lua @@ -1,6 +1,6 @@ -- 指定模块引用目录,否则无法加载同目录下的其他文件 ---package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; -package.path = package.path .. ';/?.lua'; +package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; +--package.path = package.path .. ';/?.lua'; local yun = require("lib.upyun") local cjson = require "cjson" @@ -8,19 +8,23 @@ local cjson = require "cjson" local function upyun_upload_file() local config = { - user = ngx.var.upyun_operator, - passwd = ngx.var.upyun_password, - localFilePath = "/file_upload/abc.mp3" + --user = ngx.var.upyun_operator, + --passwd = ngx.var.upyun_password, + user = "moicen", + passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", + --localFilePath = "/file_upload/abc.mp3" + localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir/chunxiao.mp3" } local upyun = yun:new(config) - local bucket = ngx.var.upyun_bucket + --local bucket = ngx.var.upyun_bucket + local bucket = "moicen" local directory = 'test' - local savePath = bucket .. "/" .. directory .. "/abc.mp3" + local savePath = bucket .. "/" .. directory .. "/chunxiao.mp3" local info, err = upyun:upload_file(savePath, nil, nil) if not info then - ngx.say("failed to upload image file : " .. err) + ngx.say("failed to upload file : " .. err) return else ngx.say(cjson.encode(info)) @@ -28,4 +32,49 @@ local function upyun_upload_file() end -upyun_upload_file() \ No newline at end of file +local function upyun_remove_file() + + -- 使用 os.tmpname 获取临时文件名 + local tempFileName = "upyun_remove_temp_file" + -- 拼接路径 + local tempFilePath = "/tmp/" .. tempFileName + -- 使用 io.open 创建临时文件 + local tempFile = io.open(tempFilePath, "w") + -- 写入内容到临时文件 + tempFile:write("upyun new config local temp file") + -- 关闭文件 + tempFile:close() + -- 使用 os.execute 执行 chmod 命令 + os.execute("chmod 777 " .. tempFilePath) + --print("Upyun new config local temp file path : ", tempFilePath) + + local config = { + --user = ngx.var.upyun_operator, + --passwd = ngx.var.upyun_password, + user = "moicen", + passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", + --localFilePath = "/file_upload/abc.mp3" + --localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir/r2d2.jpeg" + localFilePath = tempFilePath + --localFilePath = nil + } + + local upyun = yun:new(config) + --local bucket = ngx.var.upyun_bucket + local bucket = "moicen" + local directory = 'test' + local removePath = bucket .. "/" .. directory .. "/chunxiao.mp3" + + local info, err = upyun:remove_file(removePath) + if not info then + ngx.say("failed to remove file : " .. err) + return + else + ngx.say(cjson.encode(info)) + end + +end + +upyun_upload_file() + +--upyun_remove_file() \ No newline at end of file diff --git a/scripts/upyun_remove.lua b/scripts/upyun_remove.lua new file mode 100644 index 0000000..dc0088f --- /dev/null +++ b/scripts/upyun_remove.lua @@ -0,0 +1,33 @@ +package.path = package.path .. ';/?.lua'; + +local http = require "resty.http" +local verify = require('lib.jwt_verify') +local cjson = require "cjson" + +local httpc = http:new() +local authHeader = ngx.req.get_headers().Authorization +local sudoerToken = ngx.req.get_headers().HtySudoerToken + +ngx.req.read_body() +local request_body = ngx.req.get_body_data() +local request_body_json = cjson.decode(request_body) +local upyun_remove_url = request_body_json["request_url"] +ngx.log(ngx.INFO, 'upyun_remove_url -> ', upyun_remove_url) + +verify(httpc, authHeader, sudoerToken) + +ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> start') +local upyun_upload = require("lib.upyun_remove") +ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> upyun_remove loaded') + +local file_to_remove = string.match(upyun_remove_url, ".*/(.*)") +ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> file to remove -> ', file_to_remove) + +--local file_dir = ngx.var.tmp_file_dir +--local fullpath = file_dir .. "/" .. file_to_remove +-- +--ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> fullpath -> ', fullpath) +-- +--upyun_upload.remove(fullpath, nil, 0) + +upyun_upload.remove(file_to_remove, file_to_remove, 0) \ No newline at end of file