2023-12-01 23:47:00 +00:00
|
|
|
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()
|
2023-12-01 23:47:00 +00:00
|
|
|
dir, exists := si.Directories[adjustedPath]
|
2024-10-07 22:44:53 +00:00
|
|
|
if !exists {
|
|
|
|
si.Directories[adjustedPath] = FileInfo{}
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
2024-10-07 22:44:53 +00:00
|
|
|
return si.SetFileMetadata(adjustedPath, dir)
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFileMetadata sets the FileInfo for the specified directory in the index.
|
2024-02-10 00:13:02 +00:00
|
|
|
// internal use only
|
2023-12-01 23:47:00 +00:00
|
|
|
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
|
2023-12-01 23:47:00 +00:00
|
|
|
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,
|
|
|
|
})
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
2024-10-07 22:44:53 +00:00
|
|
|
dir.Items = nil
|
|
|
|
dir.ReducedItems = cleanedItems
|
|
|
|
realPath, _, _ := GetRealPath(adjustedPath)
|
|
|
|
dir.Path = realPath
|
|
|
|
return dir, exists
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirectoryInfo sets the directory information in the index.
|
2024-10-07 22:44:53 +00:00
|
|
|
func (si *Index) SetDirectoryInfo(adjustedPath string, dir FileInfo) {
|
2023-12-01 23:47:00 +00:00
|
|
|
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) {
|
2023-12-01 23:47:00 +00:00
|
|
|
si.mu.RLock()
|
|
|
|
dir, exists := si.Directories[adjustedPath]
|
|
|
|
si.mu.RUnlock()
|
2024-09-16 21:01:16 +00:00
|
|
|
return dir, exists
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{},
|
2023-12-01 23:47:00 +00:00
|
|
|
NumDirs: 0,
|
|
|
|
NumFiles: 0,
|
|
|
|
inProgress: false,
|
|
|
|
}
|
|
|
|
indexesMutex.Lock()
|
|
|
|
indexes = append(indexes, newIndex)
|
|
|
|
indexesMutex.Unlock()
|
|
|
|
return newIndex
|
|
|
|
}
|