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/http"
"net/url" "net/url"
"os" "os"
"path/filepath"
"time" "time"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db" "gitea.hashru.nl/dsprenkels/rushlink/internal/db"
@ -108,7 +107,7 @@ func (rl *rushlink) viewPasteHandlerInner(w http.ResponseWriter, r *http.Request
if err != nil { if err != nil {
return err return err
} }
fileExt = filepath.Ext(fu.FileName) fileExt = fu.Ext()
} }
return nil return nil
}); err != nil { }); err != nil {
@ -213,8 +212,7 @@ func (rl *rushlink) newFileUploadPasteHandler(w http.ResponseWriter, r *http.Req
}); err != nil { }); err != nil {
panic(err) panic(err)
} }
data := map[string]interface{}{"Paste": paste, "FileUpload": fu} rl.renderCreateSuccess(w, r, paste, fu)
rl.render(w, r, "newFileUploadPasteSuccess", data)
} }
func (rl *rushlink) newPasteHandlerURLEncoded(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { 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 { }); err != nil {
panic(err) panic(err)
} }
data := map[string]interface{}{"Paste": paste} rl.renderCreateSuccess(w, r, paste, nil)
rl.render(w, r, "newRedirectPasteSuccess", data)
} }
// Delete a URL from the database // Delete a URL from the database
@ -270,35 +267,33 @@ func (rl *rushlink) deletePasteHandler(w http.ResponseWriter, r *http.Request) {
} }
var errorCode int var errorCode int
var paste db.Paste var paste *db.Paste
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error { 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 { if err != nil {
errorCode = http.StatusNotFound errorCode = http.StatusNotFound
return err return err
} }
if p.State == db.PasteStateDeleted { if paste.State == db.PasteStateDeleted {
errorCode = http.StatusGone errorCode = http.StatusGone
return errors.New("already deleted") 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 errorCode = http.StatusForbidden
return errors.New("invalid delete token") 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 errorCode = http.StatusInternalServerError
return err return err
} }
paste = *p
return nil return nil
}); err != nil { }); err != nil {
log.Printf("error: %v\n", err) log.Printf("error: %v\n", err)
rl.renderError(w, r, errorCode, fmt.Sprintf("error: %v\n", err)) rl.renderError(w, r, errorCode, fmt.Sprintf("error: %v\n", err))
return return
} }
rl.renderCreateSuccess(w, r, paste, nil)
data := map[string]interface{}{"Paste": paste}
rl.render(w, r, "deletePasteSuccess", data)
} }
// Add a new fileUpload redirect to the database // Add a new fileUpload redirect to the database

View File

@ -9,6 +9,7 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -214,3 +215,8 @@ func (fu *FileUpload) URL() *url.URL {
} }
return urlParse 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" "io"
"log" "log"
"net/http" "net/http"
"net/url"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime/debug" "runtime/debug"
@ -17,6 +18,8 @@ import (
"strings" "strings"
text "text/template" text "text/template"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
"github.com/pkg/errors" "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) 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. // resolveHost constructs the `scheme://host` part of rushlinks public API.
// //
// If the `--host` flag is set, it will return that URL. // If the `--host` flag is set, it will return that URL.