feat(app) implemented lab 4

This commit is contained in:
imadev
2026-11-06 19:06:21 +03:00
parent 68968c4a3a
commit b4239f8cf0
63 changed files with 3944 additions and 137 deletions
+41 -4
View File
@@ -3,6 +3,7 @@ package common
import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
"github.com/google/uuid"
@@ -62,13 +63,49 @@ func ValidateChatTitle(title string) error {
return nil
}
func ValidateMessageText(text string) error {
if blank(text) {
func ValidateMessageText(text string, attachmentCount int) error {
if attachmentCount == 0 && blank(text) {
return BadRequest("text must not be empty or only whitespace")
}
if !strLenBetween(text, 1, 4000) {
return BadRequest("text must be between 1 and 4000 Unicode code points")
if !strLenBetween(text, 0, 4000) {
return BadRequest("text must be between 0 and 4000 Unicode code points")
}
return nil
}
func ValidateAttachmentIDs(ids []uuid.UUID) error {
if len(ids) > 10 {
return BadRequest("attachment_ids must contain at most 10 items")
}
seen := make(map[uuid.UUID]bool, len(ids))
for _, id := range ids {
if seen[id] {
return BadRequest("attachment_ids must not contain duplicates")
}
seen[id] = true
}
return nil
}
func ValidateFilename(filename string) error {
if !strLenBetween(filename, 1, 255) {
return BadRequest("filename must be between 1 and 255 Unicode code points")
}
if blank(filename) {
return BadRequest("filename must not be empty or only whitespace")
}
if filename == "." || filename == ".." {
return BadRequest("filename is not allowed")
}
for _, r := range filename {
if r == '/' || r == '\\' || r == 0 || unicode.IsControl(r) {
return BadRequest("filename contains a forbidden character")
}
}
return nil