diff --git a/page_upload.go b/page_upload.go index b4acedde6d46a58da58145c20078c3ab98edeba2..26b881059ccc6b95c90e313f7009273eb3464d28 100644 --- a/page_upload.go +++ b/page_upload.go @@ -84,13 +84,10 @@ func pageUpload(ctx PageContext) http.Handler { err := r.ParseMultipartForm(32 << 20) if err != nil { - if err = formatTemplate(w, "upload.html", UploadData{ - user, - []Result{{ - Success: false, - Errors: []string{err.Error()}, - }}, - }); err != nil { + if err = returnJson(w, []Result{{ + Success: false, + Errors: []string{err.Error()}, + }}); err != nil { panic(err) } } @@ -103,26 +100,20 @@ func pageUpload(ctx PageContext) http.Handler { for _, header := range files { file, err := header.Open() if err != nil { - if err = formatTemplate(w, "upload.html", UploadData{ - user, - []Result{{ - Success: false, - Errors: []string{err.Error()}, - }}, - }); err != nil { + if err = returnJson(w, []Result{{ + Success: false, + Errors: []string{err.Error()}, + }}); err != nil { panic(err) } return } image, err := createImage(ctx.Config, file, header) if err != nil { - if err = formatTemplate(w, "upload.html", UploadData{ - user, - []Result{{ - Success: false, - Errors: []string{err.Error()}, - }}, - }); err != nil { + if err = returnJson(w, []Result{{ + Success: false, + Errors: []string{err.Error()}, + }}); err != nil { panic(err) } return @@ -142,13 +133,10 @@ func pageUpload(ctx PageContext) http.Handler { data, err := json.Marshal(image) if err != nil { - if err = formatTemplate(w, "upload.html", UploadData{ - user, - []Result{{ - Success: false, - Errors: []string{err.Error()}, - }}, - }); err != nil { + if err = returnJson(w, []Result{{ + Success: false, + Errors: []string{err.Error()}, + }}); err != nil { panic(err) } return @@ -165,13 +153,10 @@ func pageUpload(ctx PageContext) http.Handler { for len(waiting) != 0 { message, err := pubsub.ReceiveMessage() if err != nil { - if err = formatTemplate(w, "upload.html", UploadData{ - user, - []Result{{ - Success: false, - Errors: []string{err.Error()}, - }}, - }); err != nil { + if err = returnJson(w, []Result{{ + Success: false, + Errors: []string{err.Error()}, + }}); err != nil { panic(err) } return @@ -180,13 +165,10 @@ func pageUpload(ctx PageContext) http.Handler { result := Result{} err = json.Unmarshal([]byte(message.Payload), &result) if err != nil { - if err = formatTemplate(w, "upload.html", UploadData{ - user, - []Result{{ - Success: false, - Errors: []string{err.Error()}, - }}, - }); err != nil { + if err = returnJson(w, []Result{{ + Success: false, + Errors: []string{err.Error()}, + }}); err != nil { panic(err) } return @@ -201,10 +183,7 @@ func pageUpload(ctx PageContext) http.Handler { } } - if err = formatTemplate(w, "upload.html", UploadData{ - user, - results, - }); err != nil { + if err = returnJson(w, results); err != nil { panic(err) } return diff --git a/util.go b/util.go index cdaf5a58db0ba9dba6fd14b3d36a563e909d1281..0b0fe0d7f28bbec0254884202f12b2bb139ab938 100644 --- a/util.go +++ b/util.go @@ -8,6 +8,7 @@ import ( "database/sql" "github.com/go-redis/redis" "strings" + "encoding/json" ) type UserInfo struct { @@ -58,6 +59,20 @@ func parseUser(r *http.Request) UserInfo { } } +func returnJson(w http.ResponseWriter, data interface{}) error { + marshalled, err := json.Marshal(data) + if err != nil { + return err + } + + w.Header().Add("Content-Type", "application/json") + if _, err := w.Write(marshalled); err != nil { + return err + } + + return nil +} + func formatTemplate(w http.ResponseWriter, templateName string, data interface{}) error { pageTemplate, err := template.ParseFiles( "templates/_base.html",