1
Fork 0
photoview/api/utils/utils.go

83 lines
1.6 KiB
Go
Raw Normal View History

2020-02-09 21:25:33 +01:00
package utils
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"path"
)
2020-02-09 21:25:33 +01:00
func GenerateToken() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const length = 8
2020-02-21 12:13:49 +01:00
charLen := big.NewInt(int64(len(charset)))
2020-02-09 21:25:33 +01:00
b := make([]byte, length)
for i := range b {
2020-02-21 12:13:49 +01:00
n, err := rand.Int(rand.Reader, charLen)
if err != nil {
log.Fatalf("Could not generate random number: %s\n", err)
}
b[i] = charset[n.Int64()]
2020-02-09 21:25:33 +01:00
}
return string(b)
}
type PhotoviewError struct {
message string
original error
}
func (e PhotoviewError) Error() string {
return fmt.Sprintf("%s: %s", e.message, e.original)
}
func HandleError(message string, err error) PhotoviewError {
log.Printf("ERROR: %s: %s", message, err)
return PhotoviewError{
message: message,
original: err,
}
}
2021-02-15 17:35:28 +01:00
2021-04-24 18:51:21 +02:00
var test_cache_path string = ""
func ConfigureTestCache(tmp_dir string) {
test_cache_path = tmp_dir
}
2021-02-15 17:35:28 +01:00
// MediaCachePath returns the path for where the media cache is located on the file system
func MediaCachePath() string {
2021-04-24 18:51:21 +02:00
if test_cache_path != "" {
return test_cache_path
}
2021-02-15 17:35:28 +01:00
photoCache := EnvMediaCachePath.GetValue()
if photoCache == "" {
photoCache = "./media_cache"
}
return photoCache
}
var test_face_recognition_models_path string = ""
func ConfigureTestFaceRecognitionModelsPath(path string) {
test_face_recognition_models_path = path
}
func FaceRecognitionModelsPath() string {
if test_face_recognition_models_path != "" {
return test_face_recognition_models_path
}
if EnvFaceRecognitionModelsPath.GetValue() == "" {
return path.Join("data", "models")
}
return EnvFaceRecognitionModelsPath.GetValue()
}