Skip to content

Commit

Permalink
encode short urls, emojis 🥳 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
bueti committed Dec 22, 2023
1 parent 0e3dd8c commit 5d9fdce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/api/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/http"
url2 "net/url"
"os"
"path/filepath"
"strings"
Expand All @@ -18,7 +19,7 @@ import (

func (app *application) redirectUrlHandler(c echo.Context) error {
wildcardValue := c.Param("*")
shortUrl := strings.TrimSuffix(wildcardValue, "/")
shortUrl := url2.PathEscape(strings.TrimSuffix(wildcardValue, "/"))
url, err := app.models.Urls.GetRedirect(shortUrl)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -58,6 +59,8 @@ func (app *application) createUrlHandlerPost(c echo.Context) error {
app.sessionManager.Put(c.Request().Context(), "flash_error", "URL cannot start with shrink.ch/s/")
case "user id is required":
app.sessionManager.Put(c.Request().Context(), "flash_error", "User ID is required.")
case "short_url is too long":
app.sessionManager.Put(c.Request().Context(), "flash_error", "Short URL is too long.")
default:
app.sessionManager.Put(c.Request().Context(), "flash_error", "Failed to create url.")
}
Expand Down
14 changes: 10 additions & 4 deletions internal/model/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"strings"
"time"

url2 "net/url"

"github.com/google/uuid"
"gorm.io/gorm"
)

// Create a UrlModel struct which wraps the connection pool.
// UrlModel is a struct which wraps the connection pool.
type UrlModel struct {
DB *gorm.DB
}
Expand All @@ -19,7 +21,7 @@ type Url struct {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primary_key" json:"id,omitempty"`
Original string `gorm:"type:varchar(2048);not null;uniqueIndex" json:"original"`
ShortUrl string `gorm:"type:varchar(11);not null;uniqueIndex" json:"short_url"`
ShortUrl string `gorm:"type:varchar(256);not null;uniqueIndex" json:"short_url"`
QRCodeURL string `gorm:"type:varchar(2048)" json:"qr_code_url,omitempty"`
UserID uuid.UUID `gorm:"type:uuid" json:"user_id"`
User User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Expand Down Expand Up @@ -71,7 +73,7 @@ func (u *UrlModel) Create(urlReq *UrlCreateRequest) (Url, error) {
return Url{}, fmt.Errorf("url cannot start with shrink.ch/s/")
}
if urlReq.ShortCode != "" {
url.ShortUrl = urlReq.ShortCode
url.ShortUrl = url2.PathEscape(urlReq.ShortCode)
} else {
id := base62Encode(rand.Uint64())
url.ShortUrl = id
Expand All @@ -85,6 +87,9 @@ func (u *UrlModel) Create(urlReq *UrlCreateRequest) (Url, error) {
if strings.Contains(result.Error.Error(), "duplicate key value violates unique constraint") {
return Url{}, fmt.Errorf("url already exists")
}
if strings.Contains(result.Error.Error(), "value too long for type character") {
return Url{}, fmt.Errorf("short_url is too long")
}
return Url{}, result.Error
}

Expand Down Expand Up @@ -125,10 +130,11 @@ func (u *UrlModel) GetUrlByUser(userId uuid.UUID) (*[]UrlByUserResponse, error)

resp := []UrlByUserResponse{}
for _, url := range urls {
shortUrl, _ := url2.PathUnescape(url.ShortUrl)
resp = append(resp, UrlByUserResponse{
ID: url.ID,
Original: url.Original,
ShortUrl: url.ShortUrl,
ShortUrl: shortUrl,
Visits: url.Visits,
QRCodeURL: url.QRCodeURL,
CreatedAt: url.CreatedAt,
Expand Down

0 comments on commit 5d9fdce

Please sign in to comment.