diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-11-07 00:32:39 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 972223c992ca5aa5e5d93cff3b2ee4e30182025b (patch) | |
tree | ff61695c734852aeafdc0e872cc6f47085a8e787 /util/util.go | |
parent | bc9051fd1a17e793647cf309c973a7feefebd98f (diff) |
restructuring part 5
Diffstat (limited to 'util/util.go')
-rw-r--r-- | util/util.go | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/util/util.go b/util/util.go index 5ee548e..8f32363 100644 --- a/util/util.go +++ b/util/util.go @@ -1,6 +1,7 @@ package util import ( + "fmt" "regexp" "strings" ) @@ -63,3 +64,135 @@ func GetActorFollowNameFromPath(path string) string { return actor } + +func StripTransferProtocol(value string) string { + re := regexp.MustCompile("(http://|https://)?(www.)?") + + value = re.ReplaceAllString(value, "") + + return value +} + +func GetCaptchaCode(captcha string) string { + re := regexp.MustCompile("\\w+\\.\\w+$") + code := re.FindString(captcha) + + re = regexp.MustCompile("\\w+") + code = re.FindString(code) + + return code +} + +func ShortURL(actorName string, url string) string { + + re := regexp.MustCompile(`.+\/`) + + actor := re.FindString(actorName) + + urlParts := strings.Split(url, "|") + + op := urlParts[0] + + var reply string + + if len(urlParts) > 1 { + reply = urlParts[1] + } + + re = regexp.MustCompile(`\w+$`) + temp := re.ReplaceAllString(op, "") + + if temp == actor { + id := LocalShort(op) + + re := regexp.MustCompile(`.+\/`) + replyCheck := re.FindString(reply) + + if reply != "" && replyCheck == actor { + id = id + "#" + LocalShort(reply) + } else if reply != "" { + id = id + "#" + RemoteShort(reply) + } + + return id + } else { + id := RemoteShort(op) + + re := regexp.MustCompile(`.+\/`) + replyCheck := re.FindString(reply) + + if reply != "" && replyCheck == actor { + id = id + "#" + LocalShort(reply) + } else if reply != "" { + id = id + "#" + RemoteShort(reply) + } + + return id + } +} + +func LocalShort(url string) string { + re := regexp.MustCompile(`\w+$`) + return re.FindString(StripTransferProtocol(url)) +} + +func RemoteShort(url string) string { + re := regexp.MustCompile(`\w+$`) + + id := re.FindString(StripTransferProtocol(url)) + + re = regexp.MustCompile(`.+/.+/`) + + actorurl := re.FindString(StripTransferProtocol(url)) + + re = regexp.MustCompile(`/.+/`) + + actorname := re.FindString(actorurl) + + actorname = strings.Replace(actorname, "/", "", -1) + + return "f" + actorname + "-" + id +} + +func ShortImg(url string) string { + nURL := url + + re := regexp.MustCompile(`(\.\w+$)`) + + fileName := re.ReplaceAllString(url, "") + + if len(fileName) > 26 { + re := regexp.MustCompile(`(^.{26})`) + + match := re.FindStringSubmatch(fileName) + + if len(match) > 0 { + nURL = match[0] + } + + re = regexp.MustCompile(`(\..+$)`) + + match = re.FindStringSubmatch(url) + + if len(match) > 0 { + nURL = nURL + "(...)" + match[0] + } + } + + return nURL +} + +func ConvertSize(size int64) string { + var rValue string + + convert := float32(size) / 1024.0 + + if convert > 1024 { + convert = convert / 1024.0 + rValue = fmt.Sprintf("%.2f MB", convert) + } else { + rValue = fmt.Sprintf("%.2f KB", convert) + } + + return rValue +} |