github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/vfs/file_pather.go (about) 1 package vfs 2 3 import ( 4 "path" 5 6 "github.com/cozy/cozy-stack/pkg/consts" 7 ) 8 9 type filePatherWithCache struct { 10 fs Indexer 11 cache map[string]string // dirID -> parentPath 12 } 13 14 // NewFilePatherWithCache creates a FilePather that will cache the calls to 15 // CouchDB for finding the parent directories. 16 func NewFilePatherWithCache(fs Indexer) FilePather { 17 return &filePatherWithCache{ 18 fs: fs, 19 cache: make(map[string]string), 20 } 21 } 22 23 func (fp *filePatherWithCache) FilePath(doc *FileDoc) (string, error) { 24 var parentPath string 25 if doc.DirID == consts.RootDirID { 26 parentPath = "/" 27 } else if doc.DirID == consts.TrashDirID { 28 parentPath = TrashDirName 29 } else if cachedPath, ok := fp.cache[doc.DirID]; ok { 30 parentPath = cachedPath 31 } else { 32 parent, err := fp.fs.DirByID(doc.DirID) 33 if err != nil { 34 return "", ErrParentDoesNotExist 35 } 36 parentPath = parent.Fullpath 37 fp.cache[doc.DirID] = parentPath 38 } 39 return path.Join(parentPath, doc.DocName), nil 40 }