utils

package
v4.67.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Exposes various utility functions for strings, generation of valid filenames and random ID strings, zipping files/directories, reading/writing images

Exposes various utility functions for strings, generation of valid filenames and random ID strings, zipping files/directories, reading/writing images

Exposes various utility functions for strings, generation of valid filenames and random ID strings, zipping files/directories, reading/writing images

Exposes various utility functions for strings, generation of valid filenames and random ID strings, zipping files/directories, reading/writing images

Example (MakeSaveableFileName)
fmt.Println(MakeSaveableFileName("my roi"))
fmt.Println(MakeSaveableFileName("Dust/Alteration"))
fmt.Println(MakeSaveableFileName("I bet $100 this is cheese"))
fmt.Println(MakeSaveableFileName("10% Ca/Fe & Coffee matrix?"))
Output:
my roi
Dust Alteration
I bet  100 this is cheese
10% Ca Fe   Coffee matrix
Example (RateLimiter)
package main

import (
	"fmt"
	"time"

	"github.com/pixlise/core/v4/core/timestamper"
)

func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Hard limit")
		}
	}()

	ts := timestamper.MockTimeNowStamper{
		QueuedTimeStamps: []int64{1234567000, 1234567000, 1234567001, 1234567003, 1234567009, 12345670010, 12345670012, 12345670013, 1234567017, 1234567025, 1234567030, 1234567030, 1234567031, 1234567032},
	}
	r := MakeRateLimiter(&ts, 3, 5, 10, 5)

	runLoop(r, len(ts.QueuedTimeStamps))

}

func runLoop(r *RateLimiter, iterations int) {
	for c := 0; c < iterations; c++ {
		startMs := time.Now().UnixMilli()
		r.CheckRateLimit()
		endMs := time.Now().UnixMilli()

		if endMs-startMs > 4000 {
			fmt.Println("Soft limit")
		} else {
			fmt.Println("No limit")
		}
	}
}
Output:
No limit
No limit
No limit
Rate limiting 5 sec...
Soft limit
Rate limiting 5 sec...
Soft limit
No limit
No limit
No limit
Rate limiting 5 sec...
Soft limit
Rate limiting 5 sec...
Soft limit
Rate limiting 5 sec...
Soft limit
Hard limit

Index

Examples

Constants

View Source
const PrettyPrintIndentForJSON = "  "

PrettyPrintIndentForJSON Pretty-print indenting of JSON

View Source
const RandomStringChars = "abcdefghijklmnopqrstuvwxyz1234567890"

Random string generation https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go

Variables

This section is empty.

Functions

func Abs

func Abs[T constraints.Integer](val T) T

func AddFilesToZip

func AddFilesToZip(w *zip.Writer, basePath, baseInZip string) error

func AddItemsToSet

func AddItemsToSet[K comparable](keys []K, theSet map[K]bool)

func ConvertIntSlice

func ConvertIntSlice[T constraints.Integer, F constraints.Integer](from []F) []T

func FilesEqual

func FilesEqual(aPath, bPath string) error

func FixUserId

func FixUserId(userId string) string

func GetDiskAvailableBytes added in v4.38.17

func GetDiskAvailableBytes() (uint64, error)

func GetMapKeys

func GetMapKeys[K comparable, V any](theMap map[K]V) []K

func ImagesEqual

func ImagesEqual(aPath, bPath string) error

func ItemInSlice

func ItemInSlice[T comparable](a T, list []T) bool

func MakeDeterministicJSON added in v4.3.15

func MakeDeterministicJSON(b []byte, flat bool) string

func MakeSaveableFileName

func MakeSaveableFileName(name string) string

MakeSaveableFileName - Given a name which may not be acceptable as a file name, generate a string for a file name that won't have issues. This replaces bad characters like slashes with spaces, etc

func MakeScanImage

func MakeScanImage(
	imgPath string,
	fileSize uint64,
	source protos.ScanImageSource,
	purpose protos.ScanImagePurpose,
	associatedScanIds []string,
	originScanId string,
	originImageURL string,
	matchInfo *protos.ImageMatchTransform,
	pyramidId string,
	PyramidFormat string,
	width uint32,
	height uint32) *protos.ScanImage

func RandPassword added in v4.42.1

func RandPassword(length int) (string, error)

func RandStringBytesMaskImpr

func RandStringBytesMaskImpr(n int) string

func ReadFileLines

func ReadFileLines(filePath string) ([]string, error)

ReadFileLines - Reads all lines in a file into a string array

func ReadImageDimensions added in v4.3.13

func ReadImageDimensions(imageName string, imgBytes []byte) (uint32, uint32, error)

Returns width, height and error. Try it with the Go libraries, if it fails, read it with libvips

func RemoveItemFromSlice

func RemoveItemFromSlice[T comparable](a T, list []T) []T

func SlicesEqual

func SlicesEqual[T comparable](listA []T, listB []T) bool

func UnzipDirectory

func UnzipDirectory(src string, dest string, flattenPaths bool) ([]string, error)

func WritePNGImageFile

func WritePNGImageFile(pathPrefix string, img image.Image) error

func ZipDirectory

func ZipDirectory(dirPath string) ([]byte, error)

ZipDirectory - zips a whole directory and its contents (NOT recursive!) See: https://golang.org/pkg/archive/zip/#example_Writer

Types

type RateLimiter added in v4.54.1

type RateLimiter struct {
	// contains filtered or unexported fields
}

func MakeRateLimiter added in v4.54.1

func MakeRateLimiter(timestamper timestamper.ITimeStamper, softLimit int, hardLimit int, timeWindowSec int64, softLimitSleepSec int) *RateLimiter

func (*RateLimiter) CheckRateLimit added in v4.54.1

func (r *RateLimiter) CheckRateLimit()

Jump to

Keyboard shortcuts

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