util

package
v0.0.0-...-7d34a87 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: AGPL-3.0 Imports: 63 Imported by: 13

Documentation

Index

Constants

View Source
const (
	BazaarStatServer = "https://bazaar.b3logfile.com" // 集市包统计服务地址,七牛云,全球 CDN
	BazaarOSSServer  = "https://oss.b3logfile.com"    // 云端对象存储地址,七牛云,仅用于读取集市包,全球 CDN
)
View Source
const (
	BLOCK_BITS = 22 // Indicate that the blocksize is 4M
	BLOCK_SIZE = 1 << BLOCK_BITS
)
View Source
const (
	EvtConfPandocInitialized = "conf.pandoc.initialized"

	EvtSQLHistoryRebuild      = "sql.history.rebuild"
	EvtSQLAssetContentRebuild = "sql.assetContent.rebuild"
)
View Source
const (
	SortModeNameASC         = iota // 0:文件名字母升序
	SortModeNameDESC               // 1:文件名字母降序
	SortModeUpdatedASC             // 2:文件更新时间升序
	SortModeUpdatedDESC            // 3:文件更新时间降序
	SortModeAlphanumASC            // 4:文件名自然数升序
	SortModeAlphanumDESC           // 5:文件名自然数降序
	SortModeCustom                 // 6:自定义排序
	SortModeRefCountASC            // 7:引用数升序
	SortModeRefCountDESC           // 8:引用数降序
	SortModeCreatedASC             // 9:文件创建时间升序
	SortModeCreatedDESC            // 10:文件创建时间降序
	SortModeSizeASC                // 11:文件大小升序
	SortModeSizeDESC               // 12:文件大小降序
	SortModeSubDocCountASC         // 13:子文档数升序
	SortModeSubDocCountDESC        // 14:子文档数降序
	SortModeFileTree               // 15:使用文档树排序规则

	SortModeUnassigned = 256 // 256:未指定排序规则,按照笔记本优先于文档树获取排序规则
)
View Source
const (
	PushProgressCodeProgressed = 0 // 有进度
	PushProgressCodeEndless    = 1 // 无进度
	PushProgressCodeEnd        = 2 // 关闭进度
)
View Source
const (
	Ver       = "3.1.16"
	IsInsider = false
)
View Source
const (
	ContainerStd     = "std"     // 桌面端
	ContainerDocker  = "docker"  // Docker 容器端
	ContainerAndroid = "android" // Android 端
	ContainerIOS     = "ios"     // iOS 端
	ContainerHarmony = "harmony" // 鸿蒙端

	LocalHost = "127.0.0.1" // 伺服地址
	FixedPort = "6806"      // 固定端口
)
View Source
const DatabaseVer = "20220501"

DatabaseVer 数据库版本。修改表结构的话需要修改这里。

View Source
const (
	// SQLFlushInterval 为数据库事务队列写入间隔。
	SQLFlushInterval = 3000 * time.Millisecond
)

Variables

View Source
var (
	TesseractBin     = "tesseract"
	TesseractEnabled bool
	TesseractMaxSize = 2 * 1000 * uint64(1000)
	TesseractLangs   []string
)
View Source
var (
	NodeOCRQueue     []string
	NodeOCRQueueLock = sync.Mutex{}
)
View Source
var (
	SSL       = false
	UserAgent = "SiYuan/" + Ver
)
View Source
var (
	SiYuanAssetsImage = []string{".apng", ".ico", ".cur", ".jpg", ".jpe", ".jpeg", ".jfif", ".pjp", ".pjpeg", ".png", ".gif", ".webp", ".bmp", ".svg", ".avif"}
	SiYuanAssetsAudio = []string{".mp3", ".wav", ".ogg", ".m4a", ".flac"}
	SiYuanAssetsVideo = []string{".mov", ".weba", ".mkv", ".mp4", ".webm"}
)
View Source
var (
	Langs           = map[string]map[int]string{}
	TimeLangs       = map[string]map[string]interface{}{}
	TaskActionLangs = map[string]map[string]interface{}{}
	TrayMenuLangs   = map[string]map[string]interface{}{}
	AttrViewLangs   = map[string]map[string]interface{}{}
)
View Source
var (
	RunInContainer             = false // 是否运行在容器中
	SiyuanAccessAuthCodeBypass = false // 是否跳过空访问授权码检查
)
View Source
var (
	HomeDir, _    = gulu.OS.Home()
	WorkingDir, _ = os.Getwd()

	WorkspaceDir       string        // 工作空间目录路径
	WorkspaceName      string        // 工作空间名称
	WorkspaceLock      *flock.Flock  // 工作空间锁
	ConfDir            string        // 配置目录路径
	DataDir            string        // 数据目录路径
	RepoDir            string        // 仓库目录路径
	HistoryDir         string        // 数据历史目录路径
	TempDir            string        // 临时目录路径
	LogPath            string        // 配置目录下的日志文件 siyuan.log 路径
	DBName             = "siyuan.db" // SQLite 数据库文件名
	DBPath             string        // SQLite 数据库文件路径
	HistoryDBPath      string        // SQLite 历史数据库文件路径
	AssetContentDBPath string        // SQLite 资源文件内容数据库文件路径
	BlockTreeDBPath    string        // 区块树数据库文件路径
	AppearancePath     string        // 配置目录下的外观目录 appearance/ 路径
	ThemesPath         string        // 配置目录下的外观目录下的 themes/ 路径
	IconsPath          string        // 配置目录下的外观目录下的 icons/ 路径
	SnippetsPath       string        // 数据目录下的 snippets/ 路径

	UIProcessIDs = sync.Map{} // UI 进程 ID
)
View Source
var (
	ServerURL  *url.URL // 内核服务 URL
	ServerPort = "0"    // HTTP/WebSocket 端口,0 为使用随机端口

	ReadOnly       bool
	AccessAuthCode string
	Lang           = ""

	Container        string // docker, android, ios, harmony, std
	ISMicrosoftStore bool   // 桌面端是否是微软商店版
)
View Source
var CurrentCloudRegion = 0
View Source
var DisabledFeatures []string
View Source
var ErrPandocNotFound = errors.New("not found executable pandoc")
View Source
var (
	HttpServing = false // 是否 HTTP 伺服已经可用
)
View Source
var IsExiting = atomic.Bool{}

IsExiting 是否正在退出程序。

View Source
var IsUILoaded = false

IsUILoaded 是否已经加载了 UI。

View Source
var LocalIPs []string
View Source
var MarkdownSettings = &Markdown{
	InlineAsterisk:      true,
	InlineUnderscore:    true,
	InlineSup:           true,
	InlineSub:           true,
	InlineTag:           true,
	InlineMath:          true,
	InlineStrikethrough: true,
}

MarkdownSettings 运行时 Markdown 配置。

View Source
var MobileOSVer string

MobileOSVer 移动端操作系统版本。

View Source
var Mode = "prod"

var Mode = "dev"

View Source
var (
	PandocBinPath string // Pandoc 可执行文件路径
)
View Source
var SK = []byte("696D897C9AA0611B")
View Source
var UseSingleLineSave = true

UseSingleLineSave 是否使用单行保存 .sy 和数据库 .json 文件。

View Source
var (
	WebSocketServer = melody.New()
)
View Source
var WrongAuthCount int

Functions

func AESDecrypt

func AESDecrypt(cryptStr string) []byte

func AESEncrypt

func AESEncrypt(str string) string

func AddPushChan

func AddPushChan(session *melody.Session)

func AssetName

func AssetName(name string) string

func BlockCount

func BlockCount(fsize int64) int

func Boot

func Boot()

func BootMobile

func BootMobile(container, appDir, workspaceBaseDir, lang string)

func Broadcast

func Broadcast(msg []byte)

func BroadcastByType

func BroadcastByType(typ, cmd string, code int, msg string, data interface{})

BroadcastByType 广播所有实例上 typ 类型的会话。

func BroadcastByTypeAndApp

func BroadcastByTypeAndApp(typ, app, cmd string, code int, msg string, data interface{})

func CalSha1

func CalSha1(b []byte, r io.Reader) ([]byte, error)

func CeilSize

func CeilSize(size int64) int64

func ChatGPT

func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, maxTokens int, temperature float64, timeout int) (ret string, stop bool, err error)

func CheckFileSysStatus

func CheckFileSysStatus()

func CleanNotExistAssetsTexts

func CleanNotExistAssetsTexts()

func ClearPushProgress

func ClearPushProgress(total int)

func ClosePushChan

func ClosePushChan(id string)

func ContainsCJK

func ContainsCJK(text string) bool

func ContainsSubStr

func ContainsSubStr(s string, subStrs []string) bool

func ContextPushMsg

func ContextPushMsg(context map[string]interface{}, msg string)

func Convert2Float

func Convert2Float(s string) (float64, bool)

func ConvertPandoc

func ConvertPandoc(dir string, args ...string) (path string, err error)

func CountSessions

func CountSessions() (ret int)

func CurrentTimeMillis

func CurrentTimeMillis() int64

func CurrentTimeSecondsStr

func CurrentTimeSecondsStr() string

func DataSize

func DataSize() (dataSize, assetsSize int64)

func DisableFeature

func DisableFeature(feature string)

func EscapeHTML

func EscapeHTML(s string) (ret string)

func ExistsAssetText

func ExistsAssetText(asset string) (ret bool)

func Ext

func Ext(name string) (ret string)

func FilterFileName

func FilterFileName(name string) string

func FilterFilePath

func FilterFilePath(p string) (ret string)

func FilterMoveDocFromPaths

func FilterMoveDocFromPaths(fromPaths []string, toPath string) (ret []string)

func FilterSelfChildDocs

func FilterSelfChildDocs(paths []string) (ret []string)

func FilterUploadFileName

func FilterUploadFileName(name string) string

func GBK2UTF8

func GBK2UTF8(src []byte) (string, error)

GBK2UTF8 transform GBK byte array into UTF8 string.

func GetAbsPathInWorkspace

func GetAbsPathInWorkspace(relPath string) (string, error)

func GetAssetText

func GetAssetText(asset string) (ret string)

func GetBootProgress

func GetBootProgress() int32

func GetBootProgressDetails

func GetBootProgressDetails() (progress int32, details string)

func GetChildDocDepth

func GetChildDocDepth(treeAbsPath string) (ret int)

func GetCloudAccountServer

func GetCloudAccountServer() string

func GetCloudAssetsServer

func GetCloudAssetsServer() string

func GetCloudForumAssetsServer

func GetCloudForumAssetsServer() string

func GetCloudServer

func GetCloudServer() string

func GetCloudSyncServer

func GetCloudSyncServer() string

func GetCloudWebSocketServer

func GetCloudWebSocketServer() string

func GetDataAssetsAbsPath

func GetDataAssetsAbsPath() (ret string)

func GetDeviceID

func GetDeviceID() string

func GetDeviceName

func GetDeviceName() string

func GetDiskUsage

func GetDiskUsage(p string) (total, used, free uint64)

func GetDuplicateName

func GetDuplicateName(master string) (ret string)

func GetEtag

func GetEtag(filename string) (etag string, err error)

func GetEtagByHandle

func GetEtagByHandle(f io.Reader, size int64) (etag string, err error)

func GetFilePathsByExts

func GetFilePathsByExts(dirPath string, exts []string) (ret []string)

func GetLocalIPs

func GetLocalIPs() (ret []string)

func GetMimeTypeByExt

func GetMimeTypeByExt(filePath string) (ret string)

func GetOSPlatform

func GetOSPlatform() (plat string)

func GetOcrJsonText

func GetOcrJsonText(jsonData []map[string]interface{}) (ret string)

提取并连接所有 text 字段的函数

func GetRemoteAddr

func GetRemoteAddr(req *http.Request) string

func GetRhyResult

func GetRhyResult(force bool) (map[string]interface{}, error)

func GetTreeID

func GetTreeID(treePath string) string

func GetUniqueFilename

func GetUniqueFilename(filePath string) string

func HookUILoaded

func HookUILoaded()

func HumanizeDiffTime

func HumanizeDiffTime(a, b time.Time, lang string) string

func HumanizeRelTime

func HumanizeRelTime(a time.Time, b time.Time, lang string) string

func HumanizeTime

func HumanizeTime(then time.Time, lang string) string

func ISOWeek

func ISOWeek(date time.Time) int

ISOWeek returns the ISO 8601 year and week number in which date occurs. Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1 of year n+1.

func IncBootProgress

func IncBootProgress(progress int32, details string)

func InitPandoc

func InitPandoc()

func InitTesseract

func InitTesseract()

func InsertElem

func InsertElem[T any](s []T, index int, value T) []T

InsertElem inserts value at index into a. 0 <= index <= len(s)

func InvalidIDPattern

func InvalidIDPattern(idArg string, result *gulu.Result) bool

func IsAbsPathInWorkspace

func IsAbsPathInWorkspace(absPath string) bool

func IsAssetLinkDest

func IsAssetLinkDest(dest []byte) bool

func IsBooted

func IsBooted() bool

func IsChinaCloud

func IsChinaCloud() bool

func IsCloudDrivePath

func IsCloudDrivePath(workspaceAbsPath string) bool

func IsCorruptedSYData

func IsCorruptedSYData(data []byte) bool
func IsDirRegularOrSymlink(dir fs.DirEntry) bool

func IsDisplayableAsset

func IsDisplayableAsset(p string) bool

func IsEmptyDir

func IsEmptyDir(p string) bool

func IsLocalHost

func IsLocalHost(host string) bool

func IsLocalHostname

func IsLocalHostname(hostname string) bool

func IsLocalOrigin

func IsLocalOrigin(origin string) bool

func IsOnline

func IsOnline(checkURL string, skipTlsVerify bool, timeout int) bool

func IsPathRegularDirOrSymlinkDir

func IsPathRegularDirOrSymlinkDir(path string) bool

func IsPortOpen

func IsPortOpen(port string) bool

func IsRelativePath

func IsRelativePath(dest string) bool

func IsReservedFilename

func IsReservedFilename(baseName string) bool

func IsSubPath

func IsSubPath(absPath, toCheckPath string) bool
func IsSymlink(dir fs.DirEntry) bool

func IsSymlinkPath

func IsSymlinkPath(absPath string) bool

func IsTesseractExtractable

func IsTesseractExtractable(p string) bool

func IsValidPandocBin

func IsValidPandocBin(binPath string) bool

func IsValidURL

func IsValidURL(str string) bool

func IsWorkspaceLocked

func IsWorkspaceLocked(workspacePath string) bool

func JsonArg

func JsonArg(c *gin.Context, result *gulu.Result) (arg map[string]interface{}, ok bool)

func LastID

func LastID(p string) (name, id string)

func LinkTarget

func LinkTarget(htmlStr, linkBase string) (ret string)

func LoadAssetsTexts

func LoadAssetsTexts()

func LoadSysFonts

func LoadSysFonts() (ret []string)

func Millisecond2Time

func Millisecond2Time(t int64) time.Time

func NaturalCompare

func NaturalCompare(str1, str2 string) bool

func NeedCaptcha

func NeedCaptcha() bool

func NeedWarnDiskUsage

func NeedWarnDiskUsage(dataSize int64) bool

func NewLute

func NewLute() (ret *lute.Lute)

func NewOpenAIClient

func NewOpenAIClient(apiKey, apiProxy, apiBaseURL, apiUserAgent, apiVersion, apiProvider string) *openai.Client

func NewStdLute

func NewStdLute() (ret *lute.Lute)

func NormalizeConcurrentReqs

func NormalizeConcurrentReqs(concurrentReqs int, provider int) int

func NormalizeEndpoint

func NormalizeEndpoint(endpoint string) string

func NormalizeTimeout

func NormalizeTimeout(timeout int) int

func OcrAsset

func OcrAsset(asset string) (ret []map[string]interface{})

func Pandoc

func Pandoc(from, to, o, content string) (err error)

func ParsePort

func ParsePort(portString string) (uint16, error)

func PinYinCompare

func PinYinCompare(str1, str2 string) bool

func PushBackgroundTask

func PushBackgroundTask(data map[string]interface{})

func PushClearAllMsg

func PushClearAllMsg()

func PushClearMsg

func PushClearMsg(msgId string)

PushClearMsg 会清空指定消息。

func PushClearProgress

func PushClearProgress()

PushClearProgress 取消进度遮罩。

func PushDownloadProgress

func PushDownloadProgress(id string, percent float32)

func PushEndlessProgress

func PushEndlessProgress(msg string)

func PushErrMsg

func PushErrMsg(msg string, timeout int) (msgId string)

func PushEvent

func PushEvent(event *Result)

func PushMsg

func PushMsg(msg string, timeout int) (msgId string)

func PushNodeOCRQueue

func PushNodeOCRQueue(n *ast.Node)

func PushProgress

func PushProgress(code, current, total int, msg string)

func PushProtyleLoading

func PushProtyleLoading(rootID, msg string)

func PushReloadDoc

func PushReloadDoc(rootID string)

func PushReloadDocInfo

func PushReloadDocInfo(docInfo map[string]any)

func PushReloadEmojiConf

func PushReloadEmojiConf()

func PushReloadFiletree

func PushReloadFiletree()

func PushReloadProtyle

func PushReloadProtyle(rootID string)

func PushSaveDoc

func PushSaveDoc(rootID, typ string, sources interface{})

func PushSetDefRefCount

func PushSetDefRefCount(rootID, blockID string, refIDs []string, refCount, rootRefCount int)

func PushSetRefDynamicText

func PushSetRefDynamicText(rootID, blockID, defBlockID, refText string)

func PushStatusBar

func PushStatusBar(msg string)

func PushTxErr

func PushTxErr(msg string, code int, data interface{})

func PushUpdateIDs

func PushUpdateIDs(ids map[string]string)

func PushUpdateMsg

func PushUpdateMsg(msgId string, msg string, timeout int)

func RandString

func RandString(length int) string

func RandomSleep

func RandomSleep(minMills, maxMills int)

func ReadWorkspacePaths

func ReadWorkspacePaths() (ret []string, err error)

func ReloadUI

func ReloadUI()

func ReloadUIResetScroll

func ReloadUIResetScroll()

func RemoveAssetText

func RemoveAssetText(asset string)

func RemoveElem

func RemoveElem[T any](s []T, index int) []T

RemoveElem removes the element at index i from s.

func RemoveEmojiInvisible

func RemoveEmojiInvisible(text string) (ret string)

func RemoveID

func RemoveID(name string) string

func RemoveInvalid

func RemoveInvalid(text string) (ret string)

func RemovePushChan

func RemovePushChan(session *melody.Session)

func RemoveRedundantSpace

func RemoveRedundantSpace(str string) string

func RemoveWorkspaceSession

func RemoveWorkspaceSession(session *SessionData)

func ReplaceStr

func ReplaceStr(strs []string, old, new string) (ret []string, changed bool)

func ReportFileSysFatalError

func ReportFileSysFatalError(err error)

func Reverse

func Reverse(s string) string

func SaveAssetsTexts

func SaveAssetsTexts()

func SessionsByType

func SessionsByType(typ string) (ret []*melody.Session)

func SetAssetText

func SetAssetText(asset, text string)

func SetBootDetails

func SetBootDetails(details string)

func SetBooted

func SetBooted()

func SetNetworkProxy

func SetNetworkProxy(proxyURL string)

func ShortPathForBootingDisplay

func ShortPathForBootingDisplay(p string) string

func SizeOfDirectory

func SizeOfDirectory(path string) (size int64, err error)

func Tesseract

func Tesseract(imgAbsPath string) (ret []map[string]interface{})

func TimeFromID

func TimeFromID(id string) (ret string)

func TruncateLenFileName

func TruncateLenFileName(name string) (ret string)

func UTF82GBK

func UTF82GBK(src string) ([]byte, error)

UTF82GBK transform UTF8 rune into GBK byte array.

func UnescapeHTML

func UnescapeHTML(s string) (ret string)

func UnlockWorkspace

func UnlockWorkspace()

func ValidOptionalPort

func ValidOptionalPort(port string) bool

func WaitForTesseractInit

func WaitForTesseractInit()

func WaitForUILoaded

func WaitForUILoaded()
func WalkWithSymlinks(root string, fn fs.WalkDirFunc) error

func Weekday

func Weekday(date time.Time) int

Weekday returns the day of the week specified by date. Sunday=0, Monday=1, ..., Saturday=6.

func WeekdayCN

func WeekdayCN(date time.Time) string

WeekdayCN returns the day of the week specified by date. Sunday=日, Monday=一, ..., Saturday=六.

func WeekdayCN2

func WeekdayCN2(date time.Time) string

WeekdayCN2 returns the day of the week specified by date. Sunday=天, Monday=一, ..., Saturday=六.

func WriteWorkspacePaths

func WriteWorkspacePaths(workspacePaths []string) (err error)

Types

type AddHeaderTransport

type AddHeaderTransport struct {
	RoundTripper http.RoundTripper
	UserAgent    string
}

func (*AddHeaderTransport) RoundTrip

func (adt *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error)

type BlockStatResult

type BlockStatResult struct {
	RuneCount  int `json:"runeCount"`
	WordCount  int `json:"wordCount"`
	LinkCount  int `json:"linkCount"`
	ImageCount int `json:"imageCount"`
	RefCount   int `json:"refCount"`
	BlockCount int `json:"blockCount"`
}

type Font

type Font struct {
	Path   string
	Family string
}

type Markdown

type Markdown struct {
	InlineAsterisk      bool `json:"inlineAsterisk"`      // 是否启用行级 * 语法
	InlineUnderscore    bool `json:"inlineUnderscore"`    // 是否启用行级 _ 语法
	InlineSup           bool `json:"inlineSup"`           // 是否启用行级上标
	InlineSub           bool `json:"inlineSub"`           // 是否启用行级下标
	InlineTag           bool `json:"inlineTag"`           // 是否启用行级标签
	InlineMath          bool `json:"inlineMath"`          // 是否启用行级公式
	InlineStrikethrough bool `json:"inlineStrikethrough"` // 是否启用行级删除线
}

type PushMode

type PushMode int
const (
	PushModeBroadcast                   PushMode = 0 // 所有应用所有会话广播
	PushModeSingleSelf                  PushMode = 1 // 自我应用会话单播
	PushModeBroadcastExcludeSelf        PushMode = 2 // 非自我会话广播
	PushModeBroadcastExcludeSelfApp     PushMode = 4 // 非自我应用所有会话广播
	PushModeBroadcastApp                PushMode = 5 // 单个应用内所有会话广播
	PushModeBroadcastMainExcludeSelfApp PushMode = 6 // 非自我应用主会话广播
)

type Result

type Result struct {
	Cmd       string      `json:"cmd"`
	ReqId     float64     `json:"reqId"`
	AppId     string      `json:"app"`
	SessionId string      `json:"sid"`
	PushMode  PushMode    `json:"pushMode"`
	Callback  interface{} `json:"callback"`
	Code      int         `json:"code"`
	Msg       string      `json:"msg"`
	Data      interface{} `json:"data"`
}

func NewCmdResult

func NewCmdResult(cmdName string, cmdId float64, pushMode PushMode) *Result

func NewResult

func NewResult() *Result

func (*Result) Bytes

func (r *Result) Bytes() []byte

type SessionData

type SessionData struct {
	Workspaces map[string]*WorkspaceSession // <WorkspacePath, WorkspaceSession>
}

SessionData represents the session.

func GetSession

func GetSession(c *gin.Context) *SessionData

GetSession returns session of the specified context.

func (*SessionData) Save

func (sd *SessionData) Save(c *gin.Context) error

Save saves the current session of the specified context.

type WorkspaceSession

type WorkspaceSession struct {
	AccessAuthCode string
	Captcha        string
}

func GetWorkspaceSession

func GetWorkspaceSession(session *SessionData) (ret *WorkspaceSession)

Jump to

Keyboard shortcuts

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