filebrowser/backend/files/sync.go

90 lines
2.0 KiB
Go
Raw Normal View History

package files
import (
2024-11-21 00:15:30 +00:00
"path/filepath"
2024-12-17 00:01:55 +00:00
"github.com/gtsteffaniak/filebrowser/backend/settings"
)
// UpdateFileMetadata updates the FileInfo for the specified directory in the index.
2024-11-21 00:15:30 +00:00
func (si *Index) UpdateMetadata(info *FileInfo) bool {
2024-02-10 00:13:02 +00:00
si.mu.Lock()
defer si.mu.Unlock()
2024-11-21 00:15:30 +00:00
si.Directories[info.Path] = info
return true
}
// GetMetadataInfo retrieves the FileInfo from the specified directory in the index.
2024-11-21 00:15:30 +00:00
func (si *Index) GetReducedMetadata(target string, isDir bool) (*FileInfo, bool) {
2024-11-26 17:21:41 +00:00
si.mu.Lock()
defer si.mu.Unlock()
2024-11-21 00:15:30 +00:00
checkDir := si.makeIndexPath(target)
if !isDir {
checkDir = si.makeIndexPath(filepath.Dir(target))
}
dir, exists := si.Directories[checkDir]
2024-10-07 22:44:53 +00:00
if !exists {
2024-11-21 00:15:30 +00:00
return nil, false
}
2024-11-26 17:21:41 +00:00
if isDir {
return dir, true
2024-10-07 22:44:53 +00:00
}
2024-11-26 17:21:41 +00:00
// handle file
if checkDir == "/" {
checkDir = ""
}
2024-11-26 17:21:41 +00:00
baseName := filepath.Base(target)
for _, item := range dir.Files {
if item.Name == baseName {
return &FileInfo{
Path: checkDir + "/" + item.Name,
ItemInfo: item,
}, true
}
2024-11-21 00:15:30 +00:00
}
2024-11-26 17:21:41 +00:00
return nil, false
}
2024-11-21 00:15:30 +00:00
// GetMetadataInfo retrieves the FileInfo from the specified directory in the index.
func (si *Index) GetMetadataInfo(target string, isDir bool) (*FileInfo, bool) {
si.mu.RLock()
2024-11-21 00:15:30 +00:00
defer si.mu.RUnlock()
checkDir := si.makeIndexPath(target)
if !isDir {
checkDir = si.makeIndexPath(filepath.Dir(target))
}
dir, exists := si.Directories[checkDir]
2024-09-16 21:01:16 +00:00
return dir, exists
}
func (si *Index) RemoveDirectory(path string) {
si.mu.Lock()
defer si.mu.Unlock()
2024-11-26 17:21:41 +00:00
si.NumDeleted++
delete(si.Directories, path)
}
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-11-21 00:15:30 +00:00
Directories: map[string]*FileInfo{},
NumDirs: 0,
NumFiles: 0,
}
2024-11-21 00:15:30 +00:00
newIndex.Directories["/"] = &FileInfo{}
indexesMutex.Lock()
indexes = append(indexes, newIndex)
indexesMutex.Unlock()
return newIndex
}