From 579a25f9893e2d7bdb979908a7f649f6ea72ba12 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 14 Sep 2017 11:08:16 +0200 Subject: [PATCH] carddav: cache file contents when opening it --- carddav/fs.go | 13 +++++++++++-- webdav.go | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/carddav/fs.go b/carddav/fs.go index a6dc21d..d7ba46a 100644 --- a/carddav/fs.go +++ b/carddav/fs.go @@ -354,10 +354,19 @@ func (fs *fileSystem) OpenFile(ctx context.Context, name string, flag int, perm return nil, err } - return &file{ + f := &file{ fs: fs, ao: ao, - }, nil + } + + if flag&os.O_RDONLY != 0 { + // This file will be read, cache its contents + if _, err := f.Read(nil); err != nil { + return f, err + } + } + + return f, nil } func (fs *fileSystem) RemoveAll(ctx context.Context, name string) error { diff --git a/webdav.go b/webdav.go index e7cb953..0ab35d8 100644 --- a/webdav.go +++ b/webdav.go @@ -226,6 +226,7 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (sta } w.Header().Set("ETag", etag) // Let ServeContent determine the Content-Type header. + // TODO: use getcontenttype dead prop if available http.ServeContent(w, r, reqPath, fi.ModTime(), f) return 0, nil }