mirror of
https://github.com/axllent/mailpit.git
synced 2025-07-01 00:45:27 +02:00
- Updated error handling to use the error return value for resource closures in tests and functions, ensuring proper error reporting. - Replaced direct calls to `Close()` with deferred functions that handle errors gracefully. - Improved readability by using `strings.ReplaceAll` instead of `strings.Replace` for string manipulation. - Enhanced network connection handling by adding default cases for unsupported network types. - Updated HTTP response handling to use the appropriate status codes and error messages. - Removed unused variables and commented-out code to clean up the codebase.
52 lines
947 B
Go
52 lines
947 B
Go
package config
|
|
|
|
import (
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/axllent/mailpit/internal/tools"
|
|
)
|
|
|
|
// IsFile returns whether a file exists and is readable
|
|
func isFile(path string) bool {
|
|
f, err := os.Open(filepath.Clean(path))
|
|
defer func() { _ = f.Close() }()
|
|
return err == nil
|
|
}
|
|
|
|
// IsDir returns whether a path is a directory
|
|
func isDir(path string) bool {
|
|
info, err := os.Stat(path)
|
|
if err != nil || os.IsNotExist(err) || !info.IsDir() {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func isValidURL(s string) bool {
|
|
u, err := url.ParseRequestURI(s)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return strings.HasPrefix(u.Scheme, "http")
|
|
}
|
|
|
|
// DBTenantID converts a tenant ID to a DB-friendly value if set
|
|
func DBTenantID(s string) string {
|
|
s = tools.Normalize(s)
|
|
if s != "" {
|
|
re := regexp.MustCompile(`[^a-zA-Z0-9\_]`)
|
|
s = re.ReplaceAllString(s, "_")
|
|
if !strings.HasSuffix(s, "_") {
|
|
s = s + "_"
|
|
}
|
|
}
|
|
|
|
return s
|
|
}
|