Custom URLs: validated slugs with reserved words, uniqueness across pastes and cans
This commit is contained in:
@@ -168,6 +168,20 @@ func (s *Store) CreatePaste(p *Paste) (*Paste, error) {
|
||||
pwHash = &h
|
||||
}
|
||||
|
||||
if p.CustomSlug != nil && *p.CustomSlug != "" {
|
||||
slug := *p.CustomSlug
|
||||
if err := ValidateCustomSlug(slug); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
taken, err := s.SlugTaken(slug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if taken {
|
||||
return nil, errSlugTaken
|
||||
}
|
||||
}
|
||||
|
||||
visibility := p.Visibility
|
||||
if visibility == "" {
|
||||
visibility = "public"
|
||||
@@ -181,10 +195,14 @@ func (s *Store) CreatePaste(p *Paste) (*Paste, error) {
|
||||
contentType = "text/plain"
|
||||
}
|
||||
|
||||
var slugVal *string
|
||||
if p.CustomSlug != nil && *p.CustomSlug != "" {
|
||||
slugVal = p.CustomSlug
|
||||
}
|
||||
_, err := s.db.Exec(`INSERT INTO pastes
|
||||
(id, content, content_type, language, title, password_hash, expires_at, burn_after_read, visibility, created_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?)`,
|
||||
id, p.Content, contentType, p.Language, p.Title, pwHash, expiresAt, boolToInt(p.BurnAfterRead), visibility, now)
|
||||
(id, custom_slug, content, content_type, language, title, password_hash, expires_at, burn_after_read, visibility, created_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?)`,
|
||||
id, slugVal, p.Content, contentType, p.Language, p.Title, pwHash, expiresAt, boolToInt(p.BurnAfterRead), visibility, now)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -337,10 +355,6 @@ func (a *apiServer) handleCreatePaste(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, 413, fmt.Sprintf("content exceeds max %d bytes", a.cfg.MaxTextBytes))
|
||||
return
|
||||
}
|
||||
if p.CustomSlug != nil && *p.CustomSlug != "" {
|
||||
writeErr(w, 400, "custom slugs not implemented yet")
|
||||
return
|
||||
}
|
||||
created, err := a.store.CreatePaste(&p)
|
||||
if err != nil {
|
||||
writeErr(w, 400, err.Error())
|
||||
|
||||
Reference in New Issue
Block a user