openresty lua 文件上传与删除

时间:2023-03-10 03:34:40
openresty lua 文件上传与删除

【1】openresty 上传upload源码库

Github:https://github.com/openresty/lua-resty-upload

源码文件upload.lua文件

【2】上传

代码如下,详见注释:

 local upload = require "resty.upload"
 local cjson = require "cjson"

 -- test.sh
 -- curl -F "filename=@/home/test/test.wav" "http://127.0.0.1/uploadfile.gss?filename=test.wav&&type=wav&&billingcode=87654321"

 , ["msg"] = "upload success!"} 

 local args = ngx.req.get_uri_args()
 if not args then
     ngx.exit(ngx.HTTP_BAD_REQUEST)
 end

 local filename = args["filename"] or "noname.file"
 local billingcode = args["billingcode"]
 local filetype = args["type"]

 -- 判断文件类型
 local res, err = ngx.re.match(filename, [[\.(?:xml|wav|ext)$]])
 if not res then
     response.code =
     response.msg = "only xml or wav or ext format file can upload"
     ngx.say(cjson.encode(response))
     return
 end

 if err then
     ngx.log(ngx.ERR, "match err:", err)
     ngx.exit(ngx.HTTP_BAD_REQUEST)
 end

 -- 保存文件根目录
 local save_file_root = "/home/kaizenly/"

 local save_file_dir = save_file_root

 -- 创建各类文件的保存目录[wav/ext/app]
 if "wav" == filetype or "ext" == filetype then
     save_file_dir = save_file_dir .. filetype
 elseif "flow" == filetype or "state" == filetype then
     save_file_dir = save_file_dir .. "app"
 else
     response.code =
     response.msg = "error file type! filetype: " .. filetype
     ngx.say(cjson.encode(response))
     return
 end

 local dfile = io.open(save_file_dir, "rb")
 if dfile then
     dfile:close()
 else
     local md = "mkdir "
     local mdpath = md .. save_file_dir
     os.execute(mdpath)
     ngx.log(ngx.ERR, "create save file first dir: " .. mdpath)
 end

 save_file_dir = save_file_dir .. "/"

 -- 创建各类文件的保存子目录[按billingcode存储]
 local save_file_path = ''

 if "wav" == filetype or "ext" == filetype then
     save_file_path = save_file_dir .. billingcode
 else
     save_file_path = save_file_dir .. filetype .. "xml"
 end

 local dfile = io.open(save_file_path, "rb")
 if dfile then
     dfile:close()
 else
     local md = "mkdir "
     local mdpath = md .. save_file_path
     os.execute(mdpath)
     ngx.log(ngx.ERR, "create save file second dir: " .. mdpath)
 end

 save_file_path = save_file_path .. "/"

 -- 创建上传form
  -- should be set to 4096 or 8192

 local form, err = upload:new(chunk_size)
 if not form then
     ngx.log(ngx.ERR, "failed to new upload: ", err)
     ngx.exit()
 end

 form:set_timeout() -- 1 sec

 local function close_file(write_file)
     if io.type(write_file) == "file" then  -- write_file处于打开状态,则关闭文件。
         write_file:close()
         write_file = nil
     end
 end

 -- 上传过程
 local write_file -- 文件句柄
 while true do
     local typ, recv, err = form:read()
     if not typ then
         response.code =
         -- ngx.say("failed to read file: ", err)
         response.msg = "failed to read file"
         break
     end

     if typ == "header" and "file" ~= io.type(write_file) then
         write_file, err = io.open(save_file_path .. filename, 'wb+')
         if err then
             ngx.log(ngx.ERR, "failed create hd:", err)
             response.code =
             response.msg = "failed create file:" .. err
             break
         end
     elseif typ == "body" and "file" == io.type(write_file) then
         write_file:write(recv)
     elseif typ == "part_end" then
         close_file(write_file)
     elseif typ == "eof" then
         response.code =
         response.msg = "upload success"
         break
     end
 end

 ngx.say(cjson.encode(response))

如上代码。

【3】删除

代码如下,详见注释:

 local upload = require "resty.upload"
 local cjson = require "cjson"

 local args = ngx.req.get_uri_args()
 if not args then
     ngx.exit(ngx.HTTP_BAD_REQUEST)
 end

 local filename = args["filename"] or "noname.file"
 local billingcode = args["billingcode"] or ""
 local filetype = args["type"]

 , ["msg"] = "remove success!"} 

 -- 判断文件类型
 local res, err = ngx.re.match(filename, [[\.(?:xml|wav|ext)$]])
 if not res then
     response.code =
     response.msg = "only xml or wav or ext format file can remove"
     ngx.say(cjson.encode(response))
     return
 end

 if err then
     ngx.log(ngx.ERR, "match err:", err)
     ngx.exit(ngx.HTTP_BAD_REQUEST)
 end

 -- 保存文件根目录
 local save_file_root = "/home/kaizenly/"

 -- 确定删除文件路径
 local remove_file_path = ''
 if "wav" == filetype or "ext" == filetype then
     remove_file_path = save_file_root .. filetype
     if "" ~= billingcode then
         remove_file_path = remove_file_path .. "/" .. billingcode
     end
 elseif "flow" == filetype or "state" == filetype then
     remove_file_path = save_file_root .. "app/" .. filetype .. "xml"
 else
     response.code =
     response.msg = "failed to remove, error file type!"
     ngx.say(cjson.encode(response))
     return
 end

 remove_file  = remove_file_path .. "/" .. filename

 -- 判断删除文件是否存在
 local dfile = io.open(remove_file, "rb")
 if dfile then
     dfile:close()
 else
     response.code =
     response.msg = "the remove file is not exist!"
     ngx.say(cjson.encode(response))
     return
 end

 -- 执行删除
 local res, err = os.remove(remove_file)
 if not res then
     response.code =
     response.msg = "failed to remove " .. remove_file .. ", err: " .. (err or '')
 else
    ngx.log(ngx.ERR, "success to remove file: " .. remove_file)
    -- 如果目录为空,删除目录
    local cmd = "ls " .. remove_file_path
    local s = io.popen(cmd)
    local file_lists = s:read("*all")
     then
       ngx.log(ngx.ERR, "success to remove file second dir: " .. remove_file_path)
       os.remove(remove_file_path)
    end
 end

 ngx.say(cjson.encode(response))

如上代码

Good Good Study, Day Day Up.

顺序 选择 循环 总结