diff --git a/mount.go b/mount.go index 451b24e..12e20bd 100644 --- a/mount.go +++ b/mount.go @@ -160,6 +160,18 @@ func runGetPathCommand(args []string, cfg config, password string) error { fmt.Println(path) return nil } + client := NewDocspellClient(cfg.DocspellURL, defaultDocspellAuthHeader) + if err := client.Login(context.Background(), cfg.User, password); err != nil { + return fmt.Errorf("login: %w", err) + } + items, err := client.SearchItems(context.Background(), "id:"+id, 1) + if err != nil { + return fmt.Errorf("search item %s: %w", id, err) + } + if path := friendlyLocalPathForSearchItems(mountPoint, id, items); path != "" { + fmt.Println(path) + return nil + } matches, err := filepath.Glob(filepath.Join(mountPoint, "by-id", idShard(id), id+".*")) if err != nil { return err @@ -183,6 +195,16 @@ func friendlyLocalPathForItemID(mountPoint, id string) (string, error) { return filepath.Join(mountPoint, filepath.FromSlash(item.FriendlyPath)), nil } +func friendlyLocalPathForSearchItems(mountPoint, id string, items []SearchItem) string { + _, metadata := buildItemTree(items) + for itemID, item := range metadata.Items { + if strings.EqualFold(itemID, id) && item.FriendlyPath != "" { + return filepath.Join(mountPoint, filepath.FromSlash(item.FriendlyPath)) + } + } + return "" +} + func readMountMetadata(mountPoint string) (mountMetadata, error) { var metadata mountMetadata data, err := os.ReadFile(filepath.Join(mountPoint, mountMetadataFileName)) diff --git a/mount_test.go b/mount_test.go index 7ac2e1a..24f3f4d 100644 --- a/mount_test.go +++ b/mount_test.go @@ -338,3 +338,22 @@ func TestFriendlyLocalPathForItemIDUsesMetadata(t *testing.T) { t.Fatalf("expected %q, got %q", want, got) } } + +func TestFriendlyLocalPathForSearchItemsUsesFriendlyPath(t *testing.T) { + mountPath := t.TempDir() + items := []SearchItem{{ + ID: "EkXte7yy5Yw-Tb1coaAtAQY-7WL61AMHGLQ-56kX2TaRabG", + Name: "Invoice", + Date: 1720396800000, + Folder: &DocspellEntity{ + Name: "Bills", + }, + Attachments: []ItemAttachment{{ID: "att-1", Name: "invoice.pdf"}}, + }} + + got := friendlyLocalPathForSearchItems(mountPath, itemIDFromURLOrID("https://docs.javil.eu/app/item/EkXte7yy5Yw-Tb1coaAtAQY-7WL61AMHGLQ-56kX2TaRabG"), items) + want := filepath.Join(mountPath, "Bills", "2024", "07", "Invoice.pdf") + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +}