upload function

pull/20/head
Henrique Dias 2015-09-26 21:22:27 +01:00
parent a717ac9fad
commit 7c29957c3b
1 changed files with 40 additions and 36 deletions

View File

@ -70,42 +70,7 @@ func post(w http.ResponseWriter, r *http.Request) (int, error) {
// If it's the upload of a file
if r.Header.Get("X-Upload") == "true" {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
w.Write([]byte(err.Error()))
return 500, err
}
// For each file header in the multipart form
for _, fheaders := range r.MultipartForm.File {
// Handle each file
for _, hdr := range fheaders {
// Open the first file
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
// Create the file
var outfile *os.File
if outfile, err = os.Create(r.URL.Path + hdr.Filename); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
// Copy the file content
if _, err = io.Copy(outfile, infile); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
}
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return 200, nil
return upload(w, r)
}
// Get the JSON information sent using a buffer
@ -185,6 +150,45 @@ func post(w http.ResponseWriter, r *http.Request) (int, error) {
return 200, nil
}
func upload(w http.ResponseWriter, r *http.Request) (int, error) {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
w.Write([]byte(err.Error()))
return 500, err
}
// For each file header in the multipart form
for _, fheaders := range r.MultipartForm.File {
// Handle each file
for _, hdr := range fheaders {
// Open the first file
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
// Create the file
var outfile *os.File
if outfile, err = os.Create(r.URL.Path + hdr.Filename); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
// Copy the file content
if _, err = io.Copy(outfile, infile); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
}
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return 200, nil
}
func get(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
functions := template.FuncMap{
"CanBeEdited": utils.CanBeEdited,