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 ¶
- Constants
- func Abs[T constraints.Integer](val T) T
- func AddFilesToZip(w *zip.Writer, basePath, baseInZip string) error
- func AddItemsToSet[K comparable](keys []K, theSet map[K]bool)
- func ConvertIntSlice[T constraints.Integer, F constraints.Integer](from []F) []T
- func FilesEqual(aPath, bPath string) error
- func FixUserId(userId string) string
- func GetDiskAvailableBytes() (uint64, error)
- func GetMapKeys[K comparable, V any](theMap map[K]V) []K
- func ImagesEqual(aPath, bPath string) error
- func ItemInSlice[T comparable](a T, list []T) bool
- func MakeDeterministicJSON(b []byte, flat bool) string
- func MakeSaveableFileName(name string) string
- func MakeScanImage(imgPath string, fileSize uint64, source protos.ScanImageSource, ...) *protos.ScanImage
- func RandPassword(length int) (string, error)
- func RandStringBytesMaskImpr(n int) string
- func ReadFileLines(filePath string) ([]string, error)
- func ReadImageDimensions(imageName string, imgBytes []byte) (uint32, uint32, error)
- func RemoveItemFromSlice[T comparable](a T, list []T) []T
- func SendProtoBinary(w http.ResponseWriter, m protoreflect.ProtoMessage)
- func SendProtoJSON(w http.ResponseWriter, m protoreflect.ProtoMessage)
- func SlicesEqual[T comparable](listA []T, listB []T) bool
- func UnzipDirectory(src string, dest string, flattenPaths bool) ([]string, error)
- func WritePNGImageFile(pathPrefix string, img image.Image) error
- func ZipDirectory(dirPath string) ([]byte, error)
- type RateLimiter
Examples ¶
Constants ¶
const PrettyPrintIndentForJSON = " "
PrettyPrintIndentForJSON Pretty-print indenting of JSON
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 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 GetDiskAvailableBytes ¶ added in v4.38.17
func GetMapKeys ¶
func GetMapKeys[K comparable, V any](theMap map[K]V) []K
func ImagesEqual ¶
func ItemInSlice ¶
func ItemInSlice[T comparable](a T, list []T) bool
func MakeDeterministicJSON ¶ added in v4.3.15
func MakeSaveableFileName ¶
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 RandStringBytesMaskImpr ¶
func ReadFileLines ¶
ReadFileLines - Reads all lines in a file into a string array
func ReadImageDimensions ¶ added in v4.3.13
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 SendProtoBinary ¶
func SendProtoBinary(w http.ResponseWriter, m protoreflect.ProtoMessage)
func SendProtoJSON ¶
func SendProtoJSON(w http.ResponseWriter, m protoreflect.ProtoMessage)
func SlicesEqual ¶
func SlicesEqual[T comparable](listA []T, listB []T) bool
func UnzipDirectory ¶
func ZipDirectory ¶
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()