resty.upload 處理上傳的圖片 並把生成的url保存到數據庫中


--相關信息修改上傳頭像
local upload = require "resty.upload"
local cjson = require "cjson.safe"
local new_mysql = require("new_mysql")
local uuid = require "jit-uuid"
uuid.seed()

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

--["header",["Content-Disposition","form-data; name=\"headImg\"; filename=\"1\"","Content-Disposition: form-data; name=\"headImg\"; filename=\"1\""]]
--獲取文件名
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

--["header",["Content-Disposition","form-data; name=\"userid\"","Content-Disposition: form-data; name=\"userid\""]]
--獲取userid
function get_userid( res )
local filename = ngx.re.match(res, '(.+)name="(.+)"(.*)')
if filename then
if filename[2] == "userid" then
return "userid"
end
end
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 file = nil
local useridkey = nil
local useridvalue = nil
local image_name = 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
-- ngx.say("read: ", cjson.encode({typ, res}))
if res[1] ~= "Content-Type" then
useridkey = get_userid(res[2])

local filename = get_filename(res[2])
--local extention = getExtension(filename)
--local filepath = osfilepath..file_id.."."..extention
if filename then
local osfilepath = var.upload_head_path --用的conf里面upload_head_path
image_name = uuid()
i=i+1
local filepath = osfilepath .. "/user/" .. image_name .. ".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 useridkey then
useridvalue = res
end

if file then
file:write(res)
end
elseif typ == "part_end" then
ngx.say("read:userid ",userid)
if useridkey then
useridkey = nil
end

if file then
file:close()
file = nil

local image_url = var.request_image_url.."/user/"..image_name .. ".png"

--插入數據庫
local ok = _insertDataBaseWithImageUrl(image_url, useridvalue)
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"]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM