summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/mattermost-server/v5/model/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mattermost/mattermost-server/v5/model/utils.go')
-rw-r--r--vendor/github.com/mattermost/mattermost-server/v5/model/utils.go103
1 files changed, 53 insertions, 50 deletions
diff --git a/vendor/github.com/mattermost/mattermost-server/v5/model/utils.go b/vendor/github.com/mattermost/mattermost-server/v5/model/utils.go
index 3aed19da..0c5a272c 100644
--- a/vendor/github.com/mattermost/mattermost-server/v5/model/utils.go
+++ b/vendor/github.com/mattermost/mattermost-server/v5/model/utils.go
@@ -18,10 +18,11 @@ import (
"regexp"
"strconv"
"strings"
+ "sync"
"time"
"unicode"
- goi18n "github.com/mattermost/go-i18n/i18n"
+ "github.com/mattermost/mattermost-server/v5/shared/i18n"
"github.com/pborman/uuid"
)
@@ -30,10 +31,10 @@ const (
UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
NUMBERS = "0123456789"
SYMBOLS = " !\"\\#$%&'()*+,-./:;<=>?@[]^_`|~"
+ MB = 1 << 20
)
type StringInterface map[string]interface{}
-type StringMap map[string]string
type StringArray []string
func (sa StringArray) Remove(input string) StringArray {
@@ -72,10 +73,13 @@ func (sa StringArray) Equals(input StringArray) bool {
return true
}
-var translateFunc goi18n.TranslateFunc = nil
+var translateFunc i18n.TranslateFunc
+var translateFuncOnce sync.Once
-func AppErrorInit(t goi18n.TranslateFunc) {
- translateFunc = t
+func AppErrorInit(t i18n.TranslateFunc) {
+ translateFuncOnce.Do(func() {
+ translateFunc = t
+ })
}
type AppError struct {
@@ -93,7 +97,7 @@ func (er *AppError) Error() string {
return er.Where + ": " + er.Message + ", " + er.DetailedError
}
-func (er *AppError) Translate(T goi18n.TranslateFunc) {
+func (er *AppError) Translate(T i18n.TranslateFunc) {
if T == nil {
er.Message = er.Id
return
@@ -106,12 +110,11 @@ func (er *AppError) Translate(T goi18n.TranslateFunc) {
}
}
-func (er *AppError) SystemMessage(T goi18n.TranslateFunc) string {
+func (er *AppError) SystemMessage(T i18n.TranslateFunc) string {
if er.params == nil {
return T(er.Id)
- } else {
- return T(er.Id, er.params)
}
+ return T(er.Id, er.params)
}
func (er *AppError) ToJson() string {
@@ -132,11 +135,10 @@ func AppErrorFromJson(data io.Reader) *AppError {
decoder := json.NewDecoder(strings.NewReader(str))
var er AppError
err := decoder.Decode(&er)
- if err == nil {
- return &er
- } else {
+ if err != nil {
return NewAppError("AppErrorFromJson", "model.utils.decode_json.app_error", nil, "body: "+str, http.StatusInternalServerError)
}
+ return &er
}
func NewAppError(where string, id string, params map[string]interface{}, details string, status int) *AppError {
@@ -183,14 +185,6 @@ func NewRandomString(length int) string {
return encoding.EncodeToString(data)[:length]
}
-// NewRandomBase32String returns a base32 encoded string of a random slice
-// of bytes of the given size. The resulting entropy will be (8 * size) bits.
-func NewRandomBase32String(size int) string {
- data := make([]byte, size)
- rand.Read(data)
- return base32.StdEncoding.EncodeToString(data)
-}
-
// GetMillis is a convenience method to get milliseconds since epoch.
func GetMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
@@ -201,6 +195,11 @@ func GetMillisForTime(thisTime time.Time) int64 {
return thisTime.UnixNano() / int64(time.Millisecond)
}
+// GetTimeForMillis is a convenience method to get time.Time for milliseconds since epoch.
+func GetTimeForMillis(millis int64) time.Time {
+ return time.Unix(0, millis*int64(time.Millisecond))
+}
+
// PadDateStringZeros is a convenience method to pad 2 digit date parts with zeros to meet ISO 8601 format
func PadDateStringZeros(dateString string) string {
parts := strings.Split(dateString, "-")
@@ -254,9 +253,8 @@ func MapFromJson(data io.Reader) map[string]string {
var objmap map[string]string
if err := decoder.Decode(&objmap); err != nil {
return make(map[string]string)
- } else {
- return objmap
}
+ return objmap
}
// MapFromJson will decode the key/value pair map
@@ -266,9 +264,8 @@ func MapBoolFromJson(data io.Reader) map[string]bool {
var objmap map[string]bool
if err := decoder.Decode(&objmap); err != nil {
return make(map[string]bool)
- } else {
- return objmap
}
+ return objmap
}
func ArrayToJson(objmap []string) string {
@@ -282,9 +279,8 @@ func ArrayFromJson(data io.Reader) []string {
var objmap []string
if err := decoder.Decode(&objmap); err != nil {
return make([]string, 0)
- } else {
- return objmap
}
+ return objmap
}
func ArrayFromInterface(data interface{}) []string {
@@ -315,9 +311,8 @@ func StringInterfaceFromJson(data io.Reader) map[string]interface{} {
var objmap map[string]interface{}
if err := decoder.Decode(&objmap); err != nil {
return make(map[string]interface{})
- } else {
- return objmap
}
+ return objmap
}
func StringToJson(s string) string {
@@ -331,14 +326,19 @@ func StringFromJson(data io.Reader) string {
var s string
if err := decoder.Decode(&s); err != nil {
return ""
- } else {
- return s
}
+ return s
+}
+
+// ToJson serializes an arbitrary data type to JSON, discarding the error.
+func ToJson(v interface{}) []byte {
+ b, _ := json.Marshal(v)
+ return b
}
func GetServerIpAddress(iface string) string {
var addrs []net.Addr
- if len(iface) == 0 {
+ if iface == "" {
var err error
addrs, err = net.InterfaceAddrs()
if err != nil {
@@ -397,6 +397,7 @@ var reservedName = []string{
"channel",
"claim",
"error",
+ "files",
"help",
"landing",
"login",
@@ -437,6 +438,12 @@ func IsValidAlphaNumHyphenUnderscore(s string, withFormat bool) bool {
return validSimpleAlphaNumHyphenUnderscore.MatchString(s)
}
+func IsValidAlphaNumHyphenUnderscorePlus(s string) bool {
+
+ validSimpleAlphaNumHyphenUnderscorePlus := regexp.MustCompile(`^[a-zA-Z0-9+_-]+$`)
+ return validSimpleAlphaNumHyphenUnderscorePlus.MatchString(s)
+}
+
func Etag(parts ...interface{}) string {
etag := CurrentVersion
@@ -486,25 +493,6 @@ func ParseHashtags(text string) (string, string) {
return strings.TrimSpace(hashtagString), strings.TrimSpace(plainString)
}
-func IsFileExtImage(ext string) bool {
- ext = strings.ToLower(ext)
- for _, imgExt := range IMAGE_EXTENSIONS {
- if ext == imgExt {
- return true
- }
- }
- return false
-}
-
-func GetImageMimeType(ext string) string {
- ext = strings.ToLower(ext)
- if len(IMAGE_MIME_TYPES[ext]) == 0 {
- return "image"
- } else {
- return IMAGE_MIME_TYPES[ext]
- }
-}
-
func ClearMentionTags(post string) string {
post = strings.Replace(post, "<mention>", "", -1)
post = strings.Replace(post, "</mention>", "", -1)
@@ -721,3 +709,18 @@ func filterBlocklist(r rune) rune {
return r
}
+
+// UniqueStrings returns a unique subset of the string slice provided.
+func UniqueStrings(input []string) []string {
+ u := make([]string, 0, len(input))
+ m := make(map[string]bool)
+
+ for _, val := range input {
+ if _, ok := m[val]; !ok {
+ m[val] = true
+ u = append(u, val)
+ }
+ }
+
+ return u
+}