2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-24 07:37:13 +00:00
|
|
|
"bytes"
|
2018-10-25 13:51:47 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2021-06-19 11:41:27 +00:00
|
|
|
"path/filepath"
|
2018-10-25 13:51:47 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-15 07:53:58 +00:00
|
|
|
regexpSpaces = regexp.MustCompile(`[\s]+`)
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
2021-05-23 14:47:42 +00:00
|
|
|
// inArray checks if a string is present in a list of strings.
|
|
|
|
func inArray(val string, vals []string) (ok bool) {
|
|
|
|
for _, v := range vals {
|
|
|
|
if v == val {
|
|
|
|
return true
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-23 14:47:42 +00:00
|
|
|
return false
|
2019-10-15 12:21:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 11:41:27 +00:00
|
|
|
// makeFilename sanitizes a filename (user supplied upload filenames).
|
|
|
|
func makeFilename(fName string) string {
|
2019-10-15 12:21:32 +00:00
|
|
|
name := strings.TrimSpace(fName)
|
|
|
|
if name == "" {
|
|
|
|
name, _ = generateRandomString(10)
|
|
|
|
}
|
2022-01-15 07:53:58 +00:00
|
|
|
// replace whitespace with "-"
|
|
|
|
name = regexpSpaces.ReplaceAllString(name, "-")
|
2021-06-19 11:41:27 +00:00
|
|
|
return filepath.Base(name)
|
2019-10-15 12:21:32 +00:00
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-07-18 07:14:46 +00:00
|
|
|
// makeMsgTpl takes a page title, heading, and message and returns
|
2022-02-28 13:19:50 +00:00
|
|
|
// a msgTpl that can be rendered as an HTML view. This is used for
|
2019-10-25 05:41:47 +00:00
|
|
|
// rendering arbitrary HTML views with error and success messages.
|
2019-07-18 07:14:46 +00:00
|
|
|
func makeMsgTpl(pageTitle, heading, msg string) msgTpl {
|
2018-10-31 12:54:21 +00:00
|
|
|
if heading == "" {
|
|
|
|
heading = pageTitle
|
|
|
|
}
|
2019-07-18 07:14:46 +00:00
|
|
|
err := msgTpl{}
|
2018-10-31 12:54:21 +00:00
|
|
|
err.Title = pageTitle
|
2019-07-18 07:14:46 +00:00
|
|
|
err.MessageTitle = heading
|
|
|
|
err.Message = msg
|
2018-10-31 12:54:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-12-18 05:24:55 +00:00
|
|
|
|
|
|
|
// parseStringIDs takes a slice of numeric string IDs and
|
|
|
|
// parses each number into an int64 and returns a slice of the
|
|
|
|
// resultant values.
|
2022-04-03 15:24:40 +00:00
|
|
|
func parseStringIDs(s []string) ([]int, error) {
|
|
|
|
vals := make([]int, 0, len(s))
|
2018-12-18 05:24:55 +00:00
|
|
|
for _, v := range s {
|
2022-04-03 15:24:40 +00:00
|
|
|
i, err := strconv.Atoi(v)
|
2018-12-18 05:24:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < 1 {
|
|
|
|
return nil, fmt.Errorf("%d is not a valid ID", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = append(vals, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vals, nil
|
|
|
|
}
|
2019-10-15 12:21:32 +00:00
|
|
|
|
|
|
|
// generateRandomString generates a cryptographically random, alphanumeric string of length n.
|
|
|
|
func generateRandomString(n int) (string, error) {
|
|
|
|
const dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
var bytes = make([]byte, n)
|
2020-03-07 19:05:34 +00:00
|
|
|
|
2019-10-15 12:21:32 +00:00
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bytes), nil
|
|
|
|
}
|
2020-03-08 07:33:38 +00:00
|
|
|
|
|
|
|
// strHasLen checks if the given string has a length within min-max.
|
|
|
|
func strHasLen(str string, min, max int) bool {
|
|
|
|
return len(str) >= min && len(str) <= max
|
|
|
|
}
|
2020-10-24 14:30:29 +00:00
|
|
|
|
|
|
|
// strSliceContains checks if a string is present in the string slice.
|
|
|
|
func strSliceContains(str string, sl []string) bool {
|
|
|
|
for _, s := range sl {
|
|
|
|
if s == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2023-06-24 07:37:13 +00:00
|
|
|
|
2023-08-06 05:19:12 +00:00
|
|
|
func trimNullBytes(b []byte) string {
|
2023-06-24 07:37:13 +00:00
|
|
|
return string(bytes.Trim(b, "\x00"))
|
|
|
|
}
|