b5cd9498b1
This is a long pending refactor. All the DB, query, CRUD, and related logic scattered across HTTP handlers are now moved into a central `core` package with clean, abstracted methods, decoupling HTTP handlers from executing direct DB queries and other business logic. eg: `core.CreateList()`, `core.GetLists()` etc. - Remove obsolete subscriber methods. - Move optin hook queries to core. - Move campaign methods to `core`. - Move all campaign methods to `core`. - Move public page functions to `core`. - Move all template functions to `core`. - Move media and settings function to `core`. - Move handler middleware functions to `core`. - Move all bounce functions to `core`. - Move all dashboard functions to `core`. - Fix GetLists() not honouring type - Fix unwrapped JSON responses. - Clean up obsolete pre-core util function. - Replace SQL array null check with cardinality check. - Fix missing validations in `core` queries. - Remove superfluous deps on internal `subimporter`. - Add dashboard functions to `core`. - Fix broken domain ban check. - Fix broken subscriber check middleware. - Remove redundant error handling. - Remove obsolete functions. - Remove obsolete structs. - Remove obsolete queries and DB functions. - Document the `core` package.
101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
regexpSpaces = regexp.MustCompile(`[\s]+`)
|
|
)
|
|
|
|
// 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
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// makeFilename sanitizes a filename (user supplied upload filenames).
|
|
func makeFilename(fName string) string {
|
|
name := strings.TrimSpace(fName)
|
|
if name == "" {
|
|
name, _ = generateRandomString(10)
|
|
}
|
|
// replace whitespace with "-"
|
|
name = regexpSpaces.ReplaceAllString(name, "-")
|
|
return filepath.Base(name)
|
|
}
|
|
|
|
// makeMsgTpl takes a page title, heading, and message and returns
|
|
// a msgTpl that can be rendered as an HTML view. This is used for
|
|
// rendering arbitrary HTML views with error and success messages.
|
|
func makeMsgTpl(pageTitle, heading, msg string) msgTpl {
|
|
if heading == "" {
|
|
heading = pageTitle
|
|
}
|
|
err := msgTpl{}
|
|
err.Title = pageTitle
|
|
err.MessageTitle = heading
|
|
err.Message = msg
|
|
return err
|
|
}
|
|
|
|
// parseStringIDs takes a slice of numeric string IDs and
|
|
// parses each number into an int64 and returns a slice of the
|
|
// resultant values.
|
|
func parseStringIDs(s []string) ([]int, error) {
|
|
vals := make([]int, 0, len(s))
|
|
for _, v := range s {
|
|
i, err := strconv.Atoi(v)
|
|
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
|
|
}
|
|
|
|
// generateRandomString generates a cryptographically random, alphanumeric string of length n.
|
|
func generateRandomString(n int) (string, error) {
|
|
const dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
var bytes = make([]byte, n)
|
|
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|