Redirect to /meta after upload/shorten

This commit is contained in:
Daan Sprenkels 2019-12-16 10:51:21 +05:30
parent d34ac11d5e
commit ca859adab1
9 changed files with 32 additions and 67 deletions

View File

@ -1,3 +0,0 @@
{{define "title"}}
Success - rushlink
{{end}}

View File

@ -1,16 +0,0 @@
{{define "title"}}
Success - rushlink
{{end}}
{{define "body"}}
<pre>
{{.Host}}/{{.Paste.Key}}{{.FileExt}}
---
# View metadata
curl <a href="{{.Host}}/{{.Paste.Key}}{{.FileExt}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}}">{{.Host}}/{{.Paste.Key}}{{.FileExt}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}}</a>
# Delete this object
curl --request DELETE <a href="{{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken | urlquery}}">{{.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken | urlquery}}</a>
</pre>
{{end}}

View File

@ -1,16 +0,0 @@
{{define "title"}}
Success - rushlink
{{end}}
{{define "body"}}
<pre>
{{.Host}}/{{.Paste.Key}}
---
# View metadata
curl <a href="{{.Host}}/{{.Paste.Key}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}}">{{.Host}}/{{.Paste.Key}}/meta?deleteToken={{.Paste.DeleteToken | urlquery}}</a>
# Delete this object
curl --request DELETE <a href="{{.Host}}/{{.Paste.Key}}?deleteToken={{.Paste.DeleteToken | urlquery}}">{{.Host}}/{{.Paste.Key}}?deleteToken={{.Paste.DeleteToken | urlquery}}</a>
</pre>
{{end}}

View File

@ -1 +0,0 @@
<{{.Host}}/{{.Paste.Key}}> was succesfully deleted

View File

@ -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}}

View File

@ -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}}

View File

@ -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

View File

@ -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)
}

View File

@ -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.