utils

package
v0.0.0-...-b1da578 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

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 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 ComparePassword

func ComparePassword(hash, password string) error

ComparePassword 比较密码

func CopyWithBuffer

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

func CopyWithBufferN

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

func CreateNestedDirectory(path string) error

CreateNestedDirectory create nested directory

func CreateNestedFile

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

CreateNestedFile create nested file

func EncodePath

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

func EncryptPassword

func EncryptPassword(password string) (string, error)

EncryptPassword 加密密码

func Ext

func Ext(path string) string

func FixAndCleanPath

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

func GetActualMountPath(mountPath string) string

GetActualMountPath remove balance suffix

func GetFullPath

func GetFullPath(mountPath, path string) string

func GetMD5EncodeStr

func GetMD5EncodeStr(data string) string

func GetNoneEmpty

func GetNoneEmpty(strArr ...string) string

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

func GetStorageUsage

func GetStorageUsage(path string) (int64, error)

GetStorageUsage 获取存储使用情况

func HashData

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

HashData get hash of one hashType

func HashFile

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

HashFile get hash of one hashType from a model.File

func HashReader

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

HashReader get hash of one hashType from a reader

func IsBalance

func IsBalance(str string) bool

func IsExist

func IsExist(path string) bool

IsExist 判断文件是否存在

func IsImage

func IsImage(file *multipart.FileHeader) bool

IsImage 判断文件是否为图片

func IsSubPath

func IsSubPath(path string, subPath string) bool

func JoinBasePath

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

func LimitWriter

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

func MappingName

func MappingName(name string) string

func Max

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

func MergeErrors

func MergeErrors(errs ...error) error

func Min

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

func MustSliceConvert

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

func NewLimitReadCloser

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

func NewReadCloser

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

func PathAddSeparatorSuffix

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 RemoveAll

func RemoveAll(path string) error

RemoveAll 删除文件夹

func RemoveFile

func RemoveFile(path string) error

RemoveFile 删除文件

func Retry

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

func SafeAtob

func SafeAtob(data string) (string, error)

func ScanDir

func ScanDir(path string) []string

ScanDir 扫描目录下的所有文件

func SliceAllContains

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

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

func SliceMeet

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

func SliceReplace

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

func TrimSpace

func TrimSpace(str string) (string, error)

TrimSpace 去除字符串两端的空格,返回去除空格后的字符串,同时不能出现完全是空格的字符串,如果处理之后长度为0则抛出错误

func WriteJsonToFile

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

WriteJsonToFile 将数据写入json文件

Types

type CloseFunc

type CloseFunc func() error

func (CloseFunc) Close

func (c CloseFunc) Close() error

type Closers

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

func EmptyClosers

func EmptyClosers() Closers

func NewClosers

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

func (*Closers) Add

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

func (*Closers) AddClosers

func (c *Closers) AddClosers(closers Closers)

func (*Closers) Close

func (c *Closers) Close() error

func (*Closers) GetClosers

func (c *Closers) GetClosers() Closers

type ClosersIF

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

type HashInfo

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

A HashInfo contains hash string for one or more hashType

func FromString

func FromString(str string) HashInfo

func NewHashInfo

func NewHashInfo(ht *HashType, str string) HashInfo

func NewHashInfoByMap

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

func (HashInfo) Export

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

func (HashInfo) GetHash

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

func (HashInfo) String

func (hi HashInfo) String() string

type HashType

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

HashType indicates a standard hashing algorithm

func RegisterHash

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

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

func (*HashType) MarshalJSON

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

func (*HashType) MarshalText

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

type MultiHasher

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

A MultiHasher will construct various hashes on all incoming writes.

func NewMultiHasher

func NewMultiHasher(types []*HashType) *MultiHasher

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

func (*MultiHasher) GetHashInfo

func (m *MultiHasher) GetHashInfo() *HashInfo

func (*MultiHasher) Size

func (m *MultiHasher) Size() int64

Size returns the number of bytes written

func (*MultiHasher) Sum

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

Sum returns the specified hash from the multihasher

func (*MultiHasher) Write

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

type MultiReadable

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

func NewMultiReadable

func NewMultiReadable(reader io.Reader) *MultiReadable

func (*MultiReadable) Close

func (mr *MultiReadable) Close() error

func (*MultiReadable) Read

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

func (*MultiReadable) Reset

func (mr *MultiReadable) Reset() error

type ReadCloser

type ReadCloser struct {
	io.Reader
	io.Closer
}

Jump to

Keyboard shortcuts

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