From e15bf8cd1375f24251929ff3e13f883f692ee03a Mon Sep 17 00:00:00 2001 From: KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> Date: Tue, 9 Nov 2021 19:37:34 -0400 Subject: slacking off is great; here's part 6 --- util/util.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'util') 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)) +} -- cgit v1.2.3