diff --git a/delete.go b/delete.go index 64b17ef..521b277 100644 --- a/delete.go +++ b/delete.go @@ -35,7 +35,7 @@ func deleteHandler(c web.C, w http.ResponseWriter, r *http.Request) { metaDelErr := os.Remove(metaPath) if (fileDelErr != nil) || (metaDelErr != nil) { - oopsHandler(c, w, r) // 500 - can't delete something + oopsHandler(c, w, r, RespPLAIN, "Could not delete") return } diff --git a/display.go b/display.go index f40bc5a..5fc26c7 100644 --- a/display.go +++ b/display.go @@ -91,6 +91,6 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { }, w) if err != nil { - oopsHandler(c, w, r) + oopsHandler(c, w, r, RespHTML, "") } } diff --git a/fileserve.go b/fileserve.go index 8097bfd..b568ce3 100644 --- a/fileserve.go +++ b/fileserve.go @@ -38,7 +38,7 @@ func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) { filePath := strings.TrimPrefix(path, "/static/") file, err := staticBox.Open(filePath) if err != nil { - oopsHandler(c, w, r) + notFoundHandler(c, w, r) return } diff --git a/pages.go b/pages.go index c20e1b8..c4676a4 100644 --- a/pages.go +++ b/pages.go @@ -1,12 +1,24 @@ package main import ( + "encoding/json" + "fmt" "net/http" + "strings" "github.com/flosch/pongo2" "github.com/zenazn/goji/web" ) +type RespType int + +const ( + RespPLAIN RespType = iota + RespJSON + RespHTML + RespAUTO +) + func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) { err := Templates["index.html"].ExecuteWriter(pongo2.Context{}, w) if err != nil { @@ -17,7 +29,7 @@ func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) { func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) { err := Templates["paste.html"].ExecuteWriter(pongo2.Context{}, w) if err != nil { - oopsHandler(c, w, r) + oopsHandler(c, w, r, RespHTML, "") } } @@ -25,15 +37,41 @@ func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) { w.WriteHeader(404) err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w) if err != nil { - oopsHandler(c, w, r) + oopsHandler(c, w, r, RespHTML, "") } } -func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request) { - w.WriteHeader(500) - err := Templates["oops.html"].ExecuteWriter(pongo2.Context{}, w) - if err != nil { - oopsHandler(c, w, r) +func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) { + if msg == "" { + msg = "Oops! Something went wrong..." + } + + if rt == RespHTML { + Templates["oops.html"].ExecuteWriter(pongo2.Context{"msg": msg}, w) + w.WriteHeader(500) + return + + } else if rt == RespPLAIN { + fmt.Fprintf(w, "%s", msg) + w.WriteHeader(500) + return + + } else if rt == RespJSON { + js, _ := json.Marshal(map[string]string{ + "error": msg, + }) + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(500) + w.Write(js) + return + + } else if rt == RespAUTO { + if strings.EqualFold("application/json", r.Header.Get("Accept")) { + oopsHandler(c, w, r, RespJSON, msg) + } else { + oopsHandler(c, w, r, RespHTML, msg) + } } } diff --git a/server_test.go b/server_test.go index 19a189a..31e690b 100644 --- a/server_test.go +++ b/server_test.go @@ -136,8 +136,8 @@ func TestPutEmptyUpload(t *testing.T) { goji.DefaultMux.ServeHTTP(w, req) - if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") { - t.Fatal("Response doesn't contain'Oops! Something went wrong.'") + if !strings.Contains(w.Body.String(), "Empty file") { + t.Fatal("Response doesn't contain'Empty file'") } } diff --git a/static/js/upload.js b/static/js/upload.js index 44afffc..6c6dc62 100644 --- a/static/js/upload.js +++ b/static/js/upload.js @@ -58,15 +58,15 @@ Dropzone.options.dropzone = { xhr.send(); }.bind(this); }, - error: function(file, errMsg, xhrO) { + error: function(file, resp, xhrO) { file.rightLeftElement.onclick = null; file.rightLeftElement.innerHTML = ""; file.rightRightElement.innerHTML = ""; if (file.status === "canceled") { - file.leftElement.innerHTML = "Canceled " + file.name; + file.leftElement.innerHTML = file.name + ": Canceled "; } else { - file.leftElement.innerHTML = "Could not upload " + file.name; + file.leftElement.innerHTML = file.name + ": " + resp.error; } file.leftElement.className = "error"; }, diff --git a/templates/oops.html b/templates/oops.html index 9627bba..41dfd8f 100644 --- a/templates/oops.html +++ b/templates/oops.html @@ -3,7 +3,7 @@ {% block content %}
-

{{ error_message|default:"Oops! Something went wrong." }}

+

{{ msg }}

{% endblock %} diff --git a/torrent.go b/torrent.go index 01176aa..1f8c942 100644 --- a/torrent.go +++ b/torrent.go @@ -87,7 +87,7 @@ func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) { encoded, err := createTorrent(fileName, filePath) if err != nil { - oopsHandler(c, w, r) // 500 - creating torrent failed + oopsHandler(c, w, r, RespHTML, "Could not create torrent.") return } diff --git a/upload.go b/upload.go index edc4742..39f62ce 100644 --- a/upload.go +++ b/upload.go @@ -46,7 +46,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(contentType, "multipart/form-data") { file, headers, err := r.FormFile("file") if err != nil { - oopsHandler(c, w, r) + oopsHandler(c, w, r, RespHTML, "Could not upload file.") return } defer file.Close() @@ -60,7 +60,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { upReq.filename = headers.Filename } else { if r.FormValue("content") == "" { - oopsHandler(c, w, r) + oopsHandler(c, w, r, RespHTML, "Could not upload file.") return } extension := r.FormValue("extension") @@ -74,16 +74,22 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { } upload, err := processUpload(upReq) - if err != nil { - oopsHandler(c, w, r) - return - } if strings.EqualFold("application/json", r.Header.Get("Accept")) { + if err != nil { + oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error()) + return + } + js := generateJSONresponse(upload) w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Write(js) } else { + if err != nil { + oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error()) + return + } + http.Redirect(w, r, "/"+upload.Filename, 301) } @@ -98,16 +104,22 @@ func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) { upReq.src = r.Body upload, err := processUpload(upReq) - if err != nil { - oopsHandler(c, w, r) - return - } if strings.EqualFold("application/json", r.Header.Get("Accept")) { + if err != nil { + oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error()) + return + } + js := generateJSONresponse(upload) w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Write(js) } else { + if err != nil { + oopsHandler(c, w, r, RespPLAIN, "Could not upload file: "+err.Error()) + return + } + fmt.Fprintf(w, Config.siteURL+upload.Filename) } } @@ -123,7 +135,7 @@ func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) { resp, err := http.Get(grabUrl.String()) if err != nil { - oopsHandler(c, w, r) + oopsHandler(c, w, r, RespAUTO, "Could not retrieve URL") return } @@ -131,16 +143,22 @@ func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) { upReq.src = resp.Body upload, err := processUpload(upReq) - if err != nil { - oopsHandler(c, w, r) - return - } if strings.EqualFold("application/json", r.Header.Get("Accept")) { + if err != nil { + oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error()) + return + } + js := generateJSONresponse(upload) w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Write(js) } else { + if err != nil { + oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error()) + return + } + http.Redirect(w, r, "/"+upload.Filename, 301) } } @@ -174,8 +192,8 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { // Pull the first 512 bytes off for use in MIME detection header = make([]byte, 512) n, err := upReq.src.Read(header) - if n == 0 { - return upload, err + if n == 0 || err != nil { + return upload, errors.New("Empty file") } header = header[:n] @@ -228,12 +246,15 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { metadataWrite(upload.Filename, &upload) bytes, err := io.Copy(dst, io.MultiReader(bytes.NewReader(header), upReq.src)) - if err != nil { - return - } else if bytes == 0 { + if bytes == 0 { os.Remove(path.Join(Config.filesDir, upload.Filename)) os.Remove(path.Join(Config.metaDir, upload.Filename)) return upload, errors.New("Empty file") + + } else if err != nil { + os.Remove(path.Join(Config.filesDir, upload.Filename)) + os.Remove(path.Join(Config.metaDir, upload.Filename)) + return } upload.Size = bytes