fix(export): exclude application data from portable notes
This commit is contained in:
1 parent
9449a1aa65
commit
c705d1ab33
5 files changed
+159
-4
No files matched your search
@@ -17,7 +17,8 @@ type MissingAsset struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func (v *Vault) ExportNotes(ctx context.Context) (result *os.File, err error) {
|
||||
// exclude 由应用层提供,笔记库不持有数据库和资源目录的配置。
|
||||
func (v *Vault) ExportNotes(ctx context.Context, exclude func(string) bool) (result *os.File, err error) {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
f, err := os.CreateTemp("", "rawmark-notes-*.zip")
|
||||
@@ -47,6 +48,12 @@ func (v *Vault) ExportNotes(ctx context.Context) (result *os.File, err error) {
|
||||
return nil
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
if full == f.Name() || (exclude != nil && exclude(rel)) {
|
||||
if d.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, p := range strings.Split(rel, "/") {
|
||||
if strings.HasPrefix(p, ".") {
|
||||
if d.IsDir() {
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestExportNotesKeepsMarkdownAndReportsMissingAssets(t *testing.T) {
|
||||
}
|
||||
v.Delete("other.md")
|
||||
v.SaveAsset("note.md", "a.png", strings.NewReader("image"), 50<<20)
|
||||
file, err := v.ExportNotes(context.Background())
|
||||
file, err := v.ExportNotes(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -58,3 +58,28 @@ func TestExportNotesKeepsMarkdownAndReportsMissingAssets(t *testing.T) {
|
||||
t.Fatalf("导出包不能导回: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportNotesExcludesItsOwnOutputInsideVault(t *testing.T) {
|
||||
v := newVault(t)
|
||||
t.Setenv("TMPDIR", v.Root())
|
||||
if _, err := v.Write("note.md", []byte("source"), Precondition{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
file, err := v.ExportNotes(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
z, err := zip.NewReader(file, info.Size())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(z.File) != 1 || z.File[0].Name != "note.md" {
|
||||
t.Fatal("导出包含自身临时文件")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user