Fix FUSE attachment sizes and item filenames

This commit is contained in:
Jan Bader
2026-07-13 23:07:30 +02:00
parent d57724151a
commit 621fbe4913
3 changed files with 58 additions and 15 deletions
+23 -8
View File
@@ -54,7 +54,7 @@ func runMountCommand(args []string, cfg config, password string) error {
if err != nil {
return fmt.Errorf("search items: %w", err)
}
root := buildMountTree(client, items)
root := buildMountTree(context.Background(), client, items)
conn, err := fuse.Mount(
fsFlags.Arg(0),
@@ -71,9 +71,12 @@ func runMountCommand(args []string, cfg config, password string) error {
return fs.Serve(conn, &docspellFS{root: root})
}
func buildMountTree(client *DocspellClient, items []SearchItem) *dirNode {
func buildMountTree(ctx context.Context, client *DocspellClient, items []SearchItem) *dirNode {
root := &dirNode{name: "", children: map[string]fs.Node{}}
for _, item := range items {
if full, err := client.GetItem(ctx, item.ID); err == nil {
item = *full
}
folderName := "No Folder"
if item.Folder != nil && item.Folder.Name != "" {
folderName = item.Folder.Name
@@ -94,15 +97,22 @@ func buildMountTree(client *DocspellClient, items []SearchItem) *dirNode {
base = item.ID
}
for i, att := range item.Attachments {
name := sanitizeName(att.Name)
if name == "" {
name = fmt.Sprintf("%s-%d.pdf", base, i+1)
ext := filepath.Ext(att.Name)
if ext == "" {
ext = ".pdf"
}
name := base + ext
if len(item.Attachments) > 1 {
name = fmt.Sprintf("%s - %02d - %s", base, i+1, name)
name = fmt.Sprintf("%s - %02d%s", base, i+1, ext)
}
name = uniqueName(dir.children, name)
dir.children[name] = &fileNode{name: name, attachmentID: att.ID, size: uint64(att.Size), knownSize: att.Size > 0, modTime: modTime, client: client}
size := att.Size
if size <= 0 {
if headSize, err := client.AttachmentSize(ctx, att.ID); err == nil && headSize > 0 {
size = headSize
}
}
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}
}
}
return root
@@ -189,6 +199,11 @@ func (f *fileNode) Attr(ctx context.Context, a *fuse.Attr) error {
return nil
}
func (f *fileNode) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) error {
resp.Flags |= fuse.OpenDirectIO
return nil
}
func (f *fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
data, err := f.load(ctx)
if err != nil {