码迷,mamicode.com
首页 > 数据库 > 详细

resty.upload 处理上传的图片 并把生成的url保存到数据库中

时间:2017-04-12 11:41:28      阅读:603      评论:0      收藏:0      [点我收藏+]

标签:conf   insert   127.0.0.1   end   ror   timeout   oca   upload   否则   

--相关信息修改上传头像
local upload = require "resty.upload"
local cjson = require "cjson.safe"
local new_mysql = require("new_mysql")
--local uuid = require "resty.uuid"

local chunk_size = 4096 --如果不设置默认是4096
local form = upload:new(chunk_size)
if not form then
ngx.log(ngx.ERR, "failed to new upload: ", err)
ngx.exit(500)
end
form:set_timeout(1000) --1 sec

local file

--获取文件名
function get_filename( res )
local filename = ngx.re.match(res, ‘(.+)filename="(.+)"(.*)‘)
if filename then
return filename[2]
end
end

--获取文件扩展名
function getExtension( str )
return str:match(".+%.(%w+)$")
end

--上传成功的头像url存入数据库
local function _insertDataBaseWithImageUrl( url , userid)
local mysql = new_mysql:new({})
local sqlstr = string.format("UPDATE `BasketballDatabase`.`User` SET `head_img`=‘%s‘ WHERE `userid`=‘%s‘",url, userid)
local res, err = mysql:query(sqlstr)
if res then
return 0
else
ngx.log(ngx.ERR, "query error:", err)
return -1
end
end


local var = ngx.var
local result = {}
result.result = 1

--/usr/local/openresty/nginx/html/文件夹必须是nobody权限,否则不能写文件,报错。
--chown nobody:nobody /usr/local/openresty/nginx/html
-- local osfilepath = "/Users/xinshaofeng/Work/BasketballServer/upload/"
local i = 0
local filename = nil
while true do
local typ, res, err = form:read()
ngx.say("read: ", cjson.encode({typ, res}))
if not typ then
result.msg = "failed to read:"..err
break
end
if typ == "header" then
if res[1] ~= "Content-Type" then
local osfilepath = var.upload_head_path --用的conf里面upload_head_path
--local file_id = uuid.new()
filename = get_filename(res[2])
--local extention = getExtension(filename)
--local filepath = osfilepath..file_id.."."..extention
if filename then
i=i+1
local filepath = osfilepath .. "/user/" .. filename .. ".png"
file = io.open(filepath,"w+")
if not file then
result.msg = "failed to open file "
break
end
end
end
elseif typ == "body" then
if file then
file:write(res)
end
elseif typ == "part_end" then
if file then
file:close()
file = nil

local image_url = var.request_image_url.."/user/"..filename .. ".png"
--插入数据库
local ok = _insertDataBaseWithImageUrl(image_url, filename)
if ok == 0 then
result.result = 0
result.msg = "file upload success"
result.url = image_url
else
result.msg = "insert database failed"
end
end
elseif typ == "eof" then
break
else
-- do nothing
end
end

if i==0 then
result.msg = "please upload at least ont file!"
end

-- ngx.say(cjson.encode(result))
ngx.exit(ngx.HTTP_OK)

 

//curl 模拟上传图片

curl -F file=/Users/xinshaofeng/Work/BasketballServer/upload/headImg.png -F userid=1 http://127.0.0.1:8088/user_headupload

//命令行的输出

read: ["header",["Content-Disposition","form-data; name=\"file\"","Content-Disposition: form-data; name=\"file\""]]

read: ["body","\/Users\/xinshaofeng\/Work\/BasketballServer\/upload\/headImg.png"]

read: ["part_end"]

read: ["header",["Content-Disposition","form-data; name=\"userid\"","Content-Disposition: form-data; name=\"userid\""]]

read: ["body","1"]

read: ["part_end"]

read: ["eof"]

resty.upload 处理上传的图片 并把生成的url保存到数据库中

标签:conf   insert   127.0.0.1   end   ror   timeout   oca   upload   否则   

原文地址:http://www.cnblogs.com/xilanglang/p/6697923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!