utils

package
v3.37.4 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: AGPL-3.0 Imports: 31 Imported by: 11

Documentation

Index

Constants

View Source
const (
	KB = 1 << (10 * (iota + 1))
	MB
	GB
	TB
)

Variables

View Source
var (
	// MD5 indicates MD5 support
	MD5 = RegisterHash("md5", "MD5", 32, md5.New)

	// SHA1 indicates SHA-1 support
	SHA1 = RegisterHash("sha1", "SHA-1", 40, sha1.New)

	// SHA256 indicates SHA-256 support
	SHA256 = RegisterHash("sha256", "SHA-256", 64, sha256.New)
)
View Source
var CNLoc = time.FixedZone("UTC", 8*60*60)
View Source
var DEC = map[string]string{
	"-": "+",
	"_": "/",
	".": "=",
}
View Source
var ErrUnsupported = errors.New("hash type not supported")

ErrUnsupported should be returned by filesystem, if it is requested to deliver an unsupported hash type.

View Source
var IoBuffPool = &sync.Pool{
	New: func() interface{} {
		return make([]byte, 32*1024*2)
	},
}
View Source
var Log = log.New()
View Source
var (
	Supported []*HashType
)

Functions

func ClientIP

func ClientIP(r *http.Request) string

func CopyDir

func CopyDir(src, dst string) error

CopyDir Dir copies a whole directory recursively

func CopyFile

func CopyFile(src, dst string) error

CopyFile File copies a single file from src to dst

func CopyWithBuffer added in v3.34.0

func CopyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error)

func CopyWithBufferN added in v3.34.0

func CopyWithBufferN(dst io.Writer, src io.Reader, n int64) (written int64, err error)

func CopyWithCtx

func CopyWithCtx(ctx context.Context, out io.Writer, in io.Reader, size int64, progress func(percentage float64)) error

CopyWithCtx slightly modified function signature: - context has been added in order to propagate cancellation - I do not return the number of bytes written, has it is not useful in my use case

func CreateNestedDirectory added in v3.11.0

func CreateNestedDirectory(path string) error

CreateNestedDirectory create nested directory

func CreateNestedFile

func CreateNestedFile(path string) (*os.File, error)

CreateNestedFile create nested file

func CreateTempFile

func CreateTempFile(r io.Reader, size int64) (*os.File, error)

CreateTempFile create temp file from io.ReadCloser, and seek to 0

func EncodePath

func EncodePath(path string, all ...bool) string

func Exists

func Exists(name string) bool

Exists determine whether the file exists

func Ext

func Ext(path string) string

func FixAndCleanPath added in v3.7.1

func FixAndCleanPath(path string) string

FixAndCleanPath The upper layer of the root directory is still the root directory. So ".." And "." will be cleared for example 1. ".." or "." => "/" 2. "../..." or "./..." => "/..." 3. "../.x." or "./.x." => "/.x." 4. "x//\\y" = > "/z/x"

func GetActualMountPath added in v3.7.1

func GetActualMountPath(mountPath string) string

GetActualMountPath remove balance suffix

func GetFileType

func GetFileType(filename string) int

GetFileType get file type

func GetFullPath added in v3.9.0

func GetFullPath(mountPath, path string) string

func GetMD5EncodeStr added in v3.22.0

func GetMD5EncodeStr(data string) string

func GetMimeType added in v3.2.0

func GetMimeType(name string) string

func GetNoneEmpty added in v3.27.0

func GetNoneEmpty(strArr ...string) string

GetNoneEmpty returns the first non-empty string, return empty if all empty

func GetObjType added in v3.6.0

func GetObjType(filename string, isDir bool) int

func HashData added in v3.27.0

func HashData(hashType *HashType, data []byte, params ...any) string

HashData get hash of one hashType

func HashFile added in v3.27.0

func HashFile(hashType *HashType, file io.ReadSeeker, params ...any) (string, error)

HashFile get hash of one hashType from a model.File

func HashReader added in v3.27.0

func HashReader(hashType *HashType, reader io.Reader, params ...any) (string, error)

HashReader get hash of one hashType from a reader

func InjectQuery added in v3.12.0

func InjectQuery(raw string, query url.Values) (string, error)

func IsBalance

func IsBalance(str string) bool

func IsBool added in v3.7.1

func IsBool(bs ...bool) bool

func IsCanceled

func IsCanceled(ctx context.Context) bool

func IsEmailFormat added in v3.4.0

func IsEmailFormat(email string) bool

func IsLocalIP

func IsLocalIP(ip net.IP) bool

func IsLocalIPAddr

func IsLocalIPAddr(ip string) bool

func IsSubPath added in v3.7.1

func IsSubPath(path string, subPath string) bool

func JoinBasePath added in v3.6.0

func JoinBasePath(basePath, reqPath string) (string, error)

func LimitWriter

func LimitWriter(w io.Writer, limit int64) io.Writer

func MappingName added in v3.5.1

func MappingName(name string) string

func Max added in v3.27.0

func Max[T constraints.Ordered](a, b T) T

func MergeErrors added in v3.7.1

func MergeErrors(errs ...error) error

func MergeMap

func MergeMap(mObj ...map[string]interface{}) map[string]interface{}

func Min added in v3.27.0

func Min[T constraints.Ordered](a, b T) T

func MustParseCNTime added in v3.4.0

func MustParseCNTime(str string) time.Time

func MustSliceConvert added in v3.6.0

func MustSliceConvert[S any, D any](srcS []S, convert func(src S) D) []D

func NewDebounce added in v3.9.0

func NewDebounce(interval time.Duration) func(f func())

func NewDebounce2 added in v3.9.0

func NewDebounce2(interval time.Duration, f func()) func()

func NewLimitReadCloser added in v3.12.0

func NewLimitReadCloser(reader io.Reader, close CloseFunc, limit int64) io.ReadCloser

func NewReadCloser added in v3.12.0

func NewReadCloser(reader io.Reader, close CloseFunc) io.ReadCloser

func NewThrottle added in v3.34.0

func NewThrottle(interval time.Duration) func(func())

func NewThrottle2 added in v3.34.0

func NewThrottle2(interval time.Duration, fn func()) func()

func PathAddSeparatorSuffix added in v3.7.1

func PathAddSeparatorSuffix(path string) string

PathAddSeparatorSuffix Add path '/' suffix for example /root => /root/

func PathEqual

func PathEqual(path1, path2 string) bool

PathEqual judge path is equal

func Retry added in v3.25.0

func Retry(attempts int, sleep time.Duration, f func() error) (err error)

func SafeAtob

func SafeAtob(data string) (string, error)

func SliceAllContains added in v3.31.0

func SliceAllContains[T comparable](arr []T, vs ...T) bool

SliceAllContains check if slice all contains elements

func SliceContains

func SliceContains[T comparable](arr []T, v T) bool

SliceContains check if slice contains element

func SliceConvert

func SliceConvert[S any, D any](srcS []S, convert func(src S) (D, error)) ([]D, error)

SliceConvert convert slice to another type slice

func SliceEqual

func SliceEqual[T comparable](a, b []T) bool

SliceEqual check if two slices are equal

func SliceFilter added in v3.26.0

func SliceFilter[T any](arr []T, filter func(src T) bool) []T

func SliceMeet added in v3.20.0

func SliceMeet[T1, T2 any](arr []T1, v T2, meet func(item T1, v T2) bool) bool

func SliceReplace added in v3.31.0

func SliceReplace[T any](arr []T, replace func(src T) T)

func SymlinkOrCopyFile added in v3.11.0

func SymlinkOrCopyFile(src, dst string) error

SymlinkOrCopyFile symlinks a file or copy if symlink failed

func TokenSource added in v3.36.0

func TokenSource(fn func() (*oauth2.Token, error)) oauth2.TokenSource

func WriteJsonToFile

func WriteJsonToFile(dst string, data interface{}, std ...bool) bool

WriteJsonToFile write struct to json file

Types

type CloseFunc added in v3.12.0

type CloseFunc func() error

func (CloseFunc) Close added in v3.12.0

func (c CloseFunc) Close() error

type Closers added in v3.25.0

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

func EmptyClosers added in v3.27.0

func EmptyClosers() Closers

func NewClosers added in v3.25.0

func NewClosers(c ...io.Closer) Closers

func (*Closers) Add added in v3.25.0

func (c *Closers) Add(closer io.Closer)

func (*Closers) AddClosers added in v3.27.0

func (c *Closers) AddClosers(closers Closers)

func (*Closers) Close added in v3.25.0

func (c *Closers) Close() error

func (*Closers) GetClosers added in v3.27.0

func (c *Closers) GetClosers() Closers

type ClosersIF added in v3.27.0

type ClosersIF interface {
	io.Closer
	Add(closer io.Closer)
	AddClosers(closers Closers)
	GetClosers() Closers
}

type HashInfo added in v3.27.0

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

A HashInfo contains hash string for one or more hashType

func FromString added in v3.27.0

func FromString(str string) HashInfo

func NewHashInfo added in v3.27.0

func NewHashInfo(ht *HashType, str string) HashInfo

func NewHashInfoByMap added in v3.29.0

func NewHashInfoByMap(h map[*HashType]string) HashInfo

func (HashInfo) Export added in v3.28.0

func (hi HashInfo) Export() map[*HashType]string

func (HashInfo) GetHash added in v3.27.0

func (hi HashInfo) GetHash(ht *HashType) string

func (HashInfo) String added in v3.27.0

func (hi HashInfo) String() string

type HashType added in v3.27.0

type HashType struct {
	Width   int
	Name    string
	Alias   string
	NewFunc func(...any) hash.Hash
}

HashType indicates a standard hashing algorithm

func RegisterHash added in v3.27.0

func RegisterHash(name, alias string, width int, newFunc func() hash.Hash) *HashType

RegisterHash adds a new Hash to the list and returns its Type

func RegisterHashWithParam added in v3.28.0

func RegisterHashWithParam(name, alias string, width int, newFunc func(...any) hash.Hash) *HashType

func (*HashType) MarshalJSON added in v3.27.0

func (ht *HashType) MarshalJSON() ([]byte, error)

func (*HashType) MarshalText added in v3.27.0

func (ht *HashType) MarshalText() (text []byte, err error)

type MultiHasher added in v3.27.0

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

A MultiHasher will construct various hashes on all incoming writes.

func NewMultiHasher added in v3.27.0

func NewMultiHasher(types []*HashType) *MultiHasher

NewMultiHasher will return a hash writer that will write the requested hash types.

func (*MultiHasher) GetHashInfo added in v3.27.0

func (m *MultiHasher) GetHashInfo() *HashInfo

func (*MultiHasher) Size added in v3.27.0

func (m *MultiHasher) Size() int64

Size returns the number of bytes written

func (*MultiHasher) Sum added in v3.27.0

func (m *MultiHasher) Sum(hashType *HashType) ([]byte, error)

Sum returns the specified hash from the multihasher

func (*MultiHasher) Write added in v3.27.0

func (m *MultiHasher) Write(p []byte) (n int, err error)

type MultiReadable added in v3.15.1

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

func NewMultiReadable added in v3.15.1

func NewMultiReadable(reader io.Reader) *MultiReadable

func (*MultiReadable) Close added in v3.15.1

func (mr *MultiReadable) Close() error

func (*MultiReadable) Read added in v3.15.1

func (mr *MultiReadable) Read(p []byte) (int, error)

func (*MultiReadable) Reset added in v3.15.1

func (mr *MultiReadable) Reset() error

type ReadCloser added in v3.12.0

type ReadCloser struct {
	io.Reader
	io.Closer
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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