utils

package
v0.14.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2024 License: AGPL-3.0 Imports: 29 Imported by: 0

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

View Source
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

func ApplyExifOrientation(img image.Image, isLandscape bool, exifOrientation string) image.Image

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

func BytesToBase64(imgBytes []byte) (string, error)

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

func BytesToImage(imgBytes []byte) (image.Image, error)

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

func CalculateSignature(secret, data string) string

CalculateSignature generates an HMAC-SHA256 signature for the given secret and timestamp

func ColorizeRequestId

func ColorizeRequestId(requestID string) string

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

func CombineQueries(urlQueries url.Values, refererURL string) (url.Values, error)

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

func CreateQrCode(link string) string

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

func DateToJavascriptLayout(input string) string

DateToJavascriptLayout converts a date format string from Go layout to JavaScript format

func DateToLayout

func DateToLayout(input string) string

DateToLayout takes a string and replaces normal date layouts to GO layouts

func FileExists

func FileExists(filename string) bool

FileExists checks if a file exists at the specified path and returns true if it does

func GenerateSharedSecret

func GenerateSharedSecret() (string, error)

GenerateSharedSecret generates a random 256-bit (32-byte) secret and returns it as a hex string.

func GenerateUUID

func GenerateUUID() string

GenerateUUID generates a new random UUID string

func GetImageMimeType

func GetImageMimeType(r io.Reader) string

GetImageMimeType returns the MIME type (gif/jpeg/png/webp) for an image reader

func ImageToBase64

func ImageToBase64(img image.Image) (string, error)

ImageToBase64 converts an image.Image to a base64 encoded data URI string with appropriate MIME type

func ImageToBytes

func ImageToBytes(img image.Image) ([]byte, error)

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

func IsSleepTime(sleepStartTime, sleepEndTime string, currentTime time.Time) (bool, error)

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

func IsValidSignature(receivedSignature, calculatedSignature string) bool

IsValidSignature performs a constant-time comparison of two signatures to prevent timing attacks

func IsValidTimestamp

func IsValidTimestamp(receivedTimestamp string, toleranceSeconds int) bool

IsValidTimestamp validates if a timestamp is within the acceptable tolerance window

func OptimizeImage

func OptimizeImage(img image.Image, width, height int) (image.Image, error)

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

type Color struct {
	R   int
	G   int
	B   int
	RGB string
	Hex string
}

Color represents an RGB color with string representations

func StringToColor

func StringToColor(inputString string) Color

StringToColor takes any string and returns a Color struct with deterministic RGB values. Identical input strings will always return identical color values.

type WeightedAsset

type WeightedAsset struct {
	Type string
	ID   string
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL