|
@ -1,6 +1,7 @@ |
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"encoding/json" |
|
|
"errors" |
|
|
"errors" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"io" |
|
|
"io" |
|
@ -30,15 +31,21 @@ type Upload struct { |
|
|
func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|
func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|
upReq := UploadRequest{} |
|
|
upReq := UploadRequest{} |
|
|
|
|
|
|
|
|
|
|
|
if r.Header.Get("Content-Type") == "application/octet-stream" { |
|
|
|
|
|
defer r.Body.Close() |
|
|
|
|
|
upReq.src = r.Body |
|
|
|
|
|
upReq.filename = r.URL.Query().Get("qqfile") |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
file, headers, err := r.FormFile("file") |
|
|
file, headers, err := r.FormFile("file") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Fprintf(w, err.Error()) |
|
|
|
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
defer file.Close() |
|
|
defer file.Close() |
|
|
|
|
|
|
|
|
upReq.src = file |
|
|
upReq.src = file |
|
|
upReq.filename = headers.Filename |
|
|
upReq.filename = headers.Filename |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
upload, err := processUpload(upReq) |
|
|
upload, err := processUpload(upReq) |
|
|
if err != nil { |
|
|
if err != nil { |
|
@ -46,7 +53,19 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) { |
|
|
|
|
|
js, _ := json.Marshal(map[string]string{ |
|
|
|
|
|
"filename": upload.Filename, |
|
|
|
|
|
"url": Config.siteURL + upload.Filename, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8") |
|
|
|
|
|
w.Write(js) |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
http.Redirect(w, r, "/"+upload.Filename, 301) |
|
|
http.Redirect(w, r, "/"+upload.Filename, 301) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|
func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|