diff --git a/assets/templates/html/deletePasteSuccess.html.tmpl b/assets/templates/html/deletePasteSuccess.html.tmpl deleted file mode 100644 index 334f2cb..0000000 --- a/assets/templates/html/deletePasteSuccess.html.tmpl +++ /dev/null @@ -1,3 +0,0 @@ -{{define "title"}} -Success - rushlink -{{end}} \ No newline at end of file diff --git a/assets/templates/html/newFileUploadPasteSuccess.html.tmpl b/assets/templates/html/newFileUploadPasteSuccess.html.tmpl deleted file mode 100644 index d5f8f3b..0000000 --- a/assets/templates/html/newFileUploadPasteSuccess.html.tmpl +++ /dev/null @@ -1,16 +0,0 @@ -{{define "title"}} -Success - rushlink -{{end}} - -{{define "body"}} -
-{{.Host}}/{{.Paste.Key}}{{.FileExt}} ---- - - # View metadata - curl {{.Host}}/{{.Paste.Key}}{{.FileExt}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}} - - # Delete this object - curl --request DELETE {{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken | urlquery}} --{{end}} \ No newline at end of file diff --git a/assets/templates/html/newRedirectPasteSuccess.html.tmpl b/assets/templates/html/newRedirectPasteSuccess.html.tmpl deleted file mode 100644 index b1b0372..0000000 --- a/assets/templates/html/newRedirectPasteSuccess.html.tmpl +++ /dev/null @@ -1,16 +0,0 @@ -{{define "title"}} -Success - rushlink -{{end}} - -{{define "body"}} -
-{{.Host}}/{{.Paste.Key}} ---- - -# View metadata -curl {{.Host}}/{{.Paste.Key}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}} - -# Delete this object -curl --request DELETE {{.Host}}/{{.Paste.Key}}?deleteToken={{.Paste.DeleteToken | urlquery}} --{{end}} \ No newline at end of file diff --git a/assets/templates/txt/deletePasteSuccess.txt.tmpl b/assets/templates/txt/deletePasteSuccess.txt.tmpl deleted file mode 100644 index ac40c43..0000000 --- a/assets/templates/txt/deletePasteSuccess.txt.tmpl +++ /dev/null @@ -1 +0,0 @@ -<{{.Host}}/{{.Paste.Key}}> was succesfully deleted diff --git a/assets/templates/txt/newFileUploadPasteSuccess.txt.tmpl b/assets/templates/txt/newFileUploadPasteSuccess.txt.tmpl deleted file mode 100644 index a94e8cd..0000000 --- a/assets/templates/txt/newFileUploadPasteSuccess.txt.tmpl +++ /dev/null @@ -1,8 +0,0 @@ -{{.Host}}/{{.Paste.Key}}{{.FileExt}} ---- - -# View metadata -curl {{.Host}}/{{.Paste.Key}}{{.FileExt}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}} - -# Delete this object -curl --request DELETE {{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken | urlquery}} diff --git a/assets/templates/txt/newRedirectPasteSuccess.txt.tmpl b/assets/templates/txt/newRedirectPasteSuccess.txt.tmpl deleted file mode 100644 index 5b1e2f6..0000000 --- a/assets/templates/txt/newRedirectPasteSuccess.txt.tmpl +++ /dev/null @@ -1,8 +0,0 @@ -{{.Host}}/{{.Paste.Key}} ---- - -# View metadata -curl {{.Host}}/{{.Paste.Key}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}} - -# Delete this object -curl --request DELETE {{.Host}}/{{.Paste.Key}}?deleteToken={{.Paste.DeleteToken | urlquery}} diff --git a/handlers.go b/handlers.go index f7a9429..5d9da67 100644 --- a/handlers.go +++ b/handlers.go @@ -9,7 +9,6 @@ import ( "net/http" "net/url" "os" - "path/filepath" "time" "gitea.hashru.nl/dsprenkels/rushlink/internal/db" @@ -108,7 +107,7 @@ func (rl *rushlink) viewPasteHandlerInner(w http.ResponseWriter, r *http.Request if err != nil { return err } - fileExt = filepath.Ext(fu.FileName) + fileExt = fu.Ext() } return nil }); err != nil { @@ -213,8 +212,7 @@ func (rl *rushlink) newFileUploadPasteHandler(w http.ResponseWriter, r *http.Req }); err != nil { panic(err) } - data := map[string]interface{}{"Paste": paste, "FileUpload": fu} - rl.render(w, r, "newFileUploadPasteSuccess", data) + rl.renderCreateSuccess(w, r, paste, fu) } func (rl *rushlink) newPasteHandlerURLEncoded(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { @@ -254,8 +252,7 @@ func (rl *rushlink) newRedirectPasteHandler(w http.ResponseWriter, r *http.Reque }); err != nil { panic(err) } - data := map[string]interface{}{"Paste": paste} - rl.render(w, r, "newRedirectPasteSuccess", data) + rl.renderCreateSuccess(w, r, paste, nil) } // Delete a URL from the database @@ -270,35 +267,33 @@ func (rl *rushlink) deletePasteHandler(w http.ResponseWriter, r *http.Request) { } var errorCode int - var paste db.Paste + var paste *db.Paste if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error { - p, err := db.GetPaste(tx, key) + var err error + paste, err = db.GetPaste(tx, key) if err != nil { errorCode = http.StatusNotFound return err } - if p.State == db.PasteStateDeleted { + if paste.State == db.PasteStateDeleted { errorCode = http.StatusGone return errors.New("already deleted") } - if subtle.ConstantTimeCompare([]byte(deleteToken), []byte(p.DeleteToken)) == 0 { + if subtle.ConstantTimeCompare([]byte(deleteToken), []byte(paste.DeleteToken)) == 0 { errorCode = http.StatusForbidden return errors.New("invalid delete token") } - if err := p.Delete(tx, rl.fs); err != nil { + if err := paste.Delete(tx, rl.fs); err != nil { errorCode = http.StatusInternalServerError return err } - paste = *p return nil }); err != nil { log.Printf("error: %v\n", err) rl.renderError(w, r, errorCode, fmt.Sprintf("error: %v\n", err)) return } - - data := map[string]interface{}{"Paste": paste} - rl.render(w, r, "deletePasteSuccess", data) + rl.renderCreateSuccess(w, r, paste, nil) } // Add a new fileUpload redirect to the database diff --git a/internal/db/fileupload.go b/internal/db/fileupload.go index 45ceaf3..f4ff68e 100644 --- a/internal/db/fileupload.go +++ b/internal/db/fileupload.go @@ -9,6 +9,7 @@ import ( "net/url" "os" "path" + "path/filepath" "github.com/google/uuid" "github.com/pkg/errors" @@ -214,3 +215,8 @@ func (fu *FileUpload) URL() *url.URL { } return urlParse } + +// Ext returns the extension of the file attached to this FileUpload. +func (fu *FileUpload) Ext() string { + return filepath.Ext(fu.FileName) +} diff --git a/views.go b/views.go index b18072a..eaa9112 100644 --- a/views.go +++ b/views.go @@ -9,6 +9,7 @@ import ( "io" "log" "net/http" + "net/url" "path/filepath" "regexp" "runtime/debug" @@ -17,6 +18,8 @@ import ( "strings" text "text/template" + "gitea.hashru.nl/dsprenkels/rushlink/internal/db" + "github.com/pkg/errors" ) @@ -151,6 +154,19 @@ func (rl *rushlink) renderInternalServerError(w http.ResponseWriter, r *http.Req rl.renderError(w, r, http.StatusInternalServerError, msg) } +func (rl *rushlink) renderCreateSuccess(w http.ResponseWriter, r *http.Request, paste *db.Paste, fu *db.FileUpload) { + var fileExt string + if fu != nil { + fileExt = fu.Ext() + } + var redirectURL url.URL + redirectURL.Path = fmt.Sprintf("/%s%s/meta", paste.Key, fileExt) + queryVals := redirectURL.Query() + queryVals.Add("deleteToken", paste.DeleteToken) + redirectURL.RawQuery = queryVals.Encode() + http.Redirect(w, r, redirectURL.String(), http.StatusFound) +} + // resolveHost constructs the `scheme://host` part of rushlinks public API. // // If the `--host` flag is set, it will return that URL.