2023-12-01 23:47:00 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2024-11-21 00:15:30 +00:00
|
|
|
"path/filepath"
|
2023-12-01 23:47:00 +00:00
|
|
|
|
|
|
|
"github.com/gtsteffaniak/filebrowser/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
|
2023-12-01 23:47:00 +00:00
|
|
|
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 = ""
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
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
|
|
|
|
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2023-12-01 23:47:00 +00:00
|
|
|
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
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (si *Index) RemoveDirectory(path string) {
|
|
|
|
si.mu.Lock()
|
|
|
|
defer si.mu.Unlock()
|
2024-11-26 17:21:41 +00:00
|
|
|
si.NumDeleted++
|
2023-12-01 23:47:00 +00:00
|
|
|
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{},
|
2023-12-01 23:47:00 +00:00
|
|
|
NumDirs: 0,
|
|
|
|
NumFiles: 0,
|
|
|
|
}
|
2024-11-21 00:15:30 +00:00
|
|
|
newIndex.Directories["/"] = &FileInfo{}
|
2023-12-01 23:47:00 +00:00
|
|
|
indexesMutex.Lock()
|
|
|
|
indexes = append(indexes, newIndex)
|
|
|
|
indexesMutex.Unlock()
|
|
|
|
return newIndex
|
|
|
|
}
|