mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-07-03 14:09:06 +02:00
e564ce7b1f
* ♻️ Add/update indirect Go dependencies in kernel Update kernel/go.mod and kernel/go.sum to add multiple indirect modules and checksum entries. Notable additions include github.com/fastschema/qjs, github.com/filecoin-project/go-jsonrpc, github.com/ipfs/go-log/v2, go.opencensus.io, go.uber.org/{atomic,multierr,zap}, golang.org/x/xerrors and github.com/golang/groupcache among many transitive entries. Changes ensure transitive dependencies are pinned and go.sum checksums are present (likely produced by `go mod tidy`) to make builds reproducible. * refactor: export bazaar.GetCurrentBackend for kernel plugin platform matching Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * build: promote qjs to direct dependency for kernel plugin system Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(plugin): add KernelPlugin struct with QJS runtime lifecycle and state machine Introduces plugin/plugin.go with KernelPlugin owning an isolated QuickJS runtime, a mutex-serialized call path, RPC method registration/dispatch, Promise awaiting, JSON round-trip result conversion, and WebSocket tracking. Adds sandbox_stub.go as a temporary no-op stub for injectSandboxGlobals. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(plugin): add PluginManager singleton for kernel plugin discovery and lifecycle * feat(plugin): add sandbox injection scaffold with siyuan.log Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(plugin): implement siyuan.storage CRUD scoped to petal storage directory Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(plugin): implement siyuan.fetch with browser-like Response interface * feat(plugin): implement siyuan.socket with browser-compatible WebSocket API - Add sync import for mutex-protected WebSocket connection tracking - Implement __siyuan_socket Go function that creates browser-compatible WebSocket objects - Support send() method with queueing for messages sent before connection opens - Support close() method for closing the WebSocket connection - Track connection state via readyState property (0=CONNECTING, 1=OPEN, 3=CLOSED) - Connect to kernel WebSocket endpoint with automatic auth token injection - Run WebSocket I/O in background goroutine with proper cleanup - Wire up siyuan.socket JS API Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(plugin): implement siyuan.rpc.register for JSON-RPC method registration * feat(plugin): add JSON-RPC 2.0 handler for kernel plugin method dispatch * feat(plugin): register /api/plugin/rpc/:name and /ws/plugin/rpc/:name routes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(plugin): wire kernel plugin manager start/stop into main lifecycle * feat(plugin): hook SetPetalEnabled to start/stop kernel plugins on enable/disable * test(plugin): add unit tests for kernel plugin state machine and eligibility * test(plugin): add comprehensive unit tests for manager, sandbox, and RPC handlers * refactor(plugin): Export IsTargetSupported and update usages Rename isTargetSupported to exported IsTargetSupported and adjust its comment. Replace local calls with bazaar.IsTargetSupported in kernel/bazaar and kernel/plugin/manager, removing the duplicated isKernelEligible helper. Update tests to import bazaar, call the new function, and change expectations to reflect that nil/empty kernel slices are treated as supported (i.e. supported on all platforms). * refactor(plugin): initialize PluginManager in main and update related usages * refactor(plugin): update JWT handling and plugin initialization for kernel plugins * refactor(plugin): enhance plugin initialization and improve sandbox global injections * refactor(kernel-plugin): Refactor plugin RPC registration and sandbox integration - Removed deprecated tests and refactored existing tests for clarity and efficiency. - Updated RPC method registration to use `bind` and `unbind` methods for better clarity. - Enhanced the `injectSandboxGlobals` function to include additional properties for the plugin. - Improved error handling in RPC methods and ensured proper state management for plugins. - Added benchmarks for map to JS conversion performance. - Cleaned up unused imports and organized code structure for better readability. * refactor(plugin): enhance concurrency handling and improve WebSocket integration * refactor(kernel-plugin): enhance RPC method handling and improve function registration * feat(kernel-plugin): add RPC method info retrieval and enhance plugin management * refactor(plugin): add plugin management endpoints and enhance plugin info retrieval * refactor(kernel-plugin): enhance RPC method handling and improve plugin info retrieval * refactor(kernel-plugin): improve error handling and response structures in RPC methods * refactor(kernel-plugin): improve error handling in RPC methods and enhance WebSocket closure management * fix(kernel-plugin): initialize sockets and socketMus maps in NewKernelPlugin * feat(kernel-plugin): add wsWrite helper and fix PushNotification omitempty Add wsWrite method on KernelPlugin that acquires the per-connection write mutex before sending a text frame, returning nil for untracked connections. Fix PushNotification's Params field to use omitempty for JSON-RPC 2.0 §4.2 compliance. Add rpc_test.go with newTestWsPair helper and tests for wsWrite. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(kernel-plugin): add BroadcastNotification and per-connection write mutex Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(kernel-plugin): expose siyuan.rpc.broadcast in plugin sandbox Add rpc.broadcast(method, params) binding in injectRpc so JS plugins can push JSON-RPC 2.0 notifications to all connected server clients. Fix deadlock by introducing a dedicated socketsMu RWMutex for the sockets map, decoupling socket tracking from the main plugin mutex that is held during Start()/Eval(). * fix(kernel-plugin): double-unlock in send handler and document PushNotification write-safety Remove spurious mu.Unlock() inside the nil-conn branch of injectSocket's CONNECTING-state send handler; the outer unconditional unlock is sufficient, so the inner one causes a panic under concurrent load. Document that PushNotification bypasses per-connection write serialization and must not be called concurrently with BroadcastNotification/wsWrite on the same connection without external locking. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * style(kernel-plugin): align struct field declarations in KernelPlugin Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(kernel-plugin): omit params field from JsonRpcRequest when nil (JSON-RPC 2.0 §4.1) Per spec, params MAY be omitted; add omitempty so marshaled requests with no parameters do not emit "params":null. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(kernel-plugin): change JsonRpcRequest.Params to *json.RawMessage A pointer correctly models the three-way distinction: - nil → params key absent (omitted from marshal output via omitempty) - non-nil → params present (null, array, or object) The previous []byte omitempty omitted the key only for nil/empty slices and could not distinguish absent from explicit null on the wire. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(kernel-plugin): unify method naming conventions and improve JSON-RPC request handling * fix(kernel-plugin): improve WebSocket message handling and ensure thread safety with mutexes * fix(kernel-plugin): enhance WebSocket handling and improve error management in storage methods * fix(kernel-plugin): rename JsonRpcRequestRaw to JsonRpcInboundRequest and update related methods * fix(kernel-plugin): improve plugin management and error handling in kernel plugin methods * fix(kernel-plugin): rename kernel field to kernels and update related references * feat(kernel-plugin): implement logging and improve concurrency handling in plugin manager and storage methods * feat(kernel-plugin): enhance RPC parameter handling and add JSON array parsing support * refactor(kernel-plugin): refactor RPC handling and improve logging functionality * refactor(kernel-plugin): streamline loggerWrapper function and improve error handling in injectFetch * refactor(kernel-plugin): optimize injectFetch function and enhance error handling * feat(kernel-plugin): add onLoaded hook and enhance plugin lifecycle management * feat(kernel-plugin): add ObjectFreeze and ObjectSeal functions to enhance API security * feat(kernel-plugin): add InitJwtKey function to generate JWT signing key * refactor(kernel-plugin): enhance error handling and logging in plugin lifecycle methods * feat(kernel-plugin): improve WebSocket error handling and add concurrency support in BroadcastNotification * feat(kernel-plugin): enhance error handling in storage and fetch methods with panic recovery * feat(kernel-plugin): enhance PluginManager concurrency and error handling with sync.Map and atomic operations * feat(kernel-plugin): refactor PluginState to use atomic operations for improved concurrency * feat(kernel-plugin): add PluginStateLoaded and update state management in plugin lifecycle * refactor(kernel-plugin): update logging level in loadPetals and refactor loggerWrapper return values * feat(kernel-plugin): simplify invokeHook and enhance error handling in Object methods * feat(kernel-plugin): remove obsolete test files for plugin functionality * refactor(kernel-plugin): implement loggerWrapper and rpcParamsToJsValue functions for improved logging and RPC parameter handling * feat(kernel-plugin): introduce Worker for serializing plugin tasks and enhance context management * refactor(worker): enhance task execution with callback support and graceful shutdown - Introduced a callback mechanism in the Task struct to handle results and errors. - Updated the Run method to accept a callback, allowing immediate handling of task results. - Added a RunSync method for synchronous task execution with result retrieval. - Implemented atomic closure state management to prevent task submission after closure. - Enhanced the Close method to ensure graceful shutdown and wait for the worker to finish processing. * feat(kernel-plugin): refactor storage and RPC methods to use PromiseRun for better error handling * feat(kernel-plugin): enhance plugin event handling with lifecycle and RPC event subscriptions * refactor(kernel-plugin): replace PromiseRun with worker.Run for improved error handling in event and storage methods * chore(kernel-plugin): add goja dependency, drop qjs * chore(kernel-plugin): delete KernelPluginLogger (qjs stdout/stderr only) * refactor(kernel-plugin): replace qjs runtime with goja in plugin.go Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test(kernel-plugin): add sandbox utility tests (pre-rewrite) * refactor(kernel-plugin): rewrite sandbox utility functions for goja Replace goValueToJsValue, getJsContextValue, dispatchEvent with goja implementations; add convertJsonNumbers helper; stub ObjectFreeze and ObjectSeal as no-ops; delete dead qjs-only helpers (invokeRpcMethod, PromiseAwait, rpcParamsToJsValue, parseJsonArrayStringToJsValueArray, parseJsonStringToJsValue, loggerWrapper, ObjectSetDataMethods). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(kernel-plugin): rewrite sandbox.go inject functions for goja Replace all qjs-based inject functions (injectGlobalContext, injectPlugin, injectLogger, injectEvent, injectStorage, injectFetch, injectSocket, injectRpc) with goja equivalents. Add ObjectSetDataMethods and loggerWrapper helpers. Remove all remaining qjs dead code; ObjectFreeze/ObjectSeal now call Object.freeze/seal via goja AssertFunction. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test(kernel-plugin): add plugin lifecycle and RPC integration tests Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(kernel-plugin): go mod tidy after qjs removal Remove fastschema/qjs from go.mod and go.sum, add go-sourcemap as indirect (transitive dep of dop251/goja), mark go-sourcemap indirect. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> * fix(kernel-plugin): fix invokeHook early-return on subscribe failure, safe await extraction, and goja value cross-goroutine access in socket methods * refactor(kernel-plugin): replace goValueToJsValue with goValueToJsValueSafely in sandbox functions and tests * feat(plugin): enhance plugin management and error handling - Added GetLoadedPlugin method to retrieve loaded plugin info by name. - Introduced file path for kernel.js in KernelPlugin struct. - Updated Eval method to use the new file path for script execution. - Improved error handling in injectGlobalContext and other injection functions using recover. - Refactored task execution in Worker to use clearer types for task executors and callbacks. - Enhanced storage methods to ensure proper error handling and logging. - Updated loggerWrapper to handle errors more gracefully. - Ensured consistent use of error handling patterns across various plugin methods. * refactor(worker): enhance task execution with goja runtime integration - Updated TaskExecutor and TaskCallback signatures to accept *goja.Runtime. - Modified Worker to start processing tasks with an event loop. - Improved error handling in task execution to catch panics from both executor and callback. - Renamed Close method to Stop for clarity on worker shutdown behavior. * refactor(kernel-plugin): streamline worker implementation and update context handling in plugin methods * refactor(kernel-plugin): update event handler to use byte slices and improve event dispatching * refactor(worker): simplify RunSync method by removing unnecessary select statement * refactor(kernel-plugin): enhance plugin lifecycle management and improve RPC method binding * refactor(kernel-plugin): improve error logging in data methods for better debugging * refactor(kernel-plugin): add version field to plugin data structures and update related methods * refactor(kernel-plugin): replace JsonRpcInboundRequest with JsonRpcRequest and update related methods * refactor(kernel-plugin): enhance plugin lifecycle hooks and improve RPC method invocation * feat(kernel-plugin): improve error handling and response processing in fetch and socket methods * refactor(kernel-plugin): update invokeFunction to handle promise results correctly * refactor(kernel-plugin): streamline event handling and remove unused JSON marshaling functions * refactor(kernel-plugin): improve error handling in start method and add event publishing for lifecycle states * refactor(kernel-plugin): move logging to separate function and execute in goroutines for improved performance * feat(kernel-plugin): add unique ID generation for start and stop events * refactor(kernel-plugin): enhance error handling and concurrency in storage operations Co-authored-by: Copilot <copilot@github.com> * fix(kernel-plugin): remove unexpected resolve in fetch function * feat(kernel-plugin): enhance JSON-RPC request handling with optional parameters and improved error reporting Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): rename await to async in dispatchEvent function for clarity Co-authored-by: Copilot <copilot@github.com> * fix(kernel-plugin): improve error handling in RPC method execution and hook invocation * feat(kernel-plugin): implement custom JSON marshaling for JsonRpcRequest to handle optional parameters * feat(kernel-plugin): add error codes for plugin state and improve error handling in RPC responses Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): clean up context usage and improve error logging for RPC methods * feat(kernel-plugin): add buffer method to object for asynchronous data processing * fix(kernel-plugin): Fixed the problem of blocking when plug-in life cycle function is not bound Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): implement public and private web server handlers and enhance request handling Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): enhance server request handling and introduce server handler invocation Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): enhance response handling and add jsValueToBytes conversion utility Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): comment out public web server route in router * feat(kernel-plugin): add WebSocket and EventSource proxy handlers and update sandbox integration Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): implement HTTP proxy handler with response header forwarding * refactor(kernel-plugin): refactor siyuan.client.* methods * feat(kernel-plugin): add support for EventSource with SSE handling and response header forwarding Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): add SSE support using r3labs/sse library for EventSource handling * feat(kernel-plugin): enhance SSE client with onclose event handling Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): implement SSE event handling and error management in server-sent events * feat(kernel-plugin): refactor SSE handling and introduce request handler utility functions Co-authored-by: Copilot <copilot@github.com> * feat(kernel-plugin): enhance WebSocket message handling with buffered amount tracking and cleanup Co-authored-by: Copilot <copilot@github.com> * perf(kernel-plugin): improve WebSocket message handling with channel-based message sending and error management Co-Authored-By: Copilot <copilot@github.com> * refactor(kernel-plugin): remove invokeServerHandler Co-Authored-By: Copilot <copilot@github.com> * feat(kernel-plugin): implement WebSocket message handling with improved structure and error management Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): Refactor code structure for improved readability and maintainability * refactor(kernel-plugin): streamline HTTP client creation and enhance event source state management Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): enhance WebSocket and SSE handling with improved closure management and error handling Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): optimize WebSocket handling by restructuring state management and improving closure logic Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): simplify header setting and improve null checks in WebSocket and SSE handling Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): update WebSocket request handling to improve error management and consistency * refactor(kernel-plugin): improve WebSocket error handling by adding close message management Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): Refactor WebSocket handling to use gws library - Replaced gorilla/websocket with lxzan/gws for WebSocket connections. - Introduced gwsEventHandler to manage WebSocket events with customizable callbacks. - Updated KernelPlugin to track gws connections and handle message broadcasting. - Refactored RPC WebSocket handling to accommodate new gws structure. - Simplified message sending and connection management logic. - Added utility function to check for undefined JavaScript values. Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): integrate gws library for improved WebSocket handling and error management Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): remove unnecessary error handling in WebSocket request processing * refactor(kernel-plugin): enhance error logging in WebSocket message handling Co-Authored-By: Copilot <copilot@github.com> * refactor(kernel-plugin): replace gwsEventHandler with WsEventHandler and improve WebSocket management Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): integrate chanx for improved event handling in SSE * refactor(kernel-plugin): update handleHttpRequest signature to include gin.Context for improved request handling Co-authored-by: Copilot <copilot@github.com> * refactor(kernel-plugin): optimize WebSocket connection management with context and sync mechanisms * refactor(kernel-plugin): improve error handling and context management in WebSocket and HTTP request handling * refactor(kernel-plugin): enhance WebSocket management with context handling and improved error reporting * fix(kernel-plugin): streamline header export and enhance error handling in injectClient function Co-authored-by: Copilot <copilot@github.com> * perf(kernel-plugin): enhance httpProxy and esProxy functions with improved error handling and content management Co-authored-by: Copilot <copilot@github.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Copilot <copilot@github.com>
1302 lines
39 KiB
Go
1302 lines
39 KiB
Go
// SiYuan - Refactor your thinking
|
||
// Copyright (c) 2020-present, b3log.org
|
||
//
|
||
// This program is free software: you can redistribute it and/or modify
|
||
// it under the terms of the GNU Affero General Public License as published by
|
||
// the Free Software Foundation, either version 3 of the License, or
|
||
// (at your option) any later version.
|
||
//
|
||
// This program is distributed in the hope that it will be useful,
|
||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
// GNU Affero General Public License for more details.
|
||
//
|
||
// You should have received a copy of the GNU Affero General Public License
|
||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
package model
|
||
|
||
import (
|
||
"bytes"
|
||
"crypto/sha1"
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"runtime"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/88250/gulu"
|
||
"github.com/88250/lute"
|
||
"github.com/88250/lute/ast"
|
||
"github.com/Xuanwo/go-locale"
|
||
"github.com/sashabaranov/go-openai"
|
||
"github.com/siyuan-note/eventbus"
|
||
"github.com/siyuan-note/filelock"
|
||
"github.com/siyuan-note/logging"
|
||
"github.com/siyuan-note/siyuan/kernel/conf"
|
||
"github.com/siyuan-note/siyuan/kernel/sql"
|
||
"github.com/siyuan-note/siyuan/kernel/task"
|
||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||
"github.com/siyuan-note/siyuan/kernel/util"
|
||
"golang.org/x/mod/semver"
|
||
"golang.org/x/text/language"
|
||
)
|
||
|
||
var Conf *AppConf
|
||
|
||
// AppConf 维护应用元数据,保存在 ~/.siyuan/conf.json。
|
||
type AppConf struct {
|
||
LogLevel string `json:"logLevel"` // 日志级别:off, trace, debug, info, warn, error, fatal
|
||
Appearance *conf.Appearance `json:"appearance"` // 外观
|
||
Langs []*conf.Lang `json:"langs"` // 界面语言列表
|
||
Lang string `json:"lang"` // 选择的界面语言,同 Appearance.Lang
|
||
FileTree *conf.FileTree `json:"fileTree"` // 文档面板
|
||
Tag *conf.Tag `json:"tag"` // 标签面板
|
||
Editor *conf.Editor `json:"editor"` // 编辑器配置
|
||
Export *conf.Export `json:"export"` // 导出配置
|
||
Graph *conf.Graph `json:"graph"` // 关系图配置
|
||
UILayout *conf.UILayout `json:"uiLayout"` // 界面布局。不要直接使用,使用 GetUILayout() 和 SetUILayout() 方法
|
||
UserData string `json:"userData"` // 社区用户信息,对 User 加密存储
|
||
User *conf.User `json:"-"` // 社区用户内存结构,不持久化。不要直接使用,使用 GetUser() 和 SetUser() 方法
|
||
Account *conf.Account `json:"account"` // 帐号配置
|
||
ReadOnly bool `json:"readonly"` // 是否是以只读模式运行
|
||
ServerAddrs []string `json:"serverAddrs"` // 本地服务器地址列表
|
||
AccessAuthCode string `json:"accessAuthCode"` // 访问授权码
|
||
System *conf.System `json:"system"` // 系统配置
|
||
Keymap *conf.Keymap `json:"keymap"` // 快捷键配置
|
||
Sync *conf.Sync `json:"sync"` // 同步配置
|
||
Search *conf.Search `json:"search"` // 搜索配置
|
||
Flashcard *conf.Flashcard `json:"flashcard"` // 闪卡配置
|
||
AI *conf.AI `json:"ai"` // 人工智能配置
|
||
Bazaar *conf.Bazaar `json:"bazaar"` // 集市配置
|
||
Stat *conf.Stat `json:"stat"` // 统计
|
||
Api *conf.API `json:"api"` // API
|
||
Repo *conf.Repo `json:"repo"` // 数据仓库
|
||
Publish *conf.Publish `json:"publish"` // 发布服务
|
||
OpenHelp bool `json:"openHelp"` // 启动后是否需要打开用户指南
|
||
ShowChangelog bool `json:"showChangelog"` // 是否显示版本更新日志
|
||
CloudRegion int `json:"cloudRegion"` // 云端区域,0:中国大陆,1:北美
|
||
Snippet *conf.Snpt `json:"snippet"` // 代码片段
|
||
DataIndexState int `json:"dataIndexState"` // 数据索引状态,0:已索引,1:未索引
|
||
CookieKey string `json:"cookieKey"` // 用于加密 Cookie 的密钥
|
||
|
||
m *sync.RWMutex // 配置数据锁
|
||
userLock *sync.RWMutex // 用户数据独立锁,避免与配置保存操作竞争
|
||
}
|
||
|
||
func NewAppConf() *AppConf {
|
||
return &AppConf{
|
||
LogLevel: "debug",
|
||
m: &sync.RWMutex{},
|
||
userLock: &sync.RWMutex{},
|
||
}
|
||
}
|
||
|
||
func (conf *AppConf) GetUILayout() *conf.UILayout {
|
||
conf.m.Lock()
|
||
defer conf.m.Unlock()
|
||
return conf.UILayout
|
||
}
|
||
|
||
func (conf *AppConf) SetUILayout(uiLayout *conf.UILayout) {
|
||
conf.m.Lock()
|
||
defer conf.m.Unlock()
|
||
conf.UILayout = uiLayout
|
||
}
|
||
|
||
func (conf *AppConf) GetUser() *conf.User {
|
||
conf.userLock.RLock()
|
||
defer conf.userLock.RUnlock()
|
||
return conf.User
|
||
}
|
||
|
||
func (conf *AppConf) SetUser(user *conf.User) {
|
||
conf.userLock.Lock()
|
||
defer conf.userLock.Unlock()
|
||
conf.User = user
|
||
}
|
||
|
||
func InitConf() {
|
||
initLang()
|
||
|
||
Conf = NewAppConf()
|
||
confPath := filepath.Join(util.ConfDir, "conf.json")
|
||
if gulu.File.IsExist(confPath) {
|
||
if data, err := os.ReadFile(confPath); err != nil {
|
||
logging.LogErrorf("load conf [%s] failed: %s", confPath, err)
|
||
} else {
|
||
if err = gulu.JSON.UnmarshalJSON(data, Conf); err != nil {
|
||
logging.LogErrorf("parse conf [%s] failed: %s", confPath, err)
|
||
} else {
|
||
logging.LogInfof("loaded conf [%s]", confPath)
|
||
}
|
||
}
|
||
}
|
||
|
||
if "" != util.Lang {
|
||
initialized := false
|
||
if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
|
||
// 移动端以上次设置的外观语言为准
|
||
if "" != Conf.Lang && util.Lang != Conf.Lang {
|
||
util.Lang = Conf.Lang
|
||
logging.LogInfof("use the last specified language [%s]", util.Lang)
|
||
initialized = true
|
||
}
|
||
}
|
||
|
||
if !initialized {
|
||
Conf.Lang = util.Lang
|
||
logging.LogInfof("initialized the specified language [%s]", util.Lang)
|
||
}
|
||
} else {
|
||
if "" == Conf.Lang {
|
||
// 未指定外观语言时使用系统语言
|
||
|
||
if userLang, err := locale.Detect(); err == nil {
|
||
var supportLangs []language.Tag
|
||
for lang := range util.Langs {
|
||
if tag, err := language.Parse(lang); err == nil {
|
||
supportLangs = append(supportLangs, tag)
|
||
} else {
|
||
logging.LogErrorf("load language [%s] failed: %s", lang, err)
|
||
}
|
||
}
|
||
matcher := language.NewMatcher(supportLangs)
|
||
lang, _, _ := matcher.Match(userLang)
|
||
base, _ := lang.Base()
|
||
region, _ := lang.Region()
|
||
util.Lang = base.String() + "_" + region.String()
|
||
Conf.Lang = util.Lang
|
||
logging.LogInfof("initialized language [%s] based on device locale", Conf.Lang)
|
||
} else {
|
||
logging.LogDebugf("check device locale failed [%s], using default language [en_US]", err)
|
||
util.Lang = "en_US"
|
||
Conf.Lang = util.Lang
|
||
}
|
||
}
|
||
util.Lang = Conf.Lang
|
||
}
|
||
|
||
Conf.Langs = loadLangs()
|
||
if nil == Conf.Appearance {
|
||
Conf.Appearance = conf.NewAppearance()
|
||
}
|
||
var langOK bool
|
||
for _, l := range Conf.Langs {
|
||
if Conf.Lang == l.Name {
|
||
langOK = true
|
||
break
|
||
}
|
||
}
|
||
if !langOK {
|
||
Conf.Lang = "en_US"
|
||
util.Lang = Conf.Lang
|
||
}
|
||
Conf.Appearance.Lang = Conf.Lang
|
||
if "ant" == Conf.Appearance.Icon || "material" == Conf.Appearance.Icon {
|
||
// v3.7.0 移除了 ant/material 图标包,如果用户之前选择了这两个其中之一,升级后改为 litheness 图标包,避免图标显示异常 https://github.com/siyuan-note/siyuan/issues/7976
|
||
Conf.Appearance.Icon = "litheness"
|
||
}
|
||
os.RemoveAll(filepath.Join(util.IconsPath, "ant"))
|
||
os.RemoveAll(filepath.Join(util.IconsPath, "material"))
|
||
if nil == Conf.UILayout {
|
||
Conf.UILayout = &conf.UILayout{}
|
||
}
|
||
if nil == Conf.Keymap {
|
||
Conf.Keymap = &conf.Keymap{}
|
||
}
|
||
if "" == Conf.Appearance.CodeBlockThemeDark {
|
||
Conf.Appearance.CodeBlockThemeDark = "dracula"
|
||
}
|
||
if "" == Conf.Appearance.CodeBlockThemeLight {
|
||
Conf.Appearance.CodeBlockThemeLight = "github"
|
||
}
|
||
if nil == Conf.Appearance.StatusBar {
|
||
Conf.Appearance.StatusBar = &util.StatusBar{}
|
||
}
|
||
util.StatusBarCfg = Conf.Appearance.StatusBar
|
||
if nil == Conf.FileTree {
|
||
Conf.FileTree = conf.NewFileTree()
|
||
}
|
||
if 1 > Conf.FileTree.MaxListCount {
|
||
Conf.FileTree.MaxListCount = 512
|
||
}
|
||
if 1 > Conf.FileTree.MaxOpenTabCount {
|
||
Conf.FileTree.MaxOpenTabCount = 8
|
||
}
|
||
if 32 < Conf.FileTree.MaxOpenTabCount {
|
||
Conf.FileTree.MaxOpenTabCount = 32
|
||
}
|
||
Conf.FileTree.DocCreateSavePath = util.TrimSpaceInPath(Conf.FileTree.DocCreateSavePath)
|
||
Conf.FileTree.RefCreateSavePath = util.TrimSpaceInPath(Conf.FileTree.RefCreateSavePath)
|
||
util.UseSingleLineSave = Conf.FileTree.UseSingleLineSave
|
||
if 2 > Conf.FileTree.LargeFileWarningSize {
|
||
Conf.FileTree.LargeFileWarningSize = 8
|
||
}
|
||
util.LargeFileWarningSize = Conf.FileTree.LargeFileWarningSize
|
||
if nil == Conf.FileTree.CreateDocAtTop { // v3.4.0 之前的版本没有该字段,设置默认值为 true,即在顶部创建新文档,不改变用户习惯
|
||
Conf.FileTree.CreateDocAtTop = func() *bool { b := true; return &b }()
|
||
}
|
||
|
||
if conf.MinFileTreeRecentDocsListCount > Conf.FileTree.RecentDocsMaxListCount {
|
||
Conf.FileTree.RecentDocsMaxListCount = conf.MinFileTreeRecentDocsListCount
|
||
}
|
||
if conf.MaxFileTreeRecentDocsListCount < Conf.FileTree.RecentDocsMaxListCount {
|
||
Conf.FileTree.RecentDocsMaxListCount = conf.MaxFileTreeRecentDocsListCount
|
||
}
|
||
|
||
util.CurrentCloudRegion = Conf.CloudRegion
|
||
|
||
if nil == Conf.Tag {
|
||
Conf.Tag = conf.NewTag()
|
||
}
|
||
|
||
defaultEditor := conf.NewEditor()
|
||
if nil == Conf.Editor {
|
||
Conf.Editor = defaultEditor
|
||
}
|
||
|
||
// 新增字段的默认值,使用指针类型来区分字段不存在(nil)和用户设置为 0(非 nil)
|
||
if nil == Conf.Editor.BacklinkSort {
|
||
Conf.Editor.BacklinkSort = defaultEditor.BacklinkSort
|
||
}
|
||
if nil == Conf.Editor.BackmentionSort {
|
||
Conf.Editor.BackmentionSort = defaultEditor.BackmentionSort
|
||
}
|
||
if 1 > len(Conf.Editor.Emoji) {
|
||
Conf.Editor.Emoji = []string{}
|
||
}
|
||
for i, emoji := range Conf.Editor.Emoji {
|
||
if strings.Contains(emoji, ".") {
|
||
// XSS through emoji name https://github.com/siyuan-note/siyuan/issues/15034
|
||
emoji = util.FilterUploadEmojiFileName(emoji)
|
||
Conf.Editor.Emoji[i] = emoji
|
||
}
|
||
}
|
||
if 9 > Conf.Editor.FontSize || 72 < Conf.Editor.FontSize {
|
||
Conf.Editor.FontSize = 16
|
||
}
|
||
if "" == Conf.Editor.PlantUMLServePath {
|
||
Conf.Editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
|
||
}
|
||
if 1 > Conf.Editor.BlockRefDynamicAnchorTextMaxLen {
|
||
Conf.Editor.BlockRefDynamicAnchorTextMaxLen = 64
|
||
}
|
||
if 5120 < Conf.Editor.BlockRefDynamicAnchorTextMaxLen {
|
||
Conf.Editor.BlockRefDynamicAnchorTextMaxLen = 5120
|
||
}
|
||
if 1440 < Conf.Editor.GenerateHistoryInterval {
|
||
Conf.Editor.GenerateHistoryInterval = 1440
|
||
}
|
||
if 1 > Conf.Editor.HistoryRetentionDays {
|
||
Conf.Editor.HistoryRetentionDays = 30
|
||
}
|
||
if 3650 < Conf.Editor.HistoryRetentionDays {
|
||
Conf.Editor.HistoryRetentionDays = 3650
|
||
}
|
||
if nil == Conf.Editor.FloatWindowDelay {
|
||
v := 620
|
||
Conf.Editor.FloatWindowDelay = &v
|
||
} else {
|
||
*Conf.Editor.FloatWindowDelay = max(0, min(2000, *Conf.Editor.FloatWindowDelay))
|
||
}
|
||
if conf.MinDynamicLoadBlocks > Conf.Editor.DynamicLoadBlocks {
|
||
Conf.Editor.DynamicLoadBlocks = conf.MinDynamicLoadBlocks
|
||
}
|
||
if 1 > len(Conf.Editor.SpellcheckLanguages) {
|
||
Conf.Editor.SpellcheckLanguages = []string{"en-US"}
|
||
}
|
||
if 0 > Conf.Editor.BacklinkExpandCount {
|
||
Conf.Editor.BacklinkExpandCount = 0
|
||
}
|
||
if -1 > Conf.Editor.BackmentionExpandCount {
|
||
Conf.Editor.BackmentionExpandCount = -1
|
||
}
|
||
if nil == Conf.Editor.Markdown {
|
||
Conf.Editor.Markdown = &util.Markdown{}
|
||
}
|
||
util.MarkdownSettings = Conf.Editor.Markdown
|
||
|
||
if nil == Conf.Export {
|
||
Conf.Export = conf.NewExport()
|
||
}
|
||
if 0 == Conf.Export.BlockRefMode || 1 == Conf.Export.BlockRefMode || 5 == Conf.Export.BlockRefMode {
|
||
// 废弃导出选项引用块转换为原始块和引述块 https://github.com/siyuan-note/siyuan/issues/3155
|
||
// 锚点哈希模式和脚注模式合并 https://github.com/siyuan-note/siyuan/issues/13331
|
||
Conf.Export.BlockRefMode = 4 // 改为脚注+锚点哈希
|
||
}
|
||
if "" == Conf.Export.PandocBin {
|
||
Conf.Export.PandocBin = util.PandocBinPath
|
||
}
|
||
|
||
if nil == Conf.Graph || nil == Conf.Graph.Local || nil == Conf.Graph.Global {
|
||
Conf.Graph = conf.NewGraph()
|
||
}
|
||
|
||
if nil == Conf.System {
|
||
Conf.System = conf.NewSystem()
|
||
if util.ContainerIOS != util.Container {
|
||
Conf.OpenHelp = true
|
||
}
|
||
} else {
|
||
cmp := semver.Compare("v"+util.Ver, "v"+Conf.System.KernelVersion)
|
||
if 0 < cmp {
|
||
logging.LogInfof("upgraded from version [%s] to [%s]", Conf.System.KernelVersion, util.Ver)
|
||
Conf.ShowChangelog = true
|
||
} else if 0 > cmp {
|
||
logging.LogInfof("downgraded from version [%s] to [%s]", Conf.System.KernelVersion, util.Ver)
|
||
}
|
||
|
||
Conf.System.KernelVersion = util.Ver
|
||
Conf.System.IsInsider = util.IsInsider
|
||
}
|
||
if nil == Conf.System.NetworkProxy {
|
||
Conf.System.NetworkProxy = &conf.NetworkProxy{}
|
||
}
|
||
if "" == Conf.System.ID {
|
||
Conf.System.ID = util.GetDeviceID()
|
||
}
|
||
if "" == Conf.System.Name {
|
||
Conf.System.Name = util.GetDeviceName()
|
||
}
|
||
if util.ContainerStd == util.Container {
|
||
Conf.System.ID = util.GetDeviceID()
|
||
Conf.System.Name = util.GetDeviceName()
|
||
}
|
||
Conf.System.DisabledFeatures = util.DisabledFeatures
|
||
if 1 > len(Conf.System.DisabledFeatures) {
|
||
Conf.System.DisabledFeatures = []string{}
|
||
}
|
||
|
||
Conf.System.AppDir = util.WorkingDir
|
||
Conf.System.ConfDir = util.ConfDir
|
||
Conf.System.HomeDir = util.HomeDir
|
||
Conf.System.WorkspaceDir = util.WorkspaceDir
|
||
Conf.System.DataDir = util.DataDir
|
||
Conf.System.Container = util.Container
|
||
Conf.System.IsMicrosoftStore = util.ISMicrosoftStore
|
||
if util.ISMicrosoftStore {
|
||
logging.LogInfof("using Microsoft Store edition")
|
||
}
|
||
Conf.System.OS = runtime.GOOS
|
||
Conf.System.OSPlatform = util.GetOSPlatform()
|
||
|
||
docxTemplate := util.RemoveInvalid(Conf.Export.DocxTemplate)
|
||
if "" != docxTemplate {
|
||
params := util.RemoveInvalid(Conf.Export.PandocParams)
|
||
if gulu.File.IsExist(docxTemplate) && !strings.Contains(params, "--reference-doc") && !Conf.System.IsMicrosoftStore {
|
||
if !strings.HasPrefix(docxTemplate, "\"") {
|
||
docxTemplate = "\"" + docxTemplate + "\""
|
||
}
|
||
params += " --reference-doc " + docxTemplate
|
||
Conf.Export.PandocParams = strings.TrimSpace(params)
|
||
}
|
||
Conf.Export.DocxTemplate = ""
|
||
Conf.Save()
|
||
}
|
||
|
||
if nil == Conf.Snippet {
|
||
Conf.Snippet = conf.NewSnpt()
|
||
}
|
||
|
||
if "" != Conf.UserData {
|
||
Conf.SetUser(loadUserFromConf())
|
||
}
|
||
if nil == Conf.Account {
|
||
Conf.Account = conf.NewAccount()
|
||
}
|
||
|
||
if nil == Conf.Sync {
|
||
Conf.Sync = conf.NewSync()
|
||
}
|
||
if 0 == Conf.Sync.Mode {
|
||
Conf.Sync.Mode = 1
|
||
}
|
||
if 30 > Conf.Sync.Interval {
|
||
Conf.Sync.Interval = 30
|
||
}
|
||
if 60*60*12 < Conf.Sync.Interval {
|
||
Conf.Sync.Interval = 60 * 60 * 12
|
||
}
|
||
if nil == Conf.Sync.S3 {
|
||
Conf.Sync.S3 = &conf.S3{PathStyle: true, SkipTlsVerify: true}
|
||
}
|
||
Conf.Sync.S3.Endpoint = util.NormalizeEndpoint(Conf.Sync.S3.Endpoint)
|
||
Conf.Sync.S3.Timeout = util.NormalizeTimeout(Conf.Sync.S3.Timeout)
|
||
Conf.Sync.S3.ConcurrentReqs = util.NormalizeConcurrentReqs(Conf.Sync.S3.ConcurrentReqs, conf.ProviderS3)
|
||
if nil == Conf.Sync.WebDAV {
|
||
Conf.Sync.WebDAV = &conf.WebDAV{SkipTlsVerify: true}
|
||
}
|
||
Conf.Sync.WebDAV.Endpoint = util.NormalizeEndpoint(Conf.Sync.WebDAV.Endpoint)
|
||
Conf.Sync.WebDAV.Timeout = util.NormalizeTimeout(Conf.Sync.WebDAV.Timeout)
|
||
Conf.Sync.WebDAV.ConcurrentReqs = util.NormalizeConcurrentReqs(Conf.Sync.WebDAV.ConcurrentReqs, conf.ProviderWebDAV)
|
||
if nil == Conf.Sync.Local {
|
||
Conf.Sync.Local = &conf.Local{}
|
||
}
|
||
Conf.Sync.Local.Endpoint = util.NormalizeLocalPath(Conf.Sync.Local.Endpoint)
|
||
Conf.Sync.Local.Timeout = util.NormalizeTimeout(Conf.Sync.Local.Timeout)
|
||
Conf.Sync.Local.ConcurrentReqs = util.NormalizeConcurrentReqs(Conf.Sync.Local.ConcurrentReqs, conf.ProviderLocal)
|
||
|
||
if util.ContainerDocker == util.Container {
|
||
Conf.Sync.Perception = false
|
||
}
|
||
|
||
if nil == Conf.Api {
|
||
Conf.Api = conf.NewAPI()
|
||
}
|
||
|
||
if nil == Conf.Bazaar {
|
||
Conf.Bazaar = conf.NewBazaar()
|
||
}
|
||
|
||
if nil == Conf.Publish {
|
||
Conf.Publish = conf.NewPublish()
|
||
}
|
||
if Conf.OpenHelp && Conf.Publish.Enable {
|
||
Conf.OpenHelp = false
|
||
}
|
||
|
||
if nil == Conf.Repo {
|
||
Conf.Repo = conf.NewRepo()
|
||
}
|
||
if timingEnv := os.Getenv("SIYUAN_SYNC_INDEX_TIMING"); "" != timingEnv {
|
||
val, err := strconv.Atoi(timingEnv)
|
||
if err == nil {
|
||
Conf.Repo.SyncIndexTiming = int64(val)
|
||
}
|
||
}
|
||
if 12000 > Conf.Repo.SyncIndexTiming {
|
||
Conf.Repo.SyncIndexTiming = 12 * 1000
|
||
}
|
||
if 1 > Conf.Repo.IndexRetentionDays {
|
||
Conf.Repo.IndexRetentionDays = 180
|
||
}
|
||
if 1 > Conf.Repo.RetentionIndexesDaily {
|
||
Conf.Repo.RetentionIndexesDaily = 2
|
||
}
|
||
if 0 < len(Conf.Repo.Key) {
|
||
logging.LogInfof("repo key [%x]", sha1.Sum(Conf.Repo.Key))
|
||
}
|
||
|
||
if nil == Conf.Search {
|
||
Conf.Search = conf.NewSearch()
|
||
}
|
||
if 1 > Conf.Search.Limit {
|
||
Conf.Search.Limit = 64
|
||
}
|
||
if 32 > Conf.Search.Limit {
|
||
Conf.Search.Limit = 32
|
||
}
|
||
if 1 > Conf.Search.BacklinkMentionKeywordsLimit {
|
||
Conf.Search.BacklinkMentionKeywordsLimit = 512
|
||
}
|
||
|
||
if nil == Conf.Stat {
|
||
Conf.Stat = conf.NewStat()
|
||
}
|
||
|
||
if nil == Conf.Flashcard {
|
||
Conf.Flashcard = conf.NewFlashcard()
|
||
}
|
||
if 0 > Conf.Flashcard.NewCardLimit {
|
||
Conf.Flashcard.NewCardLimit = 20
|
||
}
|
||
if 0 > Conf.Flashcard.ReviewCardLimit {
|
||
Conf.Flashcard.ReviewCardLimit = 200
|
||
}
|
||
if 0 >= Conf.Flashcard.RequestRetention || 1 <= Conf.Flashcard.RequestRetention {
|
||
Conf.Flashcard.RequestRetention = conf.NewFlashcard().RequestRetention
|
||
}
|
||
if 0 >= Conf.Flashcard.MaximumInterval || 36500 <= Conf.Flashcard.MaximumInterval {
|
||
Conf.Flashcard.MaximumInterval = conf.NewFlashcard().MaximumInterval
|
||
}
|
||
if "" == Conf.Flashcard.Weights {
|
||
Conf.Flashcard.Weights = conf.NewFlashcard().Weights
|
||
}
|
||
if 19 != len(strings.Split(Conf.Flashcard.Weights, ",")) {
|
||
defaultWeights := conf.DefaultFSRSWeights()
|
||
msg := "fsrs store weights length must be [19]"
|
||
logging.LogWarnf("%s , given [%s], reset to default weights [%s]", msg, Conf.Flashcard.Weights, defaultWeights)
|
||
Conf.Flashcard.Weights = defaultWeights
|
||
go func() {
|
||
util.WaitForUILoaded()
|
||
task.AppendAsyncTaskWithDelay(task.PushMsg, 2*time.Second, util.PushErrMsg, msg, 15000)
|
||
}()
|
||
}
|
||
isInvalidFlashcardWeights := false
|
||
for _, w := range strings.Split(Conf.Flashcard.Weights, ",") {
|
||
if _, err := strconv.ParseFloat(strings.TrimSpace(w), 64); err != nil {
|
||
isInvalidFlashcardWeights = true
|
||
break
|
||
}
|
||
}
|
||
if isInvalidFlashcardWeights {
|
||
defaultWeights := conf.DefaultFSRSWeights()
|
||
msg := "fsrs store weights contain invalid number"
|
||
logging.LogWarnf("%s, given [%s], reset to default weights [%s]", msg, Conf.Flashcard.Weights, defaultWeights)
|
||
Conf.Flashcard.Weights = defaultWeights
|
||
go func() {
|
||
util.WaitForUILoaded()
|
||
task.AppendAsyncTaskWithDelay(task.PushMsg, 2*time.Second, util.PushErrMsg, msg, 15000)
|
||
}()
|
||
}
|
||
|
||
if nil == Conf.AI {
|
||
Conf.AI = conf.NewAI()
|
||
}
|
||
if "" == Conf.AI.OpenAI.APIModel {
|
||
Conf.AI.OpenAI.APIModel = openai.GPT3Dot5Turbo
|
||
}
|
||
if "" == Conf.AI.OpenAI.APIUserAgent {
|
||
Conf.AI.OpenAI.APIUserAgent = util.UserAgent
|
||
}
|
||
if strings.HasPrefix(Conf.AI.OpenAI.APIUserAgent, "SiYuan/") {
|
||
Conf.AI.OpenAI.APIUserAgent = util.UserAgent
|
||
}
|
||
if "" == Conf.AI.OpenAI.APIProvider {
|
||
Conf.AI.OpenAI.APIProvider = "OpenAI"
|
||
}
|
||
if 0 > Conf.AI.OpenAI.APIMaxTokens {
|
||
Conf.AI.OpenAI.APIMaxTokens = 0
|
||
}
|
||
if 0 >= Conf.AI.OpenAI.APITemperature || 2 < Conf.AI.OpenAI.APITemperature {
|
||
Conf.AI.OpenAI.APITemperature = 1.0
|
||
}
|
||
if 1 > Conf.AI.OpenAI.APIMaxContexts || 64 < Conf.AI.OpenAI.APIMaxContexts {
|
||
Conf.AI.OpenAI.APIMaxContexts = 7
|
||
}
|
||
|
||
if "" != Conf.AI.OpenAI.APIKey {
|
||
logging.LogInfof("OpenAI API enabled\n"+
|
||
" userAgent=%s\n"+
|
||
" baseURL=%s\n"+
|
||
" timeout=%ds\n"+
|
||
" proxy=%s\n"+
|
||
" model=%s\n"+
|
||
" maxTokens=%d\n"+
|
||
" temperature=%.1f\n"+
|
||
" maxContexts=%d",
|
||
Conf.AI.OpenAI.APIUserAgent,
|
||
Conf.AI.OpenAI.APIBaseURL,
|
||
Conf.AI.OpenAI.APITimeout,
|
||
Conf.AI.OpenAI.APIProxy,
|
||
Conf.AI.OpenAI.APIModel,
|
||
Conf.AI.OpenAI.APIMaxTokens,
|
||
Conf.AI.OpenAI.APITemperature,
|
||
Conf.AI.OpenAI.APIMaxContexts)
|
||
}
|
||
|
||
Conf.ReadOnly = util.ReadOnly
|
||
|
||
if "" != util.AccessAuthCode {
|
||
Conf.AccessAuthCode = util.AccessAuthCode
|
||
}
|
||
Conf.AccessAuthCode = util.RemoveInvalid(Conf.AccessAuthCode)
|
||
Conf.AccessAuthCode = strings.TrimSpace(Conf.AccessAuthCode)
|
||
|
||
if 1 == Conf.DataIndexState {
|
||
// 上次未正常完成数据索引,后续会由 recoverWAL() 恢复
|
||
logging.LogInfof("data index state is [%d], will recover through WAL", Conf.DataIndexState)
|
||
}
|
||
|
||
Conf.DataIndexState = 0
|
||
|
||
if cookieKey := readCookieKey(); "" != cookieKey {
|
||
Conf.CookieKey = cookieKey
|
||
} else {
|
||
if "" == Conf.CookieKey {
|
||
Conf.CookieKey = gulu.Rand.String(16)
|
||
}
|
||
writeCookieKey(Conf.CookieKey)
|
||
}
|
||
|
||
Conf.Save()
|
||
logging.SetLogLevel(Conf.LogLevel)
|
||
|
||
util.SetNetworkProxy(Conf.System.NetworkProxy.String())
|
||
|
||
go util.InitPandoc()
|
||
go util.InitTesseract()
|
||
}
|
||
|
||
func readCookieKey() (cookieKey string) {
|
||
cookieKeyPath := filepath.Join(util.HomeDir, ".config", "siyuan", "cookie.key")
|
||
if !gulu.File.IsExist(cookieKeyPath) {
|
||
return
|
||
}
|
||
|
||
data, err := os.ReadFile(cookieKeyPath)
|
||
if err != nil {
|
||
logging.LogErrorf("read cookie key file [%s] failed: %s", cookieKeyPath, err)
|
||
return
|
||
}
|
||
|
||
cookieKey = string(bytes.TrimSpace(data))
|
||
return
|
||
}
|
||
|
||
func writeCookieKey(cookieKey string) {
|
||
cookieKeyPath := filepath.Join(util.HomeDir, ".config", "siyuan", "cookie.key")
|
||
if gulu.File.IsExist(cookieKeyPath) {
|
||
return
|
||
}
|
||
|
||
if err := os.WriteFile(cookieKeyPath, []byte(cookieKey), 0644); err != nil {
|
||
logging.LogErrorf("save cookie key file [%s] failed: %s", cookieKeyPath, err)
|
||
}
|
||
}
|
||
|
||
func initLang() {
|
||
p := filepath.Join(util.WorkingDir, "appearance", "langs")
|
||
dir, err := os.Open(p)
|
||
if err != nil {
|
||
logging.LogErrorf("open language configuration folder [%s] failed: %s", p, err)
|
||
util.ReportFileSysFatalError(err)
|
||
return
|
||
}
|
||
defer dir.Close()
|
||
|
||
langNames, err := dir.Readdirnames(-1)
|
||
if err != nil {
|
||
logging.LogErrorf("list language configuration folder [%s] failed: %s", p, err)
|
||
util.ReportFileSysFatalError(err)
|
||
return
|
||
}
|
||
|
||
for _, langName := range langNames {
|
||
jsonPath := filepath.Join(p, langName)
|
||
data, err := os.ReadFile(jsonPath)
|
||
if err != nil {
|
||
logging.LogErrorf("read language configuration [%s] failed: %s", jsonPath, err)
|
||
continue
|
||
}
|
||
data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
|
||
langMap := map[string]any{}
|
||
if err := gulu.JSON.UnmarshalJSON(data, &langMap); err != nil {
|
||
logging.LogErrorf("parse language configuration failed [%s] failed: %s", jsonPath, err)
|
||
continue
|
||
}
|
||
|
||
kernelMap := map[int]string{}
|
||
label := langMap["_label"].(string)
|
||
kernelLangs := langMap["_kernel"].(map[string]any)
|
||
for k, v := range kernelLangs {
|
||
num, convErr := strconv.Atoi(k)
|
||
if nil != convErr {
|
||
logging.LogErrorf("parse language configuration [%s] item [%d] failed: %s", p, num, convErr)
|
||
continue
|
||
}
|
||
kernelMap[num] = v.(string)
|
||
}
|
||
kernelMap[-1] = label
|
||
name := langName[:strings.LastIndex(langName, ".")]
|
||
util.Langs[name] = kernelMap
|
||
|
||
util.TimeLangs[name] = langMap["_time"].(map[string]any)
|
||
util.TaskActionLangs[name] = langMap["_taskAction"].(map[string]any)
|
||
util.TrayMenuLangs[name] = langMap["_trayMenu"].(map[string]any)
|
||
util.AttrViewLangs[name] = langMap["_attrView"].(map[string]any)
|
||
}
|
||
}
|
||
|
||
func loadLangs() (ret []*conf.Lang) {
|
||
for name, langMap := range util.Langs {
|
||
lang := &conf.Lang{Label: langMap[-1], Name: name}
|
||
ret = append(ret, lang)
|
||
}
|
||
sort.Slice(ret, func(i, j int) bool {
|
||
return ret[i].Name < ret[j].Name
|
||
})
|
||
return
|
||
}
|
||
|
||
var exitLock = sync.Mutex{}
|
||
|
||
// Close 退出内核进程.
|
||
//
|
||
// force:是否不执行同步过程而直接退出
|
||
//
|
||
// setCurrentWorkspace:是否将当前工作空间放到工作空间列表的最后一个
|
||
//
|
||
// execInstallPkg:是否执行新版本安装包
|
||
//
|
||
// 0:默认按照设置项 System.DownloadInstallPkg 检查并推送提示
|
||
// 1:不执行新版本安装
|
||
// 2:执行新版本安装
|
||
//
|
||
// 返回值 exitCode:
|
||
//
|
||
// 0:正常退出
|
||
// 1:同步执行失败
|
||
// 2:提示新安装包
|
||
//
|
||
// 当 force 为 true(强制退出)并且 execInstallPkg 为 0(默认检查更新)并且同步失败并且新版本安装版已经准备就绪时,执行新版本安装 https://github.com/siyuan-note/siyuan/issues/10288
|
||
func Close(force, setCurrentWorkspace bool, execInstallPkg int) (exitCode int) {
|
||
exitLock.Lock()
|
||
defer exitLock.Unlock()
|
||
|
||
logging.LogInfof("exiting kernel [force=%v, setCurrentWorkspace=%v, execInstallPkg=%d]", force, setCurrentWorkspace, execInstallPkg)
|
||
|
||
util.PushMsg(Conf.Language(95), 10000*60)
|
||
FlushTxQueue()
|
||
|
||
if !force {
|
||
// Stop kernel plugins early in shutdown
|
||
if OnKernelPluginShutdown != nil {
|
||
OnKernelPluginShutdown()
|
||
}
|
||
|
||
if Conf.Sync.Enabled && 3 != Conf.Sync.Mode &&
|
||
((IsSubscriber() && conf.ProviderSiYuan == Conf.Sync.Provider) || conf.ProviderSiYuan != Conf.Sync.Provider) {
|
||
syncData(true, false)
|
||
if 0 != ExitSyncSucc {
|
||
exitCode = 1
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
// Close the user guide when exiting https://github.com/siyuan-note/siyuan/issues/10322
|
||
closeUserGuide()
|
||
|
||
// Improve indexing completeness when exiting https://github.com/siyuan-note/siyuan/issues/12039
|
||
sql.FlushQueue()
|
||
|
||
util.IsExiting.Store(true)
|
||
waitSecondForExecInstallPkg := false
|
||
newVerInstallPkgPath := getNewVerInstallPkgPath()
|
||
if !skipNewVerInstallPkg() && "" != newVerInstallPkgPath {
|
||
if 2 == execInstallPkg || (force && 0 == execInstallPkg) { // 执行新版本安装
|
||
waitSecondForExecInstallPkg = true
|
||
if gulu.OS.IsWindows() {
|
||
util.PushMsg(Conf.Language(130), 1000*30)
|
||
}
|
||
go execNewVerInstallPkg(newVerInstallPkgPath)
|
||
} else if 0 == execInstallPkg { // 新版本安装包已经准备就绪
|
||
exitCode = 2
|
||
logging.LogInfof("the new version install pkg is ready [%s], waiting for the user's next instruction", newVerInstallPkgPath)
|
||
return
|
||
}
|
||
}
|
||
|
||
Conf.Close()
|
||
sql.CloseDatabase()
|
||
util.SaveAssetsTexts()
|
||
clearWorkspaceTemp()
|
||
clearCorruptedNotebooks()
|
||
clearPortJSON()
|
||
|
||
if setCurrentWorkspace {
|
||
// 将当前工作空间放到工作空间列表的最后一个
|
||
// Open the last workspace by default https://github.com/siyuan-note/siyuan/issues/10570
|
||
workspacePaths, err := util.ReadWorkspacePaths()
|
||
if err != nil {
|
||
logging.LogErrorf("read workspace paths failed: %s", err)
|
||
} else {
|
||
workspacePaths = gulu.Str.RemoveElem(workspacePaths, util.WorkspaceDir)
|
||
workspacePaths = append(workspacePaths, util.WorkspaceDir)
|
||
util.WriteWorkspacePaths(workspacePaths)
|
||
}
|
||
}
|
||
|
||
util.BroadcastByType("main", "exit", 0, "", nil)
|
||
util.UnlockWorkspace()
|
||
|
||
time.Sleep(500 * time.Millisecond)
|
||
if waitSecondForExecInstallPkg {
|
||
// 桌面端退出拉起更新安装时有时需要重启两次 https://github.com/siyuan-note/siyuan/issues/6544
|
||
// 这里多等待一段时间,等待安装程序启动
|
||
if gulu.OS.IsWindows() {
|
||
time.Sleep(30 * time.Second)
|
||
}
|
||
}
|
||
|
||
closeSyncWebSocket()
|
||
|
||
go func() {
|
||
time.Sleep(500 * time.Millisecond)
|
||
logging.LogInfof("exited kernel")
|
||
if nil != util.WebSocketServer {
|
||
util.WebSocketServer.Close()
|
||
}
|
||
if nil != util.HttpServer {
|
||
util.HttpServer.Close()
|
||
}
|
||
util.HttpServing = false
|
||
|
||
if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
|
||
return
|
||
}
|
||
|
||
os.Exit(logging.ExitCodeOk)
|
||
}()
|
||
return
|
||
}
|
||
|
||
var customEmojis = sync.Map{}
|
||
|
||
func AddCustomEmoji(emojiName, imgSrc string) {
|
||
customEmojis.Store(emojiName, imgSrc)
|
||
}
|
||
|
||
func ClearCustomEmojis() {
|
||
customEmojis.Clear()
|
||
}
|
||
|
||
func NewLute() (ret *lute.Lute) {
|
||
ret = util.NewLute()
|
||
ret.SetCodeSyntaxHighlightLineNum(Conf.Editor.CodeSyntaxHighlightLineNum)
|
||
ret.SetChineseParagraphBeginningSpace(Conf.Export.ParagraphBeginningSpace)
|
||
ret.SetProtyleMarkNetImg(Conf.Editor.DisplayNetImgMark)
|
||
ret.SetSpellcheck(Conf.Editor.Spellcheck)
|
||
|
||
customEmojiMap := map[string]string{}
|
||
customEmojis.Range(func(key, value any) bool {
|
||
customEmojiMap[key.(string)] = value.(string)
|
||
return true
|
||
})
|
||
ret.PutEmojis(customEmojiMap)
|
||
return
|
||
}
|
||
|
||
func enableLuteInlineSyntax(luteEngine *lute.Lute) {
|
||
luteEngine.SetInlineAsterisk(true)
|
||
luteEngine.SetInlineUnderscore(true)
|
||
luteEngine.SetSup(true)
|
||
luteEngine.SetSub(true)
|
||
luteEngine.SetTag(true)
|
||
luteEngine.SetInlineMath(true)
|
||
luteEngine.SetGFMStrikethrough(true)
|
||
}
|
||
|
||
func (conf *AppConf) Save() {
|
||
if util.ReadOnly {
|
||
return
|
||
}
|
||
|
||
Conf.m.Lock()
|
||
defer Conf.m.Unlock()
|
||
|
||
newData, _ := gulu.JSON.MarshalIndentJSON(Conf, "", " ")
|
||
confPath := filepath.Join(util.ConfDir, "conf.json")
|
||
oldData, err := filelock.ReadFile(confPath)
|
||
if err != nil {
|
||
conf.save0(newData)
|
||
return
|
||
}
|
||
|
||
if bytes.Equal(newData, oldData) {
|
||
return
|
||
}
|
||
|
||
conf.save0(newData)
|
||
}
|
||
|
||
func (conf *AppConf) save0(data []byte) {
|
||
confPath := filepath.Join(util.ConfDir, "conf.json")
|
||
if err := filelock.WriteFile(confPath, data); err != nil {
|
||
logging.LogErrorf("write conf [%s] failed: %s", confPath, err)
|
||
util.ReportFileSysFatalError(err)
|
||
return
|
||
}
|
||
}
|
||
|
||
func (conf *AppConf) Close() {
|
||
conf.Save()
|
||
}
|
||
|
||
func (conf *AppConf) Box(boxID string) *Box {
|
||
for _, box := range conf.GetOpenedBoxes() {
|
||
if box.ID == boxID {
|
||
return box
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (conf *AppConf) GetBox(boxID string) *Box {
|
||
for _, box := range conf.GetBoxes() {
|
||
if box.ID == boxID {
|
||
return box
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (conf *AppConf) BoxNames(boxIDs []string) (ret map[string]string) {
|
||
ret = map[string]string{}
|
||
|
||
boxes := conf.GetOpenedBoxes()
|
||
for _, boxID := range boxIDs {
|
||
for _, box := range boxes {
|
||
if box.ID == boxID {
|
||
ret[boxID] = box.Name
|
||
break
|
||
}
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
func (conf *AppConf) GetBoxes() (ret []*Box) {
|
||
ret = []*Box{}
|
||
notebooks, err := ListNotebooks()
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
for _, notebook := range notebooks {
|
||
id := notebook.ID
|
||
name := notebook.Name
|
||
closed := notebook.Closed
|
||
box := &Box{ID: id, Name: name, Closed: closed}
|
||
ret = append(ret, box)
|
||
}
|
||
return
|
||
}
|
||
|
||
func (conf *AppConf) GetOpenedBoxes() (ret []*Box) {
|
||
ret = []*Box{}
|
||
notebooks, err := ListNotebooks()
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
for _, notebook := range notebooks {
|
||
if !notebook.Closed {
|
||
ret = append(ret, notebook)
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
func (conf *AppConf) GetClosedBoxes() (ret []*Box) {
|
||
ret = []*Box{}
|
||
notebooks, err := ListNotebooks()
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
for _, notebook := range notebooks {
|
||
if notebook.Closed {
|
||
ret = append(ret, notebook)
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
func (conf *AppConf) Language(num int) (ret string) {
|
||
ret = conf.language(num)
|
||
ret = strings.ReplaceAll(ret, "${accountServer}", util.GetCloudAccountServer())
|
||
return
|
||
}
|
||
|
||
func (conf *AppConf) language(num int) (ret string) {
|
||
ret = util.Langs[conf.Lang][num]
|
||
if "" != ret {
|
||
return
|
||
}
|
||
ret = util.Langs["en_US"][num]
|
||
return
|
||
}
|
||
|
||
func InitBoxes() {
|
||
blockCount := treenode.CountBlocks()
|
||
initialized := 0 < blockCount
|
||
for _, box := range Conf.GetOpenedBoxes() {
|
||
box.UpdateHistoryGenerated() // 初始化历史生成时间为当前时间
|
||
|
||
if !initialized {
|
||
indexBox(box.ID)
|
||
}
|
||
}
|
||
|
||
logging.LogInfof("tree/block count [%d/%d]", treenode.CountTrees(), blockCount)
|
||
}
|
||
|
||
func IsSubscriber() bool {
|
||
u := Conf.GetUser()
|
||
return nil != u && (-1 == u.UserSiYuanProExpireTime || 0 < u.UserSiYuanProExpireTime) && 0 == u.UserSiYuanSubscriptionStatus
|
||
}
|
||
|
||
func IsPaidUser() bool {
|
||
if IsSubscriber() {
|
||
return true
|
||
}
|
||
|
||
u := Conf.GetUser()
|
||
if nil == u {
|
||
return false
|
||
}
|
||
return 1 == u.UserSiYuanOneTimePayStatus
|
||
}
|
||
|
||
const (
|
||
MaskedUserData = ""
|
||
MaskedAccessAuthCode = "*******"
|
||
)
|
||
|
||
// GetMaskedConf 获取脱敏后的 Conf
|
||
func GetMaskedConf() (ret *AppConf, err error) {
|
||
// 序列化时持锁,避免与 loadThemes/LoadIcons 等写操作并发导致 slice 在编码过程中被改写而 panic https://github.com/siyuan-note/siyuan/issues/16978
|
||
Conf.m.Lock()
|
||
data, err := gulu.JSON.MarshalJSON(Conf)
|
||
Conf.m.Unlock()
|
||
if err != nil {
|
||
logging.LogErrorf("marshal conf failed: %s", err)
|
||
return
|
||
}
|
||
ret = &AppConf{}
|
||
if err = gulu.JSON.UnmarshalJSON(data, ret); err != nil {
|
||
logging.LogErrorf("unmarshal conf failed: %s", err)
|
||
return
|
||
}
|
||
|
||
ret.UserData = MaskedUserData
|
||
if "" != ret.AccessAuthCode {
|
||
ret.AccessAuthCode = MaskedAccessAuthCode
|
||
}
|
||
return
|
||
}
|
||
|
||
// HideConfSecret 隐藏设置中的秘密信息
|
||
// REF: https://github.com/siyuan-note/siyuan/issues/11364
|
||
func HideConfSecret(c *AppConf) {
|
||
c.AI = &conf.AI{}
|
||
c.Api = &conf.API{}
|
||
c.Flashcard = &conf.Flashcard{}
|
||
c.ServerAddrs = []string{}
|
||
c.Publish = &conf.Publish{}
|
||
c.Repo = &conf.Repo{}
|
||
c.Sync = &conf.Sync{}
|
||
c.System.AppDir = ""
|
||
c.System.ConfDir = ""
|
||
c.System.DataDir = ""
|
||
c.System.HomeDir = ""
|
||
c.System.Name = ""
|
||
c.System.NetworkProxy = &conf.NetworkProxy{}
|
||
}
|
||
|
||
func clearPortJSON() {
|
||
pid := fmt.Sprintf("%d", os.Getpid())
|
||
portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
|
||
pidPorts := map[string]string{}
|
||
var data []byte
|
||
var err error
|
||
|
||
if gulu.File.IsExist(portJSON) {
|
||
data, err = os.ReadFile(portJSON)
|
||
if err != nil {
|
||
logging.LogWarnf("read port.json failed: %s", err)
|
||
} else {
|
||
if err = gulu.JSON.UnmarshalJSON(data, &pidPorts); err != nil {
|
||
logging.LogWarnf("unmarshal port.json failed: %s", err)
|
||
}
|
||
}
|
||
}
|
||
|
||
delete(pidPorts, pid)
|
||
if data, err = gulu.JSON.MarshalIndentJSON(pidPorts, "", " "); err != nil {
|
||
logging.LogWarnf("marshal port.json failed: %s", err)
|
||
} else {
|
||
if err = os.WriteFile(portJSON, data, 0644); err != nil {
|
||
logging.LogWarnf("write port.json failed: %s", err)
|
||
}
|
||
}
|
||
}
|
||
|
||
func clearCorruptedNotebooks() {
|
||
// 数据同步时展开文档树操作可能导致数据丢失 https://github.com/siyuan-note/siyuan/issues/7129
|
||
|
||
dirs, err := os.ReadDir(util.DataDir)
|
||
if err != nil {
|
||
logging.LogErrorf("read dir [%s] failed: %s", util.DataDir, err)
|
||
return
|
||
}
|
||
for _, dir := range dirs {
|
||
if util.IsReservedFilename(dir.Name()) {
|
||
continue
|
||
}
|
||
|
||
if !dir.IsDir() {
|
||
continue
|
||
}
|
||
|
||
if !ast.IsNodeIDPattern(dir.Name()) {
|
||
continue
|
||
}
|
||
|
||
boxDirPath := filepath.Join(util.DataDir, dir.Name())
|
||
boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json")
|
||
if !filelock.IsExist(boxConfPath) {
|
||
logging.LogWarnf("found a corrupted box [%s]", boxDirPath)
|
||
continue
|
||
}
|
||
}
|
||
}
|
||
|
||
func clearWorkspaceTemp() {
|
||
os.RemoveAll(filepath.Join(util.TempDir, "bazaar"))
|
||
os.RemoveAll(filepath.Join(util.TempDir, "export"))
|
||
os.RemoveAll(filepath.Join(util.TempDir, "import"))
|
||
os.RemoveAll(filepath.Join(util.TempDir, "convert"))
|
||
os.RemoveAll(filepath.Join(util.TempDir, "repo"))
|
||
os.RemoveAll(filepath.Join(util.TempDir, "os"))
|
||
os.RemoveAll(filepath.Join(util.TempDir, "base64"))
|
||
|
||
// 退出时自动删除超过 7 天的安装包 https://github.com/siyuan-note/siyuan/issues/6128
|
||
install := filepath.Join(util.TempDir, "install")
|
||
if gulu.File.IsDir(install) {
|
||
monthAgo := time.Now().Add(-time.Hour * 24 * 7)
|
||
entries, err := os.ReadDir(install)
|
||
if err != nil {
|
||
logging.LogErrorf("read dir [%s] failed: %s", install, err)
|
||
} else {
|
||
for _, entry := range entries {
|
||
info, _ := entry.Info()
|
||
if nil != info && !info.IsDir() && info.ModTime().Before(monthAgo) {
|
||
if err = os.RemoveAll(filepath.Join(install, entry.Name())); err != nil {
|
||
logging.LogErrorf("remove old install pkg [%s] failed: %s", filepath.Join(install, entry.Name()), err)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
tmps, err := filepath.Glob(filepath.Join(util.TempDir, "*.tmp"))
|
||
if err != nil {
|
||
logging.LogErrorf("glob temp files failed: %s", err)
|
||
}
|
||
for _, tmp := range tmps {
|
||
if err = os.RemoveAll(tmp); err != nil {
|
||
logging.LogErrorf("remove temp file [%s] failed: %s", tmp, err)
|
||
} else {
|
||
logging.LogInfof("removed temp file [%s]", tmp)
|
||
}
|
||
}
|
||
|
||
tmps, err = filepath.Glob(filepath.Join(util.DataDir, ".siyuan", "*.tmp"))
|
||
if err != nil {
|
||
logging.LogErrorf("glob temp files failed: %s", err)
|
||
}
|
||
for _, tmp := range tmps {
|
||
if err = os.RemoveAll(tmp); err != nil {
|
||
logging.LogErrorf("remove temp file [%s] failed: %s", tmp, err)
|
||
} else {
|
||
logging.LogInfof("removed temp file [%s]", tmp)
|
||
}
|
||
}
|
||
|
||
// 老版本遗留文件清理
|
||
os.RemoveAll(filepath.Join(util.DataDir, "assets", ".siyuan", "assets.json"))
|
||
os.RemoveAll(filepath.Join(util.DataDir, ".siyuan", "history"))
|
||
os.RemoveAll(filepath.Join(util.WorkspaceDir, "backup"))
|
||
os.RemoveAll(filepath.Join(util.WorkspaceDir, "sync"))
|
||
os.RemoveAll(filepath.Join(util.TempDir, "blocktree.msgpack")) // v2.7.2 前旧版的块树数据
|
||
os.RemoveAll(filepath.Join(util.DataDir, "%")) // v3.0.6 生成的错误历史文件夹
|
||
os.RemoveAll(filepath.Join(util.TempDir, "blocktree")) // v3.1.0 前旧版的块树数据
|
||
|
||
logging.LogInfof("cleared workspace temp")
|
||
}
|
||
|
||
func closeUserGuide() {
|
||
defer logging.Recover()
|
||
|
||
dirs, err := os.ReadDir(util.DataDir)
|
||
if err != nil {
|
||
logging.LogErrorf("read dir [%s] failed: %s", util.DataDir, err)
|
||
return
|
||
}
|
||
|
||
for _, dir := range dirs {
|
||
if !IsUserGuide(dir.Name()) {
|
||
continue
|
||
}
|
||
|
||
boxID := dir.Name()
|
||
boxDirPath := filepath.Join(util.DataDir, boxID)
|
||
boxConf := conf.NewBoxConf()
|
||
boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json")
|
||
if !filelock.IsExist(boxConfPath) {
|
||
logging.LogWarnf("found a corrupted user guide box [%s]", boxDirPath)
|
||
if removeErr := filelock.Remove(boxDirPath); nil != removeErr {
|
||
logging.LogErrorf("remove corrupted user guide box [%s] failed: %s", boxDirPath, removeErr)
|
||
} else {
|
||
logging.LogInfof("removed corrupted user guide box [%s]", boxDirPath)
|
||
}
|
||
continue
|
||
}
|
||
|
||
data, readErr := filelock.ReadFile(boxConfPath)
|
||
if nil != readErr {
|
||
logging.LogErrorf("read box conf [%s] failed: %s", boxConfPath, readErr)
|
||
if removeErr := filelock.Remove(boxDirPath); nil != removeErr {
|
||
logging.LogErrorf("remove corrupted user guide box [%s] failed: %s", boxDirPath, removeErr)
|
||
} else {
|
||
logging.LogInfof("removed corrupted user guide box [%s]", boxDirPath)
|
||
}
|
||
continue
|
||
}
|
||
if readErr = gulu.JSON.UnmarshalJSON(data, boxConf); nil != readErr {
|
||
logging.LogErrorf("parse box conf [%s] failed: %s", boxConfPath, readErr)
|
||
if removeErr := filelock.Remove(boxDirPath); nil != removeErr {
|
||
logging.LogErrorf("remove corrupted user guide box [%s] failed: %s", boxDirPath, removeErr)
|
||
} else {
|
||
logging.LogInfof("removed corrupted user guide box [%s]", boxDirPath)
|
||
}
|
||
continue
|
||
}
|
||
|
||
if boxConf.Closed {
|
||
continue
|
||
}
|
||
|
||
msgId := util.PushMsg(Conf.language(233), 30000)
|
||
|
||
unindex(boxID)
|
||
|
||
sql.FlushQueue()
|
||
|
||
if removeErr := RemoveBox(boxID); nil == removeErr {
|
||
evt := util.NewCmdResult("removeBox", 0, util.PushModeBroadcast)
|
||
evt.Data = map[string]any{
|
||
"box": boxID,
|
||
}
|
||
util.PushEvent(evt)
|
||
} else {
|
||
logging.LogErrorf("close user guide box [%s] failed: %s", boxID, removeErr)
|
||
util.PushClearMsg(msgId)
|
||
continue
|
||
}
|
||
|
||
util.PushClearMsg(msgId)
|
||
logging.LogInfof("closed user guide box [%s]", boxID)
|
||
}
|
||
}
|
||
|
||
func init() {
|
||
subscribeConfEvents()
|
||
}
|
||
|
||
func subscribeConfEvents() {
|
||
eventbus.Subscribe(util.EvtConfPandocInitialized, func() {
|
||
logging.LogInfof("pandoc initialized, set pandoc bin to [%s]", util.PandocBinPath)
|
||
Conf.Export.PandocBin = util.PandocBinPath
|
||
|
||
params := util.RemoveInvalid(Conf.Export.PandocParams)
|
||
if !strings.Contains(params, "--reference-doc") && "" != util.PandocTemplatePath && !Conf.System.IsMicrosoftStore {
|
||
params += " --reference-doc"
|
||
params += " \"" + util.PandocTemplatePath + "\""
|
||
Conf.Export.PandocParams = strings.TrimSpace(params)
|
||
}
|
||
|
||
logging.LogInfof("pandoc params set to [%s]", Conf.Export.PandocParams)
|
||
logging.LogInfof("pandoc resources [%s, %s]", util.PandocTemplatePath, util.PandocColorFilterPath)
|
||
Conf.Save()
|
||
})
|
||
}
|