--related information modify upload avatar
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--If not set default is 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 (--1 sec)
Local file
--Get file name
function Get_filename (res)
Local filename = Ngx.re.match (res, ' (. +) Filename= "(. +)" (. *) ')
If filename Then
return filename[2]
End
End
--Get file name extension
function GetExtension (str)
Return Str:match (". +%." ( %w+) $ ")
End
--Upload the successful avatar URL into the database
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/folder must be nobody permissions, otherwise you can not write files, error.
--chown nobody:nobody/usr/local/openresty/nginx/html
--Local OsFilePath = "/users/xinshaofeng/work/basketballserver/upload/"
Local i = 0
Local filename = Nil
While True does
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 inside 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"
--Insert Database
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 = "Upload at least ont file!"
End
--Ngx.say (Cjson.encode (Result))
Ngx.exit (NGX. HTTP_OK)
Curl simulates uploading pictures
Curl-f file=/users/xinshaofeng/work/basketballserver/upload/headimg.png-f userid=1 Http://127.0.0.1:8088/user_headupload
Command-line output
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 process the uploaded image and save the generated URL to the database