Add custom short code support to web UI
Allow users to optionally specify their own short code when shortening a URL, instead of always generating a random one. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
11
handler.go
11
handler.go
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.wntrmute.dev/mc/mcdsl/auth"
|
||||
@@ -82,6 +83,14 @@ func (srv *server) postURL(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
customShort := strings.TrimSpace(r.FormValue("custom"))
|
||||
if customShort != "" {
|
||||
if !regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(customShort) {
|
||||
http.Error(w, "custom short code must be letters and numbers only", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
url, err = links.CleanString(url, stripQuery)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -89,7 +98,7 @@ func (srv *server) postURL(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
short, err := links.StoreURL(ctx, srv.db, url)
|
||||
short, err := links.StoreURL(ctx, srv.db, url, customShort)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user