【1】openresty 上传upload源码库
Github:https://github.com/openresty/lua-resty-upload
源码文件upload.lua文件
【2】上传
代码如下,详见注释:
1 local upload = require "resty.upload" 2 local cjson = require "cjson" 3 4 -- test.sh 5 -- curl -F "filename=@/home/test/test.wav" "http://127.0.0.1/uploadfile.gss?filename=test.wav&&type=wav&&billingcode=87654321" 6 7 local response = {["code"] = 200, ["msg"] = "upload success!"} 8 9 local args = ngx.req.get_uri_args() 10 if not args then 11 ngx.exit(ngx.HTTP_BAD_REQUEST) 12 end 13 14 local filename = args["filename"] or "noname.file" 15 local billingcode = args["billingcode"] 16 local filetype = args["type"] 17 18 -- 判断文件类型 19 local res, err = ngx.re.match(filename, [[\.(?:xml|wav|ext)$]]) 20 if not res then 21 response.code = 401 22 response.msg = "only xml or wav or ext format file can upload" 23 ngx.say(cjson.encode(response)) 24 return 25 end 26 27 if err then 28 ngx.log(ngx.ERR, "match err:", err) 29 ngx.exit(ngx.HTTP_BAD_REQUEST) 30 end 31 32 -- 保存文件根目录 33 local save_file_root = "/home/kaizenly/" 34 35 local save_file_dir = save_file_root 36 37 -- 创建各类文件的保存目录[wav/ext/app] 38 if "wav" == filetype or "ext" == filetype then 39 save_file_dir = save_file_dir .. filetype 40 elseif "flow" == filetype or "state" == filetype then 41 save_file_dir = save_file_dir .. "app" 42 else 43 response.code = 402 44 response.msg = "error file type! filetype: " .. filetype 45 ngx.say(cjson.encode(response)) 46 return 47 end 48 49 local dfile = io.open(save_file_dir, "rb") 50 if dfile then 51 dfile:close() 52 else 53 local md = "mkdir " 54 local mdpath = md .. save_file_dir 55 os.execute(mdpath) 56 ngx.log(ngx.ERR, "create save file first dir: " .. mdpath) 57 end 58 59 save_file_dir = save_file_dir .. "/" 60 61 -- 创建各类文件的保存子目录[按billingcode存储] 62 local save_file_path = '' 63 64 if "wav" == filetype or "ext" == filetype then 65 save_file_path = save_file_dir .. billingcode 66 else 67 save_file_path = save_file_dir .. filetype .. "xml" 68 end 69 70 local dfile = io.open(save_file_path, "rb") 71 if dfile then 72 dfile:close() 73 else 74 local md = "mkdir " 75 local mdpath = md .. save_file_path 76 os.execute(mdpath) 77 ngx.log(ngx.ERR, "create save file second dir: " .. mdpath) 78 end 79 80 save_file_path = save_file_path .. "/" 81 82 -- 创建上传form 83 local chunk_size = 8192 -- should be set to 4096 or 8192 84 85 local form, err = upload:new(chunk_size) 86 if not form then 87 ngx.log(ngx.ERR, "failed to new upload: ", err) 88 ngx.exit(500) 89 end 90 91 form:set_timeout(1000) -- 1 sec 92 93 local function close_file(write_file) 94 if io.type(write_file) == "file" then -- write_file处于打开状态,则关闭文件。 95 write_file:close() 96 write_file = nil 97 end 98 end 99 100 -- 上传过程 101 local write_file -- 文件句柄 102 while true do 103 local typ, recv, err = form:read() 104 if not typ then 105 response.code = 403 106 -- ngx.say("failed to read file: ", err) 107 response.msg = "failed to read file" 108 break 109 end 110 111 if typ == "header" and "file" ~= io.type(write_file) then 112 write_file, err = io.open(save_file_path .. filename, 'wb+') 113 if err then 114 ngx.log(ngx.ERR, "failed create hd:", err) 115 response.code = 404 116 response.msg = "failed create file:" .. err 117 break 118 end 119 elseif typ == "body" and "file" == io.type(write_file) then 120 write_file:write(recv) 121 elseif typ == "part_end" then 122 close_file(write_file) 123 elseif typ == "eof" then 124 response.code = 200 125 response.msg = "upload success" 126 break 127 end 128 end 129 130 ngx.say(cjson.encode(response))
如上代码。
【3】删除
代码如下,详见注释:
1 local upload = require "resty.upload" 2 local cjson = require "cjson" 3 4 local args = ngx.req.get_uri_args() 5 if not args then 6 ngx.exit(ngx.HTTP_BAD_REQUEST) 7 end 8 9 local filename = args["filename"] or "noname.file" 10 local billingcode = args["billingcode"] or "" 11 local filetype = args["type"] 12 13 local response = {["code"] = 200, ["msg"] = "remove success!"} 14 15 -- 判断文件类型 16 local res, err = ngx.re.match(filename, [[\.(?:xml|wav|ext)$]]) 17 if not res then 18 response.code = 401 19 response.msg = "only xml or wav or ext format file can remove" 20 ngx.say(cjson.encode(response)) 21 return 22 end 23 24 if err then 25 ngx.log(ngx.ERR, "match err:", err) 26 ngx.exit(ngx.HTTP_BAD_REQUEST) 27 end 28 29 -- 保存文件根目录 30 local save_file_root = "/home/kaizenly/" 31 32 -- 确定删除文件路径 33 local remove_file_path = '' 34 if "wav" == filetype or "ext" == filetype then 35 remove_file_path = save_file_root .. filetype 36 if "" ~= billingcode then 37 remove_file_path = remove_file_path .. "/" .. billingcode 38 end 39 elseif "flow" == filetype or "state" == filetype then 40 remove_file_path = save_file_root .. "app/" .. filetype .. "xml" 41 else 42 response.code = 402 43 response.msg = "failed to remove, error file type!" 44 ngx.say(cjson.encode(response)) 45 return 46 end 47 48 remove_file = remove_file_path .. "/" .. filename 49 50 -- 判断删除文件是否存在 51 local dfile = io.open(remove_file, "rb") 52 if dfile then 53 dfile:close() 54 else 55 response.code = 403 56 response.msg = "the remove file is not exist!" 57 ngx.say(cjson.encode(response)) 58 return 59 end 60 61 -- 执行删除 62 local res, err = os.remove(remove_file) 63 if not res then 64 response.code = 404 65 response.msg = "failed to remove " .. remove_file .. ", err: " .. (err or '') 66 else 67 ngx.log(ngx.ERR, "success to remove file: " .. remove_file) 68 -- 如果目录为空,删除目录 69 local cmd = "ls " .. remove_file_path 70 local s = io.popen(cmd) 71 local file_lists = s:read("*all") 72 if #file_lists == 0 then 73 ngx.log(ngx.ERR, "success to remove file second dir: " .. remove_file_path) 74 os.remove(remove_file_path) 75 end 76 end 77 78 ngx.say(cjson.encode(response))
如上代码
Good Good Study, Day Day Up.
顺序 选择 循环 总结