Documentation ¶
Overview ¶
Package utils provides various utility functions for the application.
Index ¶
- Constants
- Variables
- func Do(fn []func() error) error
- func FindDuplicates(hashes []*Phash, distance int, durationDiff float64) [][]int
- func GenerateETag(data []byte) string
- func GetBase64StringFromData(data []byte) string
- func GetDataFromBase64String(encodedString string) ([]byte, error)
- func GetVTTTime(fracSeconds float64) string
- func IsTrue(b *bool) bool
- func MergeMaps(dest map[string]interface{}, src map[string]interface{})
- func NotNilFields(subject interface{}, tag string) []string
- func ParseDateStringAsTime(dateString string) (time.Time, error)
- func PhashToString(phash int64) string
- func ProcessBase64Image(imageString string) ([]byte, error)
- func ProcessImageInput(ctx context.Context, imageInput string) ([]byte, error)
- func ReadImageFromURL(ctx context.Context, url string) ([]byte, error)
- func ServeImage(w http.ResponseWriter, r *http.Request, image []byte)
- func ServeStaticContent(w http.ResponseWriter, r *http.Request, data []byte)
- func ServeStaticFile(w http.ResponseWriter, r *http.Request, filepath string)
- func ServeStaticFileModTime(w http.ResponseWriter, r *http.Request, path string, modTime time.Time)
- func StrFormat(format string, m StrFormatMap) string
- func StringToPhash(s string) (int64, error)
- func StringerSliceToStringSlice[V fmt.Stringer](v []V) []string
- func Timeout(todo func(), waitTime time.Duration, onTimeout func(done chan struct{}))
- func URLFromHandle(input string, siteURL string) string
- type MutexManager
- type NestedMap
- type Phash
- type StrFormatMap
- type URLMap
- type ValueOnlyContext
Examples ¶
Constants ¶
const FirefoxLinux = "Mozilla/5.0 (X11; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0"
const FirefoxLinuxArm = "Mozilla/5.0 (X11; Linux armv7l; rv:86.0) Gecko/20100101 Firefox/86.0"
const FirefoxLinuxArm64 = "Mozilla/5.0 (X11; Linux aarch64; rv:86.0) Gecko/20100101 Firefox/86.0"
const FirefoxWindows = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
const Safari = "" /* 131-byte string literal not displayed */
valid UA from https://user-agents.net
Variables ¶
var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qbaEYvRsPZzGplT4YdXvX46iHU+8ovuvOpeipC7uquvFdpXe9nWL0gW29b+qU1Ja56Xr0gSbohWtt4QvLvR1usmcpeurCrrKXIox6Km2hGL2v27eNYkxqk/p4PbrVD8K1A/+9apu87tw2qjF66sIvevrojndSw7VTUJvE6OHkMUlJp3ouna9Ht/ppuDaTGzvI2fnHjjS7KXrqwo/6TOfIji2OqbSF0kGrysNBNr7a+eSprEfX+pcSr13J7S8yLmudBt+Gg2zoJ3bh3/X9fyr9rB/mH1NpC6Xo3fj5EgdZt+wHE97q9+K1I9uullXtTzb53TKXuggrvBSXMhvo50u3rXvbXXT3vvje3UXXj9a/H2xuzv1cfEzX/jEvTtxKfxmV18v76LGLsK8Xs95imZU3qfTr1vyXuv+83gsd42Lbd7EVI/tyy7d9F1uRyateOd72XWyHfaY2/d32f8C2q1YeTD6/7Pn1ybbvAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAL+ITrOa6fKadMcEAAAAASUVORK5CYII=")
Functions ¶
func Do ¶ added in v0.19.0
Do executes each function in the slice in order. If any function returns an error, it is returned immediately.
func FindDuplicates ¶ added in v0.7.0
func GenerateETag ¶ added in v0.21.0
Returns an MD5 hash of data, formatted for use as an HTTP ETag header. Intended for use with `http.ServeContent`, to respond to conditional requests.
func GetBase64StringFromData ¶
GetBase64StringFromData returns the given byte slice as a base64 encoded string
func GetDataFromBase64String ¶
GetDataFromBase64String returns the given base64 encoded string as a byte slice
func GetVTTTime ¶
GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm)
func MergeMaps ¶ added in v0.26.0
MergeMaps merges src into dest. If a key exists in both maps, the value from src is used.
func NotNilFields ¶ added in v0.11.0
NotNilFields returns the matching tag values of fields from an object that are not nil. Panics if the provided object is not a struct.
func PhashToString ¶ added in v0.7.0
func ProcessBase64Image ¶
ProcessBase64Image transforms a base64 encoded string from a form post and returns the image itself as a byte slice.
func ProcessImageInput ¶ added in v0.6.0
ProcessImageInput transforms an image string either from a base64 encoded string, or from a URL, and returns the image as a byte slice
func ReadImageFromURL ¶ added in v0.6.0
ReadImageFromURL returns image data from a URL
func ServeImage ¶ added in v0.3.0
func ServeImage(w http.ResponseWriter, r *http.Request, image []byte)
func ServeStaticContent ¶ added in v0.21.0
func ServeStaticContent(w http.ResponseWriter, r *http.Request, data []byte)
Serves static content, adding Cache-Control: no-cache and a generated ETag header. Responds to conditional requests using the ETag.
func ServeStaticFile ¶ added in v0.21.0
func ServeStaticFile(w http.ResponseWriter, r *http.Request, filepath string)
Serves static content at filepath, adding Cache-Control: no-cache. Responds to conditional requests using the file modtime.
func ServeStaticFileModTime ¶ added in v0.27.0
ServeStaticFileModTime serves a static file at the given path using the given modTime instead of the file modTime.
func StrFormat ¶ added in v0.8.0
func StrFormat(format string, m StrFormatMap) string
StrFormat formats the provided format string, replacing placeholders in the form of "{fieldName}" with the values in the provided StrFormatMap.
For example,
StrFormat("{foo} bar {baz}", StrFormatMap{ "foo": "bar", "baz": "abc", })
would return: "bar bar abc"
Example ¶
fmt.Println(StrFormat("{foo} bar {baz}", StrFormatMap{ "foo": "bar", "baz": "abc", }))
Output: bar bar abc
func StringToPhash ¶ added in v0.8.0
func StringerSliceToStringSlice ¶ added in v0.21.0
StringerSliceToStringSlice converts a slice of fmt.Stringers to a slice of strings.
func Timeout ¶ added in v0.7.0
Timeout executes the provided todo function, and waits for it to return. If the function does not return before the waitTime duration is elapsed, then onTimeout is executed, passing a channel that will be closed when the function returns.
func URLFromHandle ¶ added in v0.27.0
URLFromHandle adds the site URL to the input if the input is not already a URL siteURL must not end with a slash
Types ¶
type MutexManager ¶ added in v0.11.0
type MutexManager struct {
// contains filtered or unexported fields
}
MutexManager manages access to mutexes using a mutex type and key.
func NewMutexManager ¶ added in v0.11.0
func NewMutexManager() *MutexManager
NewMutexManager returns a new instance of MutexManager.
func (*MutexManager) Claim ¶ added in v0.11.0
func (csm *MutexManager) Claim(mutexType string, key string, done <-chan struct{})
Claim blocks until the mutex for the mutexType and key pair is available. The mutex is then claimed by the calling code until the provided done channel is closed.
type NestedMap ¶ added in v0.26.0
type NestedMap map[string]interface{}
NestedMap is a map that supports nested keys. It is expected that the nested maps are of type map[string]interface{}
type StrFormatMap ¶ added in v0.8.0
type StrFormatMap map[string]interface{}
type ValueOnlyContext ¶ added in v0.8.0
func (ValueOnlyContext) Deadline ¶ added in v0.20.0
func (ValueOnlyContext) Deadline() (deadline time.Time, ok bool)
func (ValueOnlyContext) Done ¶ added in v0.20.0
func (ValueOnlyContext) Done() <-chan struct{}
func (ValueOnlyContext) Err ¶ added in v0.20.0
func (ValueOnlyContext) Err() error