github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/move/file_finder.go (about)

     1  package move
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/cozy/cozy-stack/model/vfs"
     7  )
     8  
     9  type fileFinderWithCache struct {
    10  	vfs   vfs.VFS
    11  	cache map[string]*vfs.FileDoc // fileID -> file
    12  }
    13  
    14  func newFileFinderWithCache(fs vfs.VFS) *fileFinderWithCache {
    15  	return &fileFinderWithCache{
    16  		vfs:   fs,
    17  		cache: make(map[string]*vfs.FileDoc),
    18  	}
    19  }
    20  
    21  func (ff *fileFinderWithCache) Find(versionID string) (*vfs.FileDoc, error) {
    22  	fileID := strings.SplitN(versionID, "/", 2)[0]
    23  	if file, ok := ff.cache[fileID]; ok {
    24  		return file, nil
    25  	}
    26  	file, err := ff.vfs.FileByID(fileID)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	ff.cache[fileID] = file
    31  	return file, nil
    32  }