streamwatcher/util.go

48 lines
1.1 KiB
Go
Executable file

package main
import (
"crypto/sha256"
"encoding/base64"
"log"
"os"
"path/filepath"
"strconv"
)
// URLToBase64 returns the base64 encoding of the URL parameter
func URLToBase64(URL string) string {
h := sha256.New()
h.Write([]byte(URL))
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
// Base64ToURL returns the string decoded base64 value
func Base64ToURL(value string) string {
decoded, _ := base64.URLEncoding.DecodeString(value)
return string(decoded)
}
// FileExists returns true if path exists
func FileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// GetStreamDirPath returns filepath.Join(".", "streams")
func GetStreamDirPath() string {
return filepath.Join(".", "streams")
}
// StreamStorePathExists returns FileExists(GetStreamDirPath())
func StreamStorePathExists() bool {
return FileExists(GetStreamDirPath())
}
// StrToInt uses strconv.ParseInt
func StrToInt(str string) int {
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Println("Could not convert string to int ", str)
}
return int(i)
}