Documentation ¶
Overview ¶
Package utils provides a collection of helper functions for various tasks.
This package includes utilities for UUID generation, date formatting, image processing, URL query manipulation, random selection, color operations, and request ID colorization. It's designed to support common operations across different parts of the application.
Index ¶
- Constants
- func ApplyExifOrientation(img image.Image, isLandscape bool, exifOrientation string) image.Image
- func BlurImage(img image.Image, isOptimized bool, clientData config.ClientData) (image.Image, error)
- func BytesToBase64(imgBytes []byte) (string, error)
- func BytesToImage(imgBytes []byte) (image.Image, error)
- func CalculateSignature(secret, data string) string
- func ColorizeRequestId(requestID string) string
- func CombineQueries(urlQueries url.Values, refererURL string) (url.Values, error)
- func CreateQrCode(link string) string
- func DateToJavascriptLayout(input string) string
- func DateToLayout(input string) string
- func FileExists(filename string) bool
- func GenerateSharedSecret() (string, error)
- func GenerateUUID() string
- func GetImageMimeType(r io.Reader) string
- func ImageToBase64(img image.Image) (string, error)
- func ImageToBytes(img image.Image) ([]byte, error)
- func IsSleepTime(sleepStartTime, sleepEndTime string, currentTime time.Time) (bool, error)
- func IsValidSignature(receivedSignature, calculatedSignature string) bool
- func IsValidTimestamp(receivedTimestamp string, toleranceSeconds int) bool
- func OptimizeImage(img image.Image, width, height int) (image.Image, error)
- func RandomItem[T any](s []T) T
- type AssetWithWeighting
- type Color
- type WeightedAsset
Constants ¶
const ( // SigmaBase is the base value for calculating Gaussian blur sigma. // This value was determined through empirical testing to provide optimal blur results. SigmaBase = 10 // SigmaConstant is used to normalise the blur effect across different image sizes. // The value 1300.0 was chosen as it provides consistent blur effects for typical screen resolutions. SigmaConstant = 1300.0 )
Variables ¶
This section is empty.
Functions ¶
func ApplyExifOrientation ¶ added in v0.14.2
ApplyExifOrientation adjusts an image's orientation based on EXIF data and desired landscape/portrait mode. It takes an image, a boolean indicating if landscape orientation is desired, and an EXIF orientation string. The EXIF orientation values follow the standard specification:
1 = Normal 2 = Flipped horizontally 3 = Rotated 180 degrees 4 = Flipped vertically 5 = Transposed (flipped horizontally and rotated 90° CW) 6 = Rotated 90° CCW 7 = Transverse (flipped horizontally and rotated 90° CCW) 8 = Rotated 90° CW
Returns the properly oriented image.
func BlurImage ¶
func BlurImage(img image.Image, isOptimized bool, clientData config.ClientData) (image.Image, error)
BlurImage applies a Gaussian blur to an image with normalized sigma based on image dimensions. It can optionally resize the image first based on client data dimensions.
func BytesToBase64 ¶
BytesToBase64 converts a byte slice to a base64 encoded string with MIME type prefix. It takes a byte slice representing an image and returns a data URI string suitable for use in HTML/CSS, such as "data:image/jpeg;base64,/9j/4AAQSkZJ...". The function detects the MIME type of the image automatically.
func BytesToImage ¶
BytesToImage converts a byte slice to an image.Image. It takes a byte slice as input and returns an image.Image and any error encountered. It handles both WebP and other common image formats (JPEG, PNG, GIF) automatically by detecting the MIME type and using the appropriate decoder.
func CalculateSignature ¶
CalculateSignature generates an HMAC-SHA256 signature for the given secret and timestamp
func ColorizeRequestId ¶
ColorizeRequestId takes a request ID string and returns a colorized string representation. It generates a color based on the input string, determines the best contrasting text color, and applies styling using lipgloss to create a visually distinct, colored representation of the request ID.
func CombineQueries ¶
CombineQueries combines URL.Query() and Referer() queries into a single url.Values. Referer query parameters will overwrite URL query parameters with the same names.
func CreateQrCode ¶
CreateQrCode generates a QR code for the given link and returns it as a base64 encoded string. Returns an empty string and logs an error if generation fails.
func DateToJavascriptLayout ¶
DateToJavascriptLayout converts a date format string from Go layout to JavaScript format
func DateToLayout ¶
DateToLayout takes a string and replaces normal date layouts to GO layouts
func FileExists ¶
FileExists checks if a file exists at the specified path and returns true if it does
func GenerateSharedSecret ¶
GenerateSharedSecret generates a random 256-bit (32-byte) secret and returns it as a hex string.
func GetImageMimeType ¶
GetImageMimeType returns the MIME type (gif/jpeg/png/webp) for an image reader
func ImageToBase64 ¶
ImageToBase64 converts an image.Image to a base64 encoded data URI string with appropriate MIME type
func ImageToBytes ¶
ImageToBytes converts an image.Image to a byte slice in JPEG format. It takes an image.Image as input and returns the encoded bytes and any error encountered. The bytes can be used for further processing, transmission, or storage.
func IsSleepTime ¶
IsSleepTime checks if the current time falls within a sleep period defined by start and end times. It handles periods that cross midnight by adjusting the times accordingly.
func IsValidSignature ¶
IsValidSignature performs a constant-time comparison of two signatures to prevent timing attacks
func IsValidTimestamp ¶
IsValidTimestamp validates if a timestamp is within the acceptable tolerance window
func OptimizeImage ¶
OptimizeImage resizes an image to the specified dimensions while maintaining aspect ratio. If width or height is 0, the image is returned unmodified.
func RandomItem ¶
func RandomItem[T any](s []T) T
RandomItem returns a random item from the given slice. Returns the zero value of type T if the slice is empty.
Types ¶
type AssetWithWeighting ¶
type AssetWithWeighting struct { Asset WeightedAsset Weight int }
AssetWithWeighting represents a WeightedAsset with an associated weight value
type Color ¶
Color represents an RGB color with string representations
func StringToColor ¶
StringToColor takes any string and returns a Color struct with deterministic RGB values. Identical input strings will always return identical color values.
type WeightedAsset ¶
WeightedAsset represents an asset with a type and ID
func PickRandomImageType ¶
func PickRandomImageType(useWeighting bool, peopleAndAlbums []AssetWithWeighting) WeightedAsset
PickRandomImageType selects a random image type based on the given configuration and weightings. It returns a WeightedAsset representing the picked image type.
func WeightedRandomItem ¶
func WeightedRandomItem(assets []AssetWithWeighting) WeightedAsset
WeightedRandomItem selects a random asset from the given slice of WeightedAsset(s) based on their logarithmic weights. It uses a weighted random selection algorithm.