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.
顺序 选择 循环 总结
openresty lua 文件上传与删除的更多相关文章
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- SpringMVC ajax技术无刷新文件上传下载删除示例
参考 Spring MVC中上传文件实例 SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传 Spring MVC 文件上传下载 (FileOperateUtil.ja ...
- 又拍云 Node.js 实现文件上传、删除
Node.js 服务端 使用 Node.js + Express.js 实现 服务端 const express = require("express"); const app = ...
- java操作FTP,实现文件上传下载删除操作
上传文件到FTP服务器: /** * Description: 向FTP服务器上传文件 * @param url FTP服务器hostname * @param port FTP服务器端口,如果默认端 ...
- HDFS操作--文件上传/创建/删除/查询文件信息
1.上传本地文件到HDFS //上传本地文件到HDFS public class CopyFile { public static void main(String[] args) { try { C ...
- js实现文件上传,删除效果
效果图: 刚开始: 点击按钮"选择更多后",可以添加很多选择文件: 点击按钮"删除"后: 实现代码: <!DOCTYPE html><html ...
- [java]文件上传下载删除与图片预览
图片预览 @GetMapping("/image") @ResponseBody public Result image(@RequestParam("imageName ...
- 【C#公共帮助类】FTPClientHelper帮助类,实现文件上传,目录操作,下载等动作
关于本文档的说明 本文档使用Socket通信方式来实现ftp文件的上传下载等命令的执行 欢迎传播分享,必须保持原作者的信息,但禁止将该文档直接用于商业盈利. 本人自从几年前走上编程之路,一直致力于收集 ...
- C# 文件上传下载功能实现 文件管理引擎开发
Prepare 本文将使用一个NuGet公开的组件技术来实现一个服务器端的文件管理引擎,提供了一些简单的API,来方便的实现文件引擎来对您自己的软件系统的文件进行管理. 在Visual Studio ...
随机推荐
- 基于【CentOS-7+ Ambari 2.7.0 + HDP 3.0】搭建HAWQ数据仓库01 —— 准备环境,搭建本地仓库,安装ambari
一.集群软硬件环境准备: 操作系统: centos 7 x86_64.1804 Ambari版本:2.7.0 HDP版本:3.0.0 HAWQ版本:2.3.05台PC作为工作站: ep-bd01 e ...
- Docker-服务(4)
服务定义 在分布式应用程序中,应用程序的不同部分称为“服务”.例如,如果您想象一个视频共享站点,它可能包括一个用于在数据库中存储应用程序数据的服务,一个用户在上传内容后在后台进行视频转码的服务,一个用 ...
- 初学UML之-------用例图
本文转载至:http://blog.csdn.net/a649518776/article/details/7493148 一.UML简介 UML(统一建模语言,Unified Modeling L ...
- Android Studio 3依赖配置
新配置 对应的过时配置 描述 implementation compile module编译时可用,module的使用者运行时可用,对于大量使用library的项目,可以显著提高编译时间,因为它可以减 ...
- C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子
C#WinForm父级窗体内Panel容器中嵌入子窗体.程序主窗体设计例子 在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点 主窗体窗体采用 ...
- 三种不同类型的ssh隧道
何谓SSH隧道 隧道是一种把一种网络协议封装进另外一种网络协议进行传输的技术.这里我们研究ssh隧道,所以所有的网络通讯都是加密的.又被称作端口转发,因为ssh隧道通常会绑定一个本地端口,所有发向这个 ...
- geopandas overlay 函数报错问题解决方案
前言 这篇文章依旧是基于上一篇文章(使用Python实现子区域数据分类统计)而写,此文章中介绍了使用 geopandas 的 overlay 函数对两个 GeoDataFrame 对象取相交或相异的部 ...
- 1.13flask完结
2019-1-13 14:16:26 终于完结flask,开始爬虫啦!!!! 还有 一些爬虫视频没看完,余下的就一点啦! 老师整理了flask的总结!和一些组件的使用! 打算重装一下电脑,边看视频,边 ...
- windows 安装mongodb
1.mongodb官网下载:http://www.mongodb.org/downloads 2.将下载的mongodb压缩包解压到对应文件夹,我的是:D:\pc\mongodb,请注意,我的bin目 ...
- Oracle课程档案,第九天
lsnrctl status:查看监听状态 Oracle网络配置三部分组成:客户端,监听,数据库 配置文件:$ vi $ORACLE_HOME/network/admin/listener.ora v ...