tools

package
v1.2.30 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: MIT Imports: 43 Imported by: 0

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 = 0o775
	DefaultFilePerm  os.FileMode = 0o665
	OnlyReadFilePerm os.FileMode = 0o444
)

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",
	"jpe":  "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 CheckIsDir added in v1.2.30

func CheckIsDir(path string) bool

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

func CheckPort

func CheckPort(port uint16) bool

CheckPort 检测端口是否可用

func ChickIsDir deprecated

func ChickIsDir(path string) bool

ChickIsDir 保留旧拼写以兼容历史调用;新代码请使用 CheckIsDir。

Deprecated: use CheckIsDir.

func CleanupSingleInstance added in v1.1.5

func CleanupSingleInstance()

CleanupSingleInstance 清理单实例资源(在程序退出时调用)

func Compare

func Compare(a, b string) bool

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

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 EnsureSingleInstance added in v1.1.5

func EnsureSingleInstance(args []string, onNewArgs func(args []string) error) (bool, error)

EnsureSingleInstance 确保程序以单实例模式运行 如果已有实例运行,将 args 发送给已运行的实例并返回 false 如果是第一个实例,返回 true

func FileExists

func FileExists(path string) bool

FileExists reports whether the named file or directory exists.

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 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 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 IsLocalStorePath added in v1.2.30

func IsLocalStorePath(storeURL string) bool

IsLocalStorePath 判断字符串是否已经是操作系统本地路径。

func IsRemoteStoreURL added in v1.2.30

func IsRemoteStoreURL(storeURL string) bool

IsRemoteStoreURL 判断 storeURL 是否为受支持的远程书库 URL。

func IsSubPath added in v1.2.30

func IsSubPath(parent, child string) bool

IsSubPath 判断 child 是否位于 parent 目录内部。

func IsValidDomain added in v1.1.5

func IsValidDomain(host string) bool

func IsValidPath

func IsValidPath(path string) bool

验证路径是否有效

func IsZipFile

func IsZipFile(filepath string) bool

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

func LookupOutboundIP added in v1.2.30

func LookupOutboundIP() (net.IP, error)

LookupOutboundIP 获取本机的首选出站 IP,并把失败交给调用方决定是否回退。

func MD5file

func MD5file(fName string) string

MD5file 计算字符串MD5

func Md5string

func Md5string(s string) string

Md5string 计算字符串的 MD5 值

func MimeType

func MimeType(path string) (mime string)

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

func NormalizeAbsPath added in v1.2.12

func NormalizeAbsPath(path string) (string, error)

NormalizeAbsPath 标准化文件路径为绝对路径 用于路径比较和冲突检测

func NormalizeAbsPathNoError added in v1.2.12

func NormalizeAbsPathNoError(path string) string

NormalizeAbsPathNoError 标准化文件路径为绝对路径(忽略错误,返回原路径) 适用于已知路径有效的场景,或者错误可以忽略的场景

func NormalizeStoreURLForCompare added in v1.2.30

func NormalizeStoreURLForCompare(storeURL string) (normalized string, remote bool, err error)

NormalizeStoreURLForCompare 统一书库路径重叠检查的比较值。

func NormalizeStoreURLKey added in v1.2.30

func NormalizeStoreURLKey(storeURL string) string

NormalizeStoreURLKey 生成 VFS 缓存 key,去掉认证信息并规范 WebDAV 等价协议。

func OpenBrowser

func OpenBrowser(isHTTPS bool, host string, port int)

func OpenBrowserByURL added in v1.1.6

func OpenBrowserByURL(uri string)

OpenBrowserByURL 打开浏览器,为了防止阻塞,需要使用go关键字调用

func OpenURL added in v1.2.21

func OpenURL(uri string) error

OpenURL 直接调用系统默认浏览器打开指定 URL,不做健康检查。

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, customTLS bool, autoTLS 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 RenderQRCodeLinesTerminal added in v1.2.23

func RenderQRCodeLinesTerminal(text string) ([]string, error)

RenderQRCodeLinesTerminal 将文本渲染为适合终端面板的二维码行内容。

说明:

  • 该实现使用 qrcode-terminal-go 生成二维码 bitmap 的终端字符表现;
  • 再将“单行模块”(█/空)按两行组合为 Unicode 半高块字符(▀▄█),从而保持与原 TUI 渲染方式接近的高度比例(避免 QR 面板过高被截断)。

func ShortName added in v1.2.30

func ShortName(title string) string

ShortName 把文件名或书名清洗成书架上展示用的短标题。

func Sort

func Sort(l []string)

Sort sorts a list of strings in a natural order

func StoreURLHost added in v1.2.30

func StoreURLHost(storeURL string) string

StoreURLHost 返回远程 URL 的主机名,解析失败或本地路径时返回空字符串。

func TrackTIme

func TrackTIme(pre time.Time) time.Duration

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

func UpdateHandler

func UpdateHandler(c echo.Context) error

UpdateHandler 提供一个 API,用于: 1. 检查当前版本与 GitHub 上最新版本号 2. 若有新版本,则执行更新 3. 返回更新结果(JSON)

func WaitUntilServerReady

func WaitUntilServerReady(host string, port uint16, timeout time.Duration)

WaitUntilServerReady 循环尝试与端口建立 TCP 连接,直到成功或超时

Types

type ConfigInterface

type ConfigInterface interface {
	GetHost() string
	GetPort() int
	GetEnableUpload() bool
}

type Message added in v1.1.5

type Message struct {
	Args []string `json:"args"`
}

Message 进程间通信消息

type ServerInfoParams

type ServerInfoParams struct {
	Cfg                   ConfigInterface
	Version               string
	AllBooksNumber        int
	ClientIP              string
	ReScanServiceEnable   bool
	ReScanServiceInterval int
}

type ServerStatus

type ServerStatus struct {
	ServerName            string       // 服务器描述
	ServerHost            string       // ServerHost 服务器主机或 IP 地址。
	ServerPort            uint16       // ServerPort 服务运行的端口号。
	TailscaleAuthURL      string       // Tailscale身份验证URL(如果适用)
	TailscaleUrl          string       // Tailscale阅读地址(如果有)
	NumberOfBooks         int          // 当前拥有的书籍总数
	SupportUploadFile     bool         // 是否支持上传文件
	ClientIP              string       // 客户端IP
	OSInfo                SystemStatus // 系统信息
	ReScanServiceEnable   bool         // 是否启用自动扫描服务
	ReScanServiceInterval int          // 自动扫描服务间隔(分钟)
}

ServerStatus 服务器当前状况

func GetServerInfo

func GetServerInfo(params ServerInfoParams) *ServerStatus

type SingleInstance added in v1.1.5

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

SingleInstance 单实例管理器

func NewSingleInstance added in v1.1.5

func NewSingleInstance(onNewArgs func(args []string) error) (*SingleInstance, error)

NewSingleInstance 创建单实例管理器

func (*SingleInstance) SendArgs added in v1.1.5

func (si *SingleInstance) SendArgs(args []string) error

SendArgs 向已运行的实例发送参数

func (*SingleInstance) StartServer added in v1.1.5

func (si *SingleInstance) StartServer() error

StartServer 启动服务器,监听来自其他实例的消息

func (*SingleInstance) Stop added in v1.1.5

func (si *SingleInstance) Stop() error

Stop 停止服务器并清理资源

func (*SingleInstance) TryLock added in v1.1.5

func (si *SingleInstance) TryLock() (bool, error)

TryLock 尝试获取锁,如果已有实例运行则返回 false

func (*SingleInstance) Unlock added in v1.1.5

func (si *SingleInstance) Unlock() error

Unlock 释放锁

type StoreBackendType added in v1.2.30

type StoreBackendType int

StoreBackendType 是跨 config/store/vfs 共用的书库后端类型。

const (
	StoreBackendLocalDisk StoreBackendType = iota
	StoreBackendSMB
	StoreBackendSFTP
	StoreBackendWebDAV
	StoreBackendS3
	StoreBackendFTP
)

type StoreURLInfo added in v1.2.30

type StoreURLInfo struct {
	URL            string
	Scheme         string
	Type           StoreBackendType
	LocalPath      string
	ServerHost     string
	ServerPort     int
	NeedAuth       bool
	AuthUsername   string
	AuthPassword   string
	RemotePath     string
	SMBShareName   string
	SMBPath        string
	S3BucketName   string
	S3ObjectPrefix string
}

StoreURLInfo 保存解析后的 store URL 信息,避免各包重复解析同一套规则。

func DetectStoreURL added in v1.2.30

func DetectStoreURL(storeURL string) StoreURLInfo

DetectStoreURL 用宽松规则识别 store URL;未知或无效 URL 按本地路径处理。

func ParseStoreURL added in v1.2.30

func ParseStoreURL(storeURL string) (StoreURLInfo, error)

ParseStoreURL 解析本地路径和远程 store URL,并对已知远程协议做严格校验。

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
Package vfs 提供虚拟文件系统抽象层,支持本地文件系统和远程存储(WebDAV、SFTP等)
Package vfs 提供虚拟文件系统抽象层,支持本地文件系统和远程存储(WebDAV、SFTP等)

Jump to

Keyboard shortcuts

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