2023-06-24 20:39:55 +08:00
|
|
|
// SiYuan - Refactor your thinking
|
2022-05-26 15:18:53 +08:00
|
|
|
// 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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-03-29 20:57:56 +08:00
|
|
|
"sync"
|
2022-05-26 15:18:53 +08:00
|
|
|
"time"
|
2022-10-22 17:09:59 +08:00
|
|
|
"unicode/utf8"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
|
|
|
"github.com/88250/lute/ast"
|
2022-06-15 23:56:47 +08:00
|
|
|
"github.com/siyuan-note/filelock"
|
2022-07-17 12:22:32 +08:00
|
|
|
"github.com/siyuan-note/logging"
|
2024-09-05 16:41:50 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/task"
|
2022-05-26 15:18:53 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateBox(name string) (id string, err error) {
|
2024-11-27 20:13:22 +08:00
|
|
|
name = util.RemoveInvalid(name)
|
2022-10-22 17:09:59 +08:00
|
|
|
if 512 < utf8.RuneCountInString(name) {
|
|
|
|
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
|
|
|
|
err = errors.New(Conf.Language(106))
|
|
|
|
return
|
|
|
|
}
|
2024-03-24 22:15:19 +08:00
|
|
|
if "" == name {
|
|
|
|
name = Conf.language(105)
|
|
|
|
}
|
2022-10-22 17:09:59 +08:00
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
FlushTxQueue()
|
2023-12-19 20:03:13 +08:00
|
|
|
|
|
|
|
createDocLock.Lock()
|
|
|
|
defer createDocLock.Unlock()
|
|
|
|
|
2025-08-29 16:11:43 +08:00
|
|
|
boxes, _ := ListNotebooks()
|
|
|
|
for i, b := range boxes {
|
|
|
|
c := b.GetConf()
|
|
|
|
c.Sort = i + 1
|
|
|
|
b.SaveConf(c)
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
id = ast.NewNodeID()
|
|
|
|
boxLocalPath := filepath.Join(util.DataDir, id)
|
|
|
|
err = os.MkdirAll(boxLocalPath, 0755)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
box := &Box{ID: id, Name: name}
|
|
|
|
boxConf := box.GetConf()
|
|
|
|
boxConf.Name = name
|
|
|
|
box.SaveConf(boxConf)
|
2022-07-14 21:50:46 +08:00
|
|
|
IncSync()
|
2024-04-15 09:09:45 +08:00
|
|
|
logging.LogInfof("created box [%s]", id)
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func RenameBox(boxID, name string) (err error) {
|
|
|
|
box := Conf.Box(boxID)
|
|
|
|
if nil == box {
|
|
|
|
return errors.New(Conf.Language(0))
|
|
|
|
}
|
|
|
|
|
2024-03-24 22:15:19 +08:00
|
|
|
if 512 < utf8.RuneCountInString(name) {
|
2024-03-24 22:15:53 +08:00
|
|
|
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
|
2024-03-24 22:15:19 +08:00
|
|
|
err = errors.New(Conf.Language(106))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if "" == name {
|
|
|
|
name = Conf.language(105)
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
boxConf := box.GetConf()
|
|
|
|
boxConf.Name = name
|
|
|
|
box.Name = name
|
|
|
|
box.SaveConf(boxConf)
|
2022-07-14 21:50:46 +08:00
|
|
|
IncSync()
|
2024-04-15 09:09:45 +08:00
|
|
|
logging.LogInfof("renamed box [%s] to [%s]", boxID, name)
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-29 20:57:56 +08:00
|
|
|
var boxLock = sync.Map{}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
func RemoveBox(boxID string) (err error) {
|
2024-03-29 20:57:56 +08:00
|
|
|
if _, ok := boxLock.Load(boxID); ok {
|
|
|
|
err = fmt.Errorf(Conf.language(239))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
boxLock.Store(boxID, true)
|
|
|
|
defer boxLock.Delete(boxID)
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
if util.IsReservedFilename(boxID) {
|
|
|
|
return errors.New(fmt.Sprintf("can not remove [%s] caused by it is a reserved file", boxID))
|
|
|
|
}
|
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
FlushTxQueue()
|
2024-03-29 20:57:56 +08:00
|
|
|
isUserGuide := IsUserGuide(boxID)
|
2023-12-19 20:03:13 +08:00
|
|
|
createDocLock.Lock()
|
|
|
|
defer createDocLock.Unlock()
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
localPath := filepath.Join(util.DataDir, boxID)
|
2023-11-06 22:13:04 +08:00
|
|
|
if !filelock.IsExist(localPath) {
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !gulu.File.IsDir(localPath) {
|
|
|
|
return errors.New(fmt.Sprintf("can not remove [%s] caused by it is not a dir", boxID))
|
|
|
|
}
|
|
|
|
|
2024-03-29 20:57:56 +08:00
|
|
|
if !isUserGuide {
|
2022-05-26 15:18:53 +08:00
|
|
|
var historyDir string
|
2022-08-23 11:30:51 +08:00
|
|
|
historyDir, err = GetHistoryDir(HistoryOpDelete)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-07-17 12:22:32 +08:00
|
|
|
logging.LogErrorf("get history dir failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p := strings.TrimPrefix(localPath, util.DataDir)
|
|
|
|
historyPath := filepath.Join(historyDir, p)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = filelock.Copy(localPath, historyPath); err != nil {
|
2022-07-17 12:22:32 +08:00
|
|
|
logging.LogErrorf("gen sync history failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
copyBoxAssetsToDataAssets(boxID)
|
|
|
|
}
|
|
|
|
|
|
|
|
unmount0(boxID)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = filelock.Remove(localPath); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
2022-07-14 21:50:46 +08:00
|
|
|
IncSync()
|
2024-04-15 09:09:45 +08:00
|
|
|
|
|
|
|
logging.LogInfof("removed box [%s]", boxID)
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func Unmount(boxID string) {
|
2024-10-22 19:20:44 +08:00
|
|
|
FlushTxQueue()
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
unmount0(boxID)
|
2023-01-01 14:09:36 +08:00
|
|
|
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
|
2022-05-26 15:18:53 +08:00
|
|
|
evt.Data = map[string]interface{}{
|
|
|
|
"box": boxID,
|
|
|
|
}
|
|
|
|
util.PushEvent(evt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmount0(boxID string) {
|
2023-01-15 14:00:01 +08:00
|
|
|
box := Conf.Box(boxID)
|
|
|
|
if nil == box {
|
|
|
|
return
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2023-01-15 14:00:01 +08:00
|
|
|
|
|
|
|
boxConf := box.GetConf()
|
|
|
|
boxConf.Closed = true
|
|
|
|
box.SaveConf(boxConf)
|
|
|
|
box.Unindex()
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Mount(boxID string) (alreadyMount bool, err error) {
|
2024-03-29 20:57:56 +08:00
|
|
|
if _, ok := boxLock.Load(boxID); ok {
|
|
|
|
err = fmt.Errorf(Conf.language(239))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
boxLock.Store(boxID, true)
|
|
|
|
defer boxLock.Delete(boxID)
|
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
FlushTxQueue()
|
2024-03-29 20:57:56 +08:00
|
|
|
isUserGuide := IsUserGuide(boxID)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
localPath := filepath.Join(util.DataDir, boxID)
|
|
|
|
var reMountGuide bool
|
2024-03-29 20:57:56 +08:00
|
|
|
if isUserGuide {
|
2022-05-26 15:18:53 +08:00
|
|
|
// 重新挂载帮助文档
|
|
|
|
|
|
|
|
guideBox := Conf.Box(boxID)
|
|
|
|
if nil != guideBox {
|
|
|
|
unmount0(guideBox.ID)
|
|
|
|
reMountGuide = true
|
|
|
|
}
|
|
|
|
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = filelock.Remove(localPath); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-09-07 09:35:48 +08:00
|
|
|
boxes, _ := ListNotebooks()
|
|
|
|
var sort int
|
|
|
|
if len(boxes) > 0 {
|
|
|
|
sort = boxes[0].Sort - 1
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
p := filepath.Join(util.WorkingDir, "guide", boxID)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = filelock.Copy(p, localPath); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:11:20 +08:00
|
|
|
avDirPath := filepath.Join(util.WorkingDir, "guide", boxID, "storage", "av")
|
2024-02-08 17:14:51 +08:00
|
|
|
if filelock.IsExist(avDirPath) {
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = filelock.Copy(avDirPath, filepath.Join(util.DataDir, "storage", "av")); err != nil {
|
2024-02-08 17:14:51 +08:00
|
|
|
return
|
|
|
|
}
|
2024-02-08 17:11:20 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
if box := Conf.Box(boxID); nil != box {
|
|
|
|
boxConf := box.GetConf()
|
|
|
|
boxConf.Closed = true
|
2025-09-07 09:35:48 +08:00
|
|
|
boxConf.Sort = sort
|
2022-05-26 15:18:53 +08:00
|
|
|
box.SaveConf(boxConf)
|
|
|
|
}
|
|
|
|
|
2023-02-24 12:49:49 +08:00
|
|
|
if Conf.OpenHelp {
|
|
|
|
Conf.OpenHelp = false
|
2022-05-26 15:18:53 +08:00
|
|
|
Conf.Save()
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:07:04 +08:00
|
|
|
task.AppendAsyncTaskWithDelay(task.PushMsg, 3*time.Second, util.PushErrMsg, Conf.Language(52), 7000)
|
2022-05-26 15:18:53 +08:00
|
|
|
go func() {
|
2022-05-31 10:53:49 +08:00
|
|
|
// 每次打开帮助文档时自动检查版本更新并提醒 https://github.com/siyuan-note/siyuan/issues/5057
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
CheckUpdate(true)
|
2022-05-26 15:18:53 +08:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gulu.File.IsDir(localPath) {
|
|
|
|
return false, errors.New("can not open file, just support open folder only")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, box := range Conf.GetOpenedBoxes() {
|
|
|
|
if box.ID == boxID {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box := &Box{ID: boxID}
|
|
|
|
boxConf := box.GetConf()
|
|
|
|
boxConf.Closed = false
|
|
|
|
box.SaveConf(boxConf)
|
|
|
|
|
2023-01-23 11:48:35 +08:00
|
|
|
box.Index()
|
2022-05-26 15:18:53 +08:00
|
|
|
// 缓存根一级的文档树展开
|
2023-09-05 16:42:15 +08:00
|
|
|
ListDocTree(box.ID, "/", util.SortModeUnassigned, false, false, Conf.FileTree.MaxListCount)
|
2022-05-26 15:18:53 +08:00
|
|
|
util.ClearPushProgress(100)
|
2023-01-24 13:21:23 +08:00
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
if reMountGuide {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-08-08 13:58:26 +08:00
|
|
|
func IsUserGuide(boxID string) bool {
|
2024-05-31 11:05:57 +09:00
|
|
|
return "20210808180117-czj9bvb" == boxID || "20210808180117-6v0mkxr" == boxID || "20211226090932-5lcq56f" == boxID || "20240530133126-axarxgx" == boxID
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|