util

package
v0.9.11 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 43 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// MimeSniffLen sniff Length, use for detect file mime type
	MimeSniffLen = 512
)

ReaderMimeType get the io.Reader mimeType

Usage:

file, err := os.Open(filepath)
if err != nil {
	return
}
mime := ReaderMimeType(file)

Variables

View Source
var (
	DefaultDirPerm   os.FileMode = 0775
	DefaultFilePerm  os.FileMode = 0665
	OnlyReadFilePerm os.FileMode = 0444
)

perm for create dir or file

View Source
var (
	// DefaultFileFlags for create and write
	DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND
	// OnlyReadFileFlags open file for read
	OnlyReadFileFlags = os.O_RDONLY
)
View Source
var (
	DirExist  = IsDir
	FileExist = IsFile
	PathExist = PathExists
)

alias methods

View Source
var ImageMimeTypes = map[string]string{
	"bmp": "image/bmp",
	"gif": "image/gif",
	"ief": "image/ief",
	"jpg": "image/jpeg",

	"jpeg": "image/jpeg",
	"png":  "image/png",
	"svg":  "image/svg+xml",
	"ico":  "image/x-icon",
	"webp": "image/webp",
}

ImageMimeTypes refer net/http package

Functions

func CheckPort

func CheckPort(port int) bool

CheckPort 检测端口是否可用

func ChickIsDir

func ChickIsDir(path string) bool

ChickIsDir 判断所给路径是否为文件夹

func Compare

func Compare(a, b string) bool

Compare returns true if the first string precedes the second one according to natural order

func Decode

func Decode(in []byte, charset string) ([]byte, error)

func DecodeFileName

func DecodeFileName(headerName string, ZipFilenameEncoding string) string

func DeleteFileIfExist

func DeleteFileIfExist(filePath string) error

删除文件

func DetectUTF8

func DetectUTF8(s string) (valid, require bool)

DetectUTF8 检测 s 是否为有效的 UTF-8 字符串,以及该字符串是否必须被视为 UTF-8 编码(即,不兼容CP-437、ASCII 或任何其他常见编码)。 来自: go\src\archive\zip\reader.go

func FileExists

func FileExists(path string) bool

FileExists reports whether the named file or directory exists.

func GbkToUtf8

func GbkToUtf8(unknowString string) (string, error)

func GenerateImage

func GenerateImage(text string) ([]byte, error)

GenerateImage 创建一个带有动态文字的JPEG图片,并返回其[]byte形式

func GetAbsPath

func GetAbsPath(path string) string

GetAbsPath 获取绝对路径

func GetAuthor

func GetAuthor(input string) string

func GetContentTypeByFileName

func GetContentTypeByFileName(fileName string) (contentType string)

GetContentTypeByFileName https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types

func GetEncoding

func GetEncoding(charset string) (encoding.Encoding, bool)

func GetFreePort

func GetFreePort() (int, error)

GetFreePort 获取一个空闲可用的端口号

func GetIPList

func GetIPList() (IPList []string, err error)

GetIPList 获取本机IP列表

func GetImageDataBlurHash

func GetImageDataBlurHash(loadedImage []byte, components int) string

GetImageDataBlurHash 获取图片的BlurHash

func GetImageDataBlurHashImage

func GetImageDataBlurHashImage(loadedImage []byte, components int) []byte

GetImageDataBlurHashImage 获取图片的BlurHash图

func GetMainName

func GetMainName(filename string) string

GetMainName 取得无后缀的文件名

func GetOutboundIP

func GetOutboundIP() net.IP

GetOutboundIP 获取本机的首选出站IP Get preferred outbound ip of this machine

func GuessText

func GuessText(unknowString string) (string, error)

func ImageAutoCrop

func ImageAutoCrop(loadedImage []byte, energyThreshold float32) []byte

ImageAutoCrop 自动裁白边

func ImageGray

func ImageGray(loadedImage []byte) []byte

ImageGray 转换为黑白图片

func ImageResize

func ImageResize(loadedImage []byte, width int, height int) []byte

ImageResize 重设图片分辨率

func ImageResizeByHeight

func ImageResizeByHeight(loadedImage []byte, height int) []byte

ImageResizeByHeight 根据一个固定 Height 缩放图片

func ImageResizeByMaxHeight

func ImageResizeByMaxHeight(loadedImage []byte, maxHeight int) ([]byte, error)

ImageResizeByMaxHeight 设定一个图片高度上限,大于这个高度就缩放

func ImageResizeByMaxWidth

func ImageResizeByMaxWidth(loadedImage []byte, maxWidth int) ([]byte, error)

ImageResizeByMaxWidth 设定一个图片宽度上限,大于这个宽度就缩放

func ImageResizeByWidth

func ImageResizeByWidth(loadedImage []byte, width int) []byte

ImageResizeByWidth 根据一个固定宽度缩放图片

func ImageThumbnail

func ImageThumbnail(loadedImage []byte, width int, height int) []byte

ImageThumbnail 根据设定的图片大小,剪裁图片

func IsAbsPath

func IsAbsPath(aPath string) bool

IsAbsPath is abs path.

func IsDir

func IsDir(path string) bool

IsDir reports whether the named directory exists.

func IsExist

func IsExist(path string) bool

IsExist 判断文件夹或文件是否存在

func IsFile

func IsFile(path string) bool

IsFile reports whether the named file or directory exists.

func IsImageFile

func IsImageFile(path string) bool

IsImageFile check file is image file.

func IsZipFile

func IsZipFile(filepath string) bool

IsZipFile check is zip file. from https://blog.csdn.net/wangshubo1989/article/details/71743374

func MD5file

func MD5file(fName string) string

MD5file 计算字符串MD5

func MimeType

func MimeType(path string) (mime string)

MimeType get File Mime Type name. eg "image/png"

func OpenBrowser

func OpenBrowser(uri string)

func PathExists

func PathExists(path string) bool

PathExists reports whether the named file or directory exists.

func PrintAllReaderURL

func PrintAllReaderURL(Port int, OpenBrowserFlag bool, PrintAllPossibleQRCode bool, ServerHost string, DisableLAN bool, enableTls bool, etcStr string)

PrintAllReaderURL 打印阅读链接

func PrintQRCode

func PrintQRCode(text string)

func ReaderMimeType

func ReaderMimeType(r io.Reader) (mime string)

func RemoveExtension

func RemoveExtension(filename string) string

RemoveExtension 从文件名中去除扩展名

func ShiftjisToUtf8

func ShiftjisToUtf8(unknowString string) (string, error)

func Sort

func Sort(l []string)

Sort sorts a list of strings in a natural order

func ToGBK

func ToGBK(str string) (string, error)

ToGBK Convert a string encoding from UTF-8 to ShiftJIS

func ToShiftJIS

func ToShiftJIS(str string) (string, error)

ToShiftJIS Convert a string encoding from UTF-8 to ShiftJIS

func TrackTIme

func TrackTIme(pre time.Time) time.Duration

TrackTIme 计算耗时 使用时只需要写一行:defer TrackTIme(time.Now())

Types

type ServerStatus

type ServerStatus struct {
	ServerName            string       //服务器描述
	ServerHost            string       //
	ServerPort            int          //
	NumberOfBooks         int          //当前拥有的书籍总数
	NumberOfOnLineUser    int          //TODO:在线用户数
	NumberOfOnLineDevices int          //TODO:在线设备数
	SupportUploadFile     bool         //
	ClientIP              string       //客户端IP
	OSInfo                SystemStatus //系统信息
}

ServerStatus 服务器当前状况

func GetAllServerInfo

func GetAllServerInfo(configHost string, comigoVersion string, configPort int, configEnableUpload bool, allBooksNumber int, clientIP string) *ServerStatus

func GetServerInfo

func GetServerInfo(configHost string, comigoVersion string, configPort int, configEnableUpload bool, allBooksNumber int) *ServerStatus

type SystemStatus

type SystemStatus struct {
	//CPU相关
	CPUNumLogical  int     `json:"cpu_num_logical_total"`
	CPUNumPhysical int     `json:"cpu_num_physical"`
	CPUUsedPercent float64 `json:"cpu_used_percent"`
	//内存相关
	MemoryTotal       uint64  `json:"memory_total"`
	MemoryFree        uint64  `json:"memory_free"`
	MemoryUsedPercent float64 `json:"memory_used_percent"`
	//设备描述
	Description string `json:"description"`
}

SystemStatus Documentation: https://pkg.go.dev/github.com/shirou/gopsutil 获取服务器当前状况

func GetSystemStatus

func GetSystemStatus() SystemStatus

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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