feat: allow calculate root folder summary (adapt for https://github.com/cloudreve/frontend/pull/333)

pull/3407/head
Darren Yu 5 months ago
parent 58b83ea1e4
commit d5e1837b04
No known key found for this signature in database
GPG Key ID: 2D69AA5646405984

@ -455,13 +455,20 @@ func (f *DBFS) Get(ctx context.Context, path *fs.URI, opts ...fs.Option) (fs.Fil
return nil, fs.ErrOwnerOnly return nil, fs.ErrOwnerOnly
} }
// first, try to load from cache loadFolderSummaryDone := false
summary, ok := f.cache.Get(fmt.Sprintf("%s%d", folderSummaryCachePrefix, target.ID()))
if ok { // first, try to load from cache if cache is not bypassed
summaryTyped := summary.(fs.FolderSummary) if !o.bypassFolderSummaryCache {
target.FileFolderSummary = &summaryTyped summary, ok := f.cache.Get(fmt.Sprintf("%s%d", folderSummaryCachePrefix, target.ID()))
} else { if ok {
// cache miss, walk the folder to get the summary summaryTyped := summary.(fs.FolderSummary)
target.FileFolderSummary = &summaryTyped
loadFolderSummaryDone = true
}
}
if !loadFolderSummaryDone {
// cache miss or bypass, walk the folder to get the summary
newSummary := &fs.FolderSummary{Completed: true} newSummary := &fs.FolderSummary{Completed: true}
if f.user.Edges.Group == nil { if f.user.Edges.Group == nil {
return nil, fmt.Errorf("user group not loaded") return nil, fmt.Errorf("user group not loaded")

@ -9,6 +9,7 @@ import (
type dbfsOption struct { type dbfsOption struct {
*fs.FsOption *fs.FsOption
loadFolderSummary bool loadFolderSummary bool
bypassFolderSummaryCache bool
extendedInfo bool extendedInfo bool
loadFilePublicMetadata bool loadFilePublicMetadata bool
loadFileShareIfOwned bool loadFileShareIfOwned bool
@ -173,6 +174,14 @@ func WithLoadFolderSummary() fs.Option {
}) })
} }
// WithBypassFolderSummaryCache enables bypassing existing cached folder summary.
// Newly calculated summary will still be written to cache.
func WithBypassFolderSummaryCache() fs.Option {
return optionFunc(func(o *dbfsOption) {
o.bypassFolderSummaryCache = true
})
}
// WithEntityUser enables loading entity user. // WithEntityUser enables loading entity user.
func WithEntityUser() fs.Option { func WithEntityUser() fs.Option {
return optionFunc(func(o *dbfsOption) { return optionFunc(func(o *dbfsOption) {

@ -603,10 +603,11 @@ func (s *UnlockFileService) Unlock(c *gin.Context) error {
type ( type (
GetFileInfoParameterCtx struct{} GetFileInfoParameterCtx struct{}
GetFileInfoService struct { GetFileInfoService struct {
Uri string `form:"uri"` Uri string `form:"uri"`
ID string `form:"id"` ID string `form:"id"`
ExtendedInfo bool `form:"extended"` ExtendedInfo bool `form:"extended"`
FolderSummary bool `form:"folder_summary"` FolderSummary bool `form:"folder_summary"`
FolderSummaryBypassCache bool `form:"nocache"`
} }
) )
@ -645,6 +646,9 @@ func (s *GetFileInfoService) Get(c *gin.Context) (*FileResponse, error) {
} }
if s.FolderSummary { if s.FolderSummary {
opts = append(opts, dbfs.WithLoadFolderSummary()) opts = append(opts, dbfs.WithLoadFolderSummary())
if s.FolderSummaryBypassCache {
opts = append(opts, dbfs.WithBypassFolderSummaryCache())
}
} }
file, err := m.Get(c, uri, opts...) file, err := m.Get(c, uri, opts...)

Loading…
Cancel
Save