Skip to content

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

:bug: Clear legacy Pandoc temporary files https://github.com/siyuan-note/siyuan/issues/18437

Daniel61c9fc87fcde
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 38203412fe..b83ef3da6e 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -805,35 +805,15 @@ func ClearTempFiles() {
util.PushUpdateMsg(msgId, msg, 7000)
}()
- bazaarTmp := filepath.Join(util.TempDir, "bazaar")
- clearTempDir(bazaarTmp, &count, &size)
-
- exportTmp := filepath.Join(util.TempDir, "export")
- clearTempDir(exportTmp, &count, &size)
-
- importTmp := filepath.Join(util.TempDir, "import")
- clearTempDir(importTmp, &count, &size)
-
- convertTmp := filepath.Join(util.TempDir, "convert")
- clearTempDir(convertTmp, &count, &size)
-
- osTmp := filepath.Join(util.TempDir, "os")
- clearTempDir(osTmp, &count, &size)
-
- base64Tmp := filepath.Join(util.TempDir, "base64")
- clearTempDir(base64Tmp, &count, &size)
-
- installTmp := filepath.Join(util.TempDir, "install")
- clearTempDir(installTmp, &count, &size)
-
- thumbnailsTmp := filepath.Join(util.TempDir, "thumbnails")
- clearTempDir(thumbnailsTmp, &count, &size)
-
- repoTmp := filepath.Join(util.TempDir, "repo")
- clearTempDir(repoTmp, &count, &size)
+ clearTempFiles(&count, &size)
+}
- clipboardTmp := filepath.Join(util.TempDir, "clipboard")
- clearTempDir(clipboardTmp, &count, &size)
+func clearTempFiles(count *int, size *int64) {
+ for _, name := range []string{
+ "bazaar", "export", "import", "convert", "pandoc", "os", "base64", "install", "thumbnails", "repo", "clipboard",
+ } {
+ clearTempDir(filepath.Join(util.TempDir, name), count, size)
+ }
}
func clearTempDir(dir string, count *int, size *int64) {
diff --git a/kernel/model/box_temp_test.go b/kernel/model/box_temp_test.go
new file mode 100644
index 0000000000..6c1ed5166a
--- /dev/null
+++ b/kernel/model/box_temp_test.go
@@ -0,0 +1,79 @@
+// 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 (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestClearTempFilesRemovesLegacyPandoc(t *testing.T) {
+ originalTempDir := util.TempDir
+ t.Cleanup(func() {
+ util.TempDir = originalTempDir
+ })
+
+ util.TempDir = filepath.Join(t.TempDir(), "temp")
+ pandocDir := filepath.Join(util.TempDir, "pandoc")
+ pandocBin := filepath.Join(pandocDir, "bin", "pandoc")
+ if err := os.MkdirAll(filepath.Dir(pandocBin), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(pandocBin, []byte("pandoc"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ var count int
+ var size int64
+ clearTempFiles(&count, &size)
+
+ if _, err := os.Stat(pandocDir); !os.IsNotExist(err) {
+ t.Fatalf("legacy Pandoc temporary directory was not removed: %v", err)
+ }
+ if 1 != count || 6 != size {
+ t.Fatalf("unexpected cleanup statistics: count=%d, size=%d", count, size)
+ }
+}
+
+func TestClearWorkspaceTempRemovesLegacyPandoc(t *testing.T) {
+ originalDataDir, originalTempDir, originalWorkspaceDir := util.DataDir, util.TempDir, util.WorkspaceDir
+ t.Cleanup(func() {
+ util.DataDir, util.TempDir, util.WorkspaceDir = originalDataDir, originalTempDir, originalWorkspaceDir
+ })
+
+ workspaceDir := t.TempDir()
+ util.DataDir = filepath.Join(workspaceDir, "data")
+ util.TempDir = filepath.Join(workspaceDir, "temp")
+ util.WorkspaceDir = workspaceDir
+ pandocDir := filepath.Join(util.TempDir, "pandoc")
+ pandocBin := filepath.Join(pandocDir, "bin", "pandoc")
+ if err := os.MkdirAll(filepath.Dir(pandocBin), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(pandocBin, []byte("pandoc"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ clearWorkspaceTemp(false)
+
+ if _, err := os.Stat(pandocDir); !os.IsNotExist(err) {
+ t.Fatalf("legacy Pandoc temporary directory was not removed on exit: %v", err)
+ }
+}
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 9602624279..7ecdf83882 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -1461,6 +1461,7 @@ func clearWorkspaceTemp(preserveInstallPkgs bool) {
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, "pandoc"))
os.RemoveAll(filepath.Join(util.TempDir, "repo"))
ClearRichClipboard()
os.RemoveAll(filepath.Join(util.TempDir, "os"))