music-library/utils.go

69 lines
1.3 KiB
Go

package main
import (
"encoding/base64"
"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)
}
}
func IsSymlink(entry os.DirEntry) bool {
return entry.Type()&os.ModeSymlink == os.ModeSymlink
}
func IsRegular(entry os.DirEntry) bool {
return entry.Type().IsRegular()
}
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()
}
func DecodeBase64String(str string) (string, error) {
data, err := base64.URLEncoding.DecodeString(str)
return string(data), err
}
func EncodeBase64String(str string) string {
return base64.URLEncoding.EncodeToString([]byte(str))
}