Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Add API for creating asset history

Daniel9738f29d2998
diff --git a/kernel/api/history.go b/kernel/api/history.go
index 0fee9dad23..8c80185298 100644
--- a/kernel/api/history.go
+++ b/kernel/api/history.go
@@ -285,3 +285,27 @@ func createDocHistory(c *gin.Context) {
return
}
}
+
+func createAssetHistory(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ var assetPath string
+ if !util.ParseJsonArgs(arg, ret,
+ util.BindJsonArg("path", &assetPath, true, true),
+ ) {
+ return
+ }
+
+ err := model.CreateAssetHistory(assetPath)
+ if err != nil {
+ ret.Code = -1
+ ret.Msg = err.Error()
+ return
+ }
+}
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 1e7fa1396d..4f76206e3b 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -184,6 +184,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/history/searchHistory", model.CheckAuth, model.CheckAdminRole, searchHistory)
ginServer.Handle("POST", "/api/history/getHistoryItems", model.CheckAuth, model.CheckAdminRole, getHistoryItems)
ginServer.Handle("POST", "/api/history/createDocHistory", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, createDocHistory)
+ ginServer.Handle("POST", "/api/history/createAssetHistory", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, createAssetHistory)
ginServer.Handle("POST", "/api/outline/getDocOutline", model.CheckAuth, getDocOutline)
diff --git a/kernel/model/history.go b/kernel/model/history.go
index e5e5a9aead..cba86357f1 100644
--- a/kernel/model/history.go
+++ b/kernel/model/history.go
@@ -774,23 +774,51 @@ func generateAssetsHistory() {
if 1 > len(assets) {
return
}
+ if err := createAssetsHistory(assets); err != nil {
+ logging.LogErrorf("generate assets history failed: %s", err)
+ }
+}
+
+// CreateAssetHistory 为指定资源文件创建历史快照。
+func CreateAssetHistory(assetPath string) (err error) {
+ assetPath = strings.TrimPrefix(filepath.ToSlash(filepath.Clean(filepath.FromSlash(assetPath))), "/")
+ if !strings.HasPrefix(assetPath, "assets/") {
+ return errors.New("asset path must be under assets")
+ }
+ assetAbsPath := filepath.Join(util.DataDir, filepath.FromSlash(assetPath))
+ assetsDir := filepath.Join(util.DataDir, "assets")
+ if !gulu.File.IsSubPath(assetsDir, assetAbsPath) {
+ return errors.New("asset path must be under assets")
+ }
+ info, statErr := os.Stat(assetAbsPath)
+ if statErr != nil {
+ return statErr
+ }
+ if info.IsDir() {
+ return errors.New("asset path must be a file")
+ }
+ return createAssetsHistory([]string{assetAbsPath})
+}
+
+func createAssetsHistory(assets []string) (err error) {
historyDir, err := getHistoryDir(HistoryOpUpdate)
if err != nil {
- logging.LogErrorf("get history dir failed: %s", err)
- return
+ return fmt.Errorf("get history directory failed: %w", err)
}
for _, file := range assets {
- historyPath := filepath.Join(historyDir, "assets", strings.TrimPrefix(file, filepath.Join(util.DataDir, "assets")))
+ assetRelPath, relErr := filepath.Rel(filepath.Join(util.DataDir, "assets"), file)
+ if relErr != nil || assetRelPath == "." || strings.HasPrefix(assetRelPath, ".."+string(filepath.Separator)) {
+ return errors.New("asset path must be under assets")
+ }
+ historyPath := filepath.Join(historyDir, "assets", assetRelPath)
if err = os.MkdirAll(filepath.Dir(historyPath), 0755); err != nil {
- logging.LogErrorf("generate history failed: %s", err)
- return
+ return fmt.Errorf("create history directory [%s] failed: %w", filepath.Dir(historyPath), err)
}
if err = filelock.Copy(file, historyPath); err != nil {
- logging.LogErrorf("copy file [%s] to [%s] failed: %s", file, historyPath, err)
- return
+ return fmt.Errorf("copy asset [%s] to [%s] failed: %w", file, historyPath, err)
}
}