util

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2023 License: AGPL-3.0 Imports: 49 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BLOCK_BITS = 22 // Indicate that the blocksize is 4M
	BLOCK_SIZE = 1 << BLOCK_BITS
)
View Source
const (
	AliyunServer     = "https://siyuan-sync.b3logfile.com"  // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS SiYuanSyncServer
	SiYuanSyncServer = "https://siyuan-data.b3logfile.com/" // 云端数据同步服务地址,七牛云 OSS,用于数据同步文件上传、下载
	BazaarStatServer = "http://bazaar.b3logfile.com"        // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存
	BazaarOSSServer  = "https://oss.b3logfile.com"          // 云端对象存储地址,七牛云,仅用于读取集市包
	LiandiServer     = "https://ld246.com"                  // 链滴服务地址,用于分享发布帖子
)
View Source
const (
	ExitCodeReadOnlyDatabase      = 20 // 数据库文件被锁
	ExitCodeUnavailablePort       = 21 // 端口不可用
	ExitCodeCreateConfDirErr      = 22 // 创建配置目录失败
	ExitCodeBlockTreeErr          = 23 // 无法读写 blocktree.msgpack 文件
	ExitCodeWorkspaceLocked       = 24 // 工作空间已被锁定
	ExitCodeCreateWorkspaceDirErr = 25 // 创建工作空间失败
	ExitCodeOk                    = 0  // 正常退出
	ExitCodeFatal                 = 1  // 致命错误
)
View Source
const (
	// FrontendQueueInterval 为前端请求队列轮询间隔。
	FrontendQueueInterval = 512 * time.Millisecond

	// SQLFlushInterval 为数据库事务队列写入间隔。
	SQLFlushInterval = 3000 * time.Millisecond
)
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:子文档数降序
)
View Source
const (
	PushProgressCodeProgressed = 0 // 有进度
	PushProgressCodeEndless    = 1 // 无进度
	PushProgressCodeEnd        = 2 // 关闭进度
)
View Source
const (
	Ver       = "2.7.0"
	IsInsider = false
)
View Source
const (
	ContainerStd     = "std"     // 桌面端
	ContainerDocker  = "docker"  // Docker 容器端
	ContainerAndroid = "android" // Android 端
	ContainerIOS     = "ios"     // iOS 端

	LocalHost = "127.0.0.1" // 伺服地址
	FixedPort = "6806"      // 固定端口
)
View Source
const DatabaseVer = "20220501" // 修改表结构的话需要修改这里

Variables

View Source
var (
	SSL       = false
	UserAgent = "SiYuan/" + Ver
)
View Source
var (
	TesseractEnabled   bool
	AssetsTexts        = map[string]string{}
	AssetsTextsLock    = sync.Mutex{}
	AssetsTextsChanged = false

	TesseractLangs []string
)
View Source
var (
	HomeDir, _    = gulu.OS.Home()
	WorkingDir, _ = os.Getwd()

	WorkspaceDir   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 历史数据库文件路径
	BlockTreePath  string        // 区块树文件路径
	PandocBinPath  string        // Pandoc 可执行文件路径
	AppearancePath string        // 配置目录下的外观目录 appearance/ 路径
	ThemesPath     string        // 配置目录下的外观目录下的 themes/ 路径
	IconsPath      string        // 配置目录下的外观目录下的 icons/ 路径
	SnippetsPath   string        // 数据目录下的 snippets/ 路径

	UIProcessIDs = sync.Map{} // UI 进程 ID

	IsNewbie bool // 是否是第一次安装
)
View Source
var (
	ServerPort     = "0" // HTTP/WebSocket 端口,0 为使用随机端口
	ReadOnly       bool
	AccessAuthCode string
	Lang           = ""

	Container        string // docker, android, ios, std
	ISMicrosoftStore bool   // 桌面端是否是微软商店版
)
View Source
var (
	HttpServing = false // 是否 HTTP 伺服已经可用
)
View Source
var LocalIPs []string
View Source
var Mode = "prod"

var Mode = "dev"

View Source
var SK = []byte("696D897C9AA0611B")
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 CalSha1

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

func CeilSize

func CeilSize(size int64) int64

func ClearPushProgress

func ClearPushProgress(total int)

func ClosePushChan

func ClosePushChan(id string)

func ContextPushMsg

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

func CountSessions

func CountSessions() (ret int)

func CurrentTimeMillis

func CurrentTimeMillis() int64

func CurrentTimeSecondsStr

func CurrentTimeSecondsStr() string

func DataSize

func DataSize() (dataSize, assetsSize int64)

func EscapeHTML

func EscapeHTML(s string) 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 GetAssetText

func GetAssetText(asset string) string

func GetBootProgress

func GetBootProgress() float64

func GetBootProgressDetails

func GetBootProgressDetails() (float64, string)

func GetChildDocDepth

func GetChildDocDepth(treeAbsPath string) (ret int)

func GetDataAssetsAbsPath

func GetDataAssetsAbsPath() (ret string)

func GetDeviceID

func GetDeviceID() 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 GetLocalIPs

func GetLocalIPs() (ret []string)

func GetRemoteAddr

func GetRemoteAddr(session *melody.Session) string

func GetRhyResult

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

func GetSysFonts

func GetSysFonts(currentLanguage string) (ret []string)

func IncBootProgress

func IncBootProgress(progress float64, details string)

func IsAssetLinkDest

func IsAssetLinkDest(dest []byte) bool

func IsBooted

func IsBooted() bool

func IsCorruptedSYData

func IsCorruptedSYData(data []byte) bool

func IsEmptyDir

func IsEmptyDir(p string) bool

func IsIDPattern

func IsIDPattern(str string) bool

func IsMutexLocked

func IsMutexLocked(m *sync.Mutex) bool

func IsRelativePath

func IsRelativePath(dest string) bool

func IsReservedFilename

func IsReservedFilename(baseName string) bool

func IsSubFolder

func IsSubFolder(parent, sub string) bool

func IsValidPandocBin

func IsValidPandocBin(binPath 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 LatestTmpFile

func LatestTmpFile(p string) string

func Millisecond2Time

func Millisecond2Time(t int64) time.Time

func NeedCaptcha

func NeedCaptcha() bool

func NeedWarnDiskUsage

func NeedWarnDiskUsage(dataSize int64) bool

func NewLute

func NewLute() (ret *lute.Lute)

func NormalizeEndpoint

func NormalizeEndpoint(endpoint string) string

func NormalizeTimeout

func NormalizeTimeout(timeout int) int

func PinYinCompare

func PinYinCompare(str1, str2 string) bool

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 PushProgress

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

func PushStatusBar

func PushStatusBar(msg string)

func PushTxErr

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

func PushUpdateMsg

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

func RandomSleep

func RandomSleep(minMills, maxMills int)

func ReadWorkspacePaths

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

func ReloadUI

func ReloadUI()

func RemoveEmoji

func RemoveEmoji(text string) string

func RemoveID

func RemoveID(name string) string

func RemovePushChan

func RemovePushChan(session *melody.Session)

func RemoveWorkspaceSession

func RemoveWorkspaceSession(session *SessionData)

func Reverse

func Reverse(s string) string

func SessionsByType

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

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) string

func TimeFromID

func TimeFromID(id string) (ret string)

func UTF82GBK

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

UTF82GBK transform UTF8 rune into GBK byte array.

func UnlockWorkspace

func UnlockWorkspace()

func WriteWorkspacePaths

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

Types

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"`
}

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