music-library/utils.go

69 lines
1.3 KiB
Go
Raw Normal View History

2023-02-14 21:59:56 +01:00
package main
import (
2023-02-16 15:15:41 +01:00
"encoding/base64"
2023-02-14 21:59:56 +01:00
"log"
"net/http"
"os"
"strings"
)
func MethodNotAllowed(w http.ResponseWriter, allowedMethods ...string) {
for _, method := range allowedMethods {
w.Header().Add("Allow", strings.ToUpper(method))
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
func InternalServerError(w http.ResponseWriter, err error) {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
func And[T any](f1, f2 func(T) bool) func(T) bool {
return func(v T) bool {
return f1(v) && f2(v)
}
}
func Or[T any](f1, f2 func(T) bool) func(T) bool {
return func(v T) bool {
return f1(v) || f2(v)
}
}
func Not[T any](f func(T) bool) func(T) bool {
return func(v T) bool {
return !f(v)
}
}
2023-02-16 12:16:04 +01:00
func IsSymlink(entry os.DirEntry) bool {
return entry.Type()&os.ModeSymlink == os.ModeSymlink
}
func IsRegular(entry os.DirEntry) bool {
return entry.Type().IsRegular()
}
2023-02-14 21:59:56 +01:00
func IsDir(entry os.DirEntry) bool {
return entry.IsDir()
}
func IsHidden(entry os.DirEntry) bool {
return strings.HasPrefix(entry.Name(), ".")
}
func FsEntry2Name(entry os.DirEntry) string {
return entry.Name()
}
2023-02-16 15:15:41 +01:00
func DecodeBase64String(str string) (string, error) {
2023-02-16 15:18:06 +01:00
data, err := base64.URLEncoding.DecodeString(str)
2023-02-16 15:15:41 +01:00
return string(data), err
}
func EncodeBase64String(str string) string {
2023-02-16 15:18:06 +01:00
return base64.URLEncoding.EncodeToString([]byte(str))
2023-02-16 15:15:41 +01:00
}