filebrowser/backend/files/sync.go

121 lines
2.8 KiB
Go
Raw Normal View History

package files
import (
"log"
"time"
"github.com/gtsteffaniak/filebrowser/settings"
)
// UpdateFileMetadata updates the FileInfo for the specified directory in the index.
func (si *Index) UpdateFileMetadata(adjustedPath string, info FileInfo) bool {
2024-02-10 00:13:02 +00:00
si.mu.Lock()
defer si.mu.Unlock()
dir, exists := si.Directories[adjustedPath]
2024-10-07 22:44:53 +00:00
if !exists {
si.Directories[adjustedPath] = FileInfo{}
}
2024-10-07 22:44:53 +00:00
return si.SetFileMetadata(adjustedPath, dir)
}
// SetFileMetadata sets the FileInfo for the specified directory in the index.
2024-02-10 00:13:02 +00:00
// internal use only
func (si *Index) SetFileMetadata(adjustedPath string, info FileInfo) bool {
_, exists := si.Directories[adjustedPath]
if !exists {
return false
}
info.CacheTime = time.Now()
2024-10-07 22:44:53 +00:00
si.Directories[adjustedPath] = info
return true
}
// GetMetadataInfo retrieves the FileInfo from the specified directory in the index.
func (si *Index) GetMetadataInfo(adjustedPath string) (FileInfo, bool) {
si.mu.RLock()
dir, exists := si.Directories[adjustedPath]
si.mu.RUnlock()
2024-10-07 22:44:53 +00:00
if !exists {
return dir, exists
}
// remove recursive items, we only want this directories direct files
cleanedItems := []ReducedItem{}
for _, item := range dir.Items {
cleanedItems = append(cleanedItems, ReducedItem{
Name: item.Name,
Size: item.Size,
IsDir: item.IsDir,
ModTime: item.ModTime,
Type: item.Type,
})
}
2024-10-07 22:44:53 +00:00
dir.Items = nil
dir.ReducedItems = cleanedItems
realPath, _, _ := GetRealPath(adjustedPath)
dir.Path = realPath
return dir, exists
}
// SetDirectoryInfo sets the directory information in the index.
2024-10-07 22:44:53 +00:00
func (si *Index) SetDirectoryInfo(adjustedPath string, dir FileInfo) {
si.mu.Lock()
si.Directories[adjustedPath] = dir
si.mu.Unlock()
}
// SetDirectoryInfo sets the directory information in the index.
2024-10-07 22:44:53 +00:00
func (si *Index) GetDirectoryInfo(adjustedPath string) (FileInfo, bool) {
si.mu.RLock()
dir, exists := si.Directories[adjustedPath]
si.mu.RUnlock()
2024-09-16 21:01:16 +00:00
return dir, exists
}
func (si *Index) RemoveDirectory(path string) {
si.mu.Lock()
defer si.mu.Unlock()
delete(si.Directories, path)
}
func (si *Index) UpdateCount(given string) {
si.mu.Lock()
defer si.mu.Unlock()
if given == "files" {
si.NumFiles++
} else if given == "dirs" {
si.NumDirs++
} else {
log.Println("could not update unknown type: ", given)
}
}
func (si *Index) resetCount() {
si.mu.Lock()
defer si.mu.Unlock()
si.NumDirs = 0
si.NumFiles = 0
si.inProgress = true
}
func GetIndex(root string) *Index {
for _, index := range indexes {
if index.Root == root {
return index
}
}
if settings.Config.Server.Root != "" {
rootPath = settings.Config.Server.Root
}
newIndex := &Index{
Root: rootPath,
2024-10-07 22:44:53 +00:00
Directories: map[string]FileInfo{},
NumDirs: 0,
NumFiles: 0,
inProgress: false,
}
indexesMutex.Lock()
indexes = append(indexes, newIndex)
indexesMutex.Unlock()
return newIndex
}