diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-11-09 19:37:34 -0400 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | e15bf8cd1375f24251929ff3e13f883f692ee03a (patch) | |
tree | 661fec8cc005630a67df1fc4c933a1e8a19f686b /util/util.go | |
parent | 7116705959bad9244893434039a0da9b500f5043 (diff) |
slacking off is great; here's part 6
Diffstat (limited to 'util/util.go')
-rw-r--r-- | util/util.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/util/util.go b/util/util.go index 8f32363..7da4671 100644 --- a/util/util.go +++ b/util/util.go @@ -1,7 +1,10 @@ package util import ( + "crypto/sha256" + "encoding/hex" "fmt" + "os" "regexp" "strings" ) @@ -196,3 +199,42 @@ func ConvertSize(size int64) string { return rValue } + +// IsInStringArray looks for a string in a string array and returns true if it is found. +func IsInStringArray(haystack []string, needle string) bool { + for _, e := range haystack { + if e == needle { + return true + } + } + return false +} + +// GetUniqueFilename will look for an available random filename in the /public/ directory. +func GetUniqueFilename(ext string) string { + id := RandomID(8) + file := "/public/" + id + "." + ext + + for true { + if _, err := os.Stat("." + file); err == nil { + id = RandomID(8) + file = "/public/" + id + "." + ext + } else { + return "/public/" + id + "." + ext + } + } + + return "" +} + +func HashMedia(media string) string { + h := sha256.New() + h.Write([]byte(media)) + return hex.EncodeToString(h.Sum(nil)) +} + +func HashBytes(media []byte) string { + h := sha256.New() + h.Write(media) + return hex.EncodeToString(h.Sum(nil)) +} |