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:
20
links/url.go
20
links/url.go
@@ -134,14 +134,16 @@ func Lookup(ctx context.Context, db *sql.DB, s string) (string, error) {
|
||||
return short, nil
|
||||
}
|
||||
|
||||
func StoreURL(ctx context.Context, db *sql.DB, s string) (string, error) {
|
||||
short, err := Lookup(ctx, db, s)
|
||||
if err == nil {
|
||||
return short, nil
|
||||
}
|
||||
func StoreURL(ctx context.Context, db *sql.DB, s string, customShort string) (string, error) {
|
||||
if customShort == "" {
|
||||
short, err := Lookup(ctx, db, s)
|
||||
if err == nil {
|
||||
return short, nil
|
||||
}
|
||||
|
||||
if err != sql.ErrNoRows {
|
||||
return "", err
|
||||
if err != sql.ErrNoRows {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
u, err := FromString(s)
|
||||
@@ -149,6 +151,10 @@ func StoreURL(ctx context.Context, db *sql.DB, s string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if customShort != "" {
|
||||
u.Short = customShort
|
||||
}
|
||||
|
||||
err = u.Store(ctx, db)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user