128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
package ersteller
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func ParseIntWithDefault(input string, defaultValue int) int {
|
|
value, err := strconv.Atoi(input)
|
|
if err != nil {
|
|
return defaultValue
|
|
}
|
|
return value
|
|
}
|
|
|
|
func ParseFloat32WithDefault(input string, defaultValue float32) float32 {
|
|
value, err := strconv.ParseFloat(input, 32)
|
|
if err != nil {
|
|
return defaultValue
|
|
}
|
|
return float32(value)
|
|
}
|
|
|
|
func ParseBoolWithDefault(input string, defaultValue bool) bool {
|
|
if input == "true" {
|
|
return true
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func ParseCheckboxWithDefault(input string, defaultValue bool) bool {
|
|
if input == "on" {
|
|
return true
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func ParseTime(timeString string, timezone string) time.Time {
|
|
if timezone == "" {
|
|
timezone = "Europe/Berlin"
|
|
}
|
|
location, err := time.LoadLocation(timezone)
|
|
if err != nil {
|
|
println(fmt.Sprintf("failed to load location %v, error: %v", timezone, err))
|
|
location = time.Local
|
|
}
|
|
timeObject, err := time.Parse("2006-01-02T15:04", timeString)
|
|
if err == nil {
|
|
timeObject, err = time.ParseInLocation("2006-01-02T15:04", timeString, location)
|
|
}
|
|
if err != nil {
|
|
timeObject = time.UnixMilli(0).UTC()
|
|
}
|
|
return timeObject
|
|
}
|
|
|
|
func TimeToString(time time.Time) string {
|
|
if time.UnixMilli() == 0 {
|
|
return ""
|
|
}
|
|
return time.Format("2006-01-02 15:04")
|
|
}
|
|
|
|
func TimeToValue(time time.Time) string {
|
|
if time.UnixMilli() == 0 {
|
|
return ""
|
|
}
|
|
return time.Format("2006-01-02T15:04")
|
|
}
|
|
|
|
func MoneyCentsToString(money int, currency string, lang Language) string {
|
|
beforeDecimals := money / 100
|
|
afterDecimals := money % 100
|
|
|
|
if afterDecimals == 0 {
|
|
// Only show whole numbers when cents are 0
|
|
if lang == De {
|
|
return fmt.Sprintf("%d %s", beforeDecimals, currency)
|
|
}
|
|
return fmt.Sprintf("%s%d", currency, beforeDecimals)
|
|
}
|
|
|
|
// Show cents with leading zero when < 10 (always two digits)
|
|
if lang == De {
|
|
return fmt.Sprintf("%d,%02d %s", beforeDecimals, afterDecimals, currency)
|
|
}
|
|
return fmt.Sprintf("%s%d.%02d", currency, beforeDecimals, afterDecimals)
|
|
}
|
|
|
|
func MultiPartFileHeaderToBase64(fileHeader *multipart.FileHeader) (string, error) {
|
|
// Open the uploaded file
|
|
src, err := fileHeader.Open()
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to open uploaded file: %v", err)
|
|
}
|
|
defer src.Close()
|
|
|
|
// Read the file content
|
|
fileContent, err := io.ReadAll(src)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to read file content: %v", err)
|
|
}
|
|
|
|
// Encode the image to base64
|
|
base64Image := base64.StdEncoding.EncodeToString(fileContent)
|
|
|
|
// Determine the MIME type based on file extension
|
|
filename := strings.ToLower(fileHeader.Filename)
|
|
var mimeType string
|
|
if strings.HasSuffix(filename, ".jpg") || strings.HasSuffix(filename, ".jpeg") {
|
|
mimeType = "image/jpeg"
|
|
} else if strings.HasSuffix(filename, ".png") {
|
|
mimeType = "image/png"
|
|
} else if strings.HasSuffix(filename, ".gif") {
|
|
mimeType = "image/gif"
|
|
} else {
|
|
return "", fmt.Errorf("unsupported image format")
|
|
}
|
|
|
|
// Create the image data URL
|
|
return fmt.Sprintf("data:%s;base64,%s", mimeType, base64Image), nil
|
|
}
|