util

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: GPL-3.0 Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// MaxFilenameLength is the maximum length in bytes for a filename
	MaxFilenameLength = 100
)

Variables

This section is empty.

Functions

func BoolPtr added in v1.9.0

func BoolPtr(v bool) *bool

BoolPtr returns a pointer to a bool value.

func BuildProxyUrl added in v1.5.1

func BuildProxyUrl(scheme, host, usr, pwd string) *url.URL

BuildProxyUrl builds a proxy url with given host, username and password.

func ByteFmt

func ByteFmt(size int64) string

func CheckDuplicateAndRename

func CheckDuplicateAndRename(path string) (string, error)

CheckDuplicateAndRename rename duplicate file, add suffix (1) (2) ... if file name is a.txt, rename to a (1).txt if directory name is a, rename to a (1) return new name

func CopyDir added in v1.4.0

func CopyDir(source string, target string, excludeDir ...string) error

CopyDir Copy all files to the target directory, if the file already exists, it will be overwritten. Remove target file if the source file is not exist.

func CreateDirIfNotExist added in v1.3.5

func CreateDirIfNotExist(dir string) error

func DeepClone added in v1.5.9

func DeepClone[T any](v *T) *T

func Dir

func Dir(path string) string

func Filepath

func Filepath(path string, originName string, customName string) string

func IsExistsFile added in v1.3.8

func IsExistsFile(path string) bool

IsExistsFile check file exists and is a file

func MapToStruct

func MapToStruct(s any, v any) error

func Match added in v1.4.0

func Match(pattern string, u string) bool

Match url with pattern by chrome extension match pattern style https://developer.chrome.com/docs/extensions/mv3/match_patterns/

func ParseDataUri added in v1.3.8

func ParseDataUri(uri string) (string, []byte)

ParseDataUri parses a data URI and returns the MIME type and decode data.

func ParseSchema

func ParseSchema(url string) string

func ProxyUrlToHandler added in v1.5.7

func ProxyUrlToHandler(proxyUrl *url.URL) func(*http.Request) (*url.URL, error)

ProxyUrlToHandler gets the proxy handler from the proxy url.

func Ptr added in v1.9.0

func Ptr[T any](v T) *T

Ptr returns a pointer to the given value.

func ReplaceInvalidFilename added in v1.5.2

func ReplaceInvalidFilename(path string) string

ReplaceInvalidFilename replace invalid path characters Deprecated: Use SafeFilename instead which also handles length truncation

func ReplacePathPlaceholders added in v1.8.3

func ReplacePathPlaceholders(path string) string

ReplacePathPlaceholders replaces date placeholders in a path with actual values Supported placeholders:

  • %year% - Current year (e.g., 2025)
  • %month% - Current month (01-12)
  • %day% - Current day (01-31)
  • %date% - Full date format (2025-01-01)

func SafeFilename added in v1.9.0

func SafeFilename(filename string) string

SafeFilename sanitizes a filename by replacing invalid characters and truncating to a safe length. It performs two operations: 1. Replaces invalid path characters (platform-specific) with underscores 2. Truncates filename to MaxFilenameLength bytes while preserving the file extension The function handles UTF-8 multi-byte characters correctly by truncating at valid boundaries.

func SafeRemove

func SafeRemove(name string) error

SafeRemove remove file safely, ignoring errors if the path does not exist.

func TruncateFilename added in v1.9.0

func TruncateFilename(filename string, maxLength int) string

TruncateFilename truncates a filename to a maximum byte length while preserving the extension. Deprecated: Use SafeFilename instead which also handles invalid character replacement

func TryUrlPathUnescape added in v1.9.0

func TryUrlPathUnescape(s string) string

TryUrlPathUnescape tries to unescape a URL path-encoded string. Unlike QueryUnescape, PathUnescape does not treat '+' as a space. This is the correct function to use for decoding URL paths and filenames where %2B should decode to '+', not to a space.

If unescaping fails, it returns the original string.

func TryUrlQueryUnescape added in v1.8.1

func TryUrlQueryUnescape(s string) string

TryUrlQueryUnescape tries to unescape a URL-encoded string.

If unescaping fails, it returns the original string.

Types

type Timer

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

func NewTimer added in v1.3.1

func NewTimer(used int64) *Timer

func (*Timer) Pause

func (t *Timer) Pause()

func (*Timer) Start

func (t *Timer) Start()

func (*Timer) Used

func (t *Timer) Used() int64

Jump to

Keyboard shortcuts

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