fix: return friendly paths for lazily mounted items
Fetch the uniquely identified item when mount metadata has not yet been
populated, so get-path does not fall back to the ID-based location.
💘 Generated with Crush
Assisted-by: Crush:openai/gpt-5.6-terra
This commit is contained in:
1 parent
6041cc0a33
commit
d15b7d082e
2 files changed
+41
No files matched your search
@@ -160,6 +160,18 @@ func runGetPathCommand(args []string, cfg config, password string) error {
|
|||||||
fmt.Println(path)
|
fmt.Println(path)
|
||||||
return nil
|
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+".*"))
|
matches, err := filepath.Glob(filepath.Join(mountPoint, "by-id", idShard(id), id+".*"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -183,6 +195,16 @@ func friendlyLocalPathForItemID(mountPoint, id string) (string, error) {
|
|||||||
return filepath.Join(mountPoint, filepath.FromSlash(item.FriendlyPath)), nil
|
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) {
|
func readMountMetadata(mountPoint string) (mountMetadata, error) {
|
||||||
var metadata mountMetadata
|
var metadata mountMetadata
|
||||||
data, err := os.ReadFile(filepath.Join(mountPoint, mountMetadataFileName))
|
data, err := os.ReadFile(filepath.Join(mountPoint, mountMetadataFileName))
|
||||||
|
|||||||
@@ -338,3 +338,22 @@ func TestFriendlyLocalPathForItemIDUsesMetadata(t *testing.T) {
|
|||||||
t.Fatalf("expected %q, got %q", want, got)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user