diff --git a/mount.go b/mount.go index f43a9d3..2c238dc 100644 --- a/mount.go +++ b/mount.go @@ -73,6 +73,8 @@ func runMountCommand(args []string, cfg config, password string) error { func buildMountTree(ctx context.Context, client *DocspellClient, items []SearchItem) *dirNode { root := &dirNode{name: "", children: map[string]fs.Node{}} + byID := &dirNode{name: "by-id", children: map[string]fs.Node{}} + root.children["by-id"] = byID for _, item := range items { if full, err := client.GetItem(ctx, item.ID); err == nil { item = *full @@ -112,7 +114,16 @@ func buildMountTree(ctx context.Context, client *DocspellClient, items []SearchI } } name = uniqueName(dir.children, sanitizeName(name)) - dir.children[name] = &fileNode{name: name, attachmentID: att.ID, size: uint64(size), knownSize: size > 0, modTime: modTime, client: client} + node := &fileNode{name: name, attachmentID: att.ID, size: uint64(size), knownSize: size > 0, modTime: modTime, client: client} + dir.children[name] = node + + byIDName := item.ID + ext + if len(item.Attachments) > 1 { + if i > 0 { + byIDName = fmt.Sprintf("%s-%d%s", item.ID, i+1, ext) + } + } + byID.children[uniqueName(byID.children, sanitizeName(byIDName))] = node } } return root diff --git a/mount_test.go b/mount_test.go index edf925c..f4480b4 100644 --- a/mount_test.go +++ b/mount_test.go @@ -70,3 +70,26 @@ func TestFileNodeUnknownSizeDoesNotAdvertiseZeroLength(t *testing.T) { t.Fatal("expected unknown size before download") } } + +func TestBuildMountTreeAddsByIDDirectory(t *testing.T) { + items := []SearchItem{{ + ID: "item-1", + Name: "Invoice", + Attachments: []ItemAttachment{ + {ID: "att-1", Name: "original.pdf"}, + {ID: "att-2", Name: "extra.txt"}, + {ID: "att-3", Name: "scan.pdf"}, + }, + }} + + root := buildMountTree(context.Background(), &DocspellClient{}, items) + byID, ok := root.children["by-id"].(*dirNode) + if !ok { + t.Fatalf("expected by-id directory, got %#v", root.children) + } + for _, name := range []string{"item-1.pdf", "item-1-2.txt", "item-1-3.pdf"} { + if _, ok := byID.children[name].(*fileNode); !ok { + t.Fatalf("expected %s in by-id, got %#v", name, byID.children) + } + } +}