V0.2.1 (#64)
Co-authored-by: Graham Steffaniak <graham.steffaniak@autodesk.com>
This commit is contained in:
parent
9b57eb1060
commit
8e4629a0c4
|
@ -11,10 +11,6 @@ import (
|
||||||
"github.com/gtsteffaniak/filebrowser/settings"
|
"github.com/gtsteffaniak/filebrowser/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
maxIndexSize = 1000
|
|
||||||
)
|
|
||||||
|
|
||||||
type Index struct {
|
type Index struct {
|
||||||
Dirs []string
|
Dirs []string
|
||||||
Files []string
|
Files []string
|
||||||
|
@ -34,8 +30,8 @@ func GetIndex() *Index {
|
||||||
func Initialize(intervalMinutes uint32) {
|
func Initialize(intervalMinutes uint32) {
|
||||||
// Initialize the index
|
// Initialize the index
|
||||||
indexes = Index{
|
indexes = Index{
|
||||||
Dirs: make([]string, 0, maxIndexSize),
|
Dirs: []string{},
|
||||||
Files: make([]string, 0, maxIndexSize),
|
Files: []string{},
|
||||||
}
|
}
|
||||||
rootPath = settings.GlobalConfiguration.Server.Root
|
rootPath = settings.GlobalConfiguration.Server.Root
|
||||||
var numFiles, numDirs int
|
var numFiles, numDirs int
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
|
|
||||||
func BenchmarkFillIndex(b *testing.B) {
|
func BenchmarkFillIndex(b *testing.B) {
|
||||||
indexes = Index{
|
indexes = Index{
|
||||||
Dirs: make([]string, 0, 1000),
|
Dirs: []string{},
|
||||||
Files: make([]string, 0, 1000),
|
Files: []string{},
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
|
|
@ -21,8 +21,6 @@ func (si *Index) Search(search string, scope string, sourceSession string) ([]st
|
||||||
if scope == "" {
|
if scope == "" {
|
||||||
scope = "/"
|
scope = "/"
|
||||||
}
|
}
|
||||||
fileTypes := map[string]bool{}
|
|
||||||
|
|
||||||
runningHash := generateRandomHash(4)
|
runningHash := generateRandomHash(4)
|
||||||
sessionInProgress.Store(sourceSession, runningHash) // Store the value in the sync.Map
|
sessionInProgress.Store(sourceSession, runningHash) // Store the value in the sync.Map
|
||||||
searchOptions := ParseSearch(search)
|
searchOptions := ParseSearch(search)
|
||||||
|
@ -55,20 +53,16 @@ func (si *Index) Search(search string, scope string, sourceSession string) ([]st
|
||||||
if count > maxSearchResults {
|
if count > maxSearchResults {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
pathName := scopedPathNameFilter(path, scope)
|
pathName := scopedPathNameFilter(path, scope, isDir)
|
||||||
if pathName == "" {
|
if pathName == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
fileTypes := map[string]bool{}
|
||||||
matches, fileType := containsSearchTerm(path, searchTerm, *searchOptions, isDir, fileTypes)
|
matches, fileType := containsSearchTerm(path, searchTerm, *searchOptions, isDir, fileTypes)
|
||||||
if !matches {
|
if !matches {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if isDir {
|
fileListTypes[pathName] = fileType
|
||||||
fileListTypes[pathName+"/"] = fileType
|
|
||||||
} else {
|
|
||||||
fileListTypes[pathName] = fileType
|
|
||||||
}
|
|
||||||
matching = append(matching, pathName)
|
matching = append(matching, pathName)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
@ -83,13 +77,17 @@ func (si *Index) Search(search string, scope string, sourceSession string) ([]st
|
||||||
return matching, fileListTypes
|
return matching, fileListTypes
|
||||||
}
|
}
|
||||||
|
|
||||||
func scopedPathNameFilter(pathName string, scope string) string {
|
func scopedPathNameFilter(pathName string, scope string, isDir bool) string {
|
||||||
scope = strings.TrimPrefix(scope, "/")
|
scope = strings.TrimPrefix(scope, "/")
|
||||||
pathName = strings.TrimPrefix(pathName, "/")
|
pathName = strings.TrimPrefix(pathName, "/")
|
||||||
|
pathName = strings.TrimSuffix(pathName, "/")
|
||||||
if strings.HasPrefix(pathName, scope) {
|
if strings.HasPrefix(pathName, scope) {
|
||||||
pathName = "/" + strings.TrimPrefix(pathName, scope)
|
pathName = strings.TrimPrefix(pathName, scope)
|
||||||
|
if isDir {
|
||||||
|
pathName = pathName + "/"
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
pathName = ""
|
pathName = "" // return not matched
|
||||||
}
|
}
|
||||||
return pathName
|
return pathName
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
|
|
||||||
func BenchmarkSearchAllIndexes(b *testing.B) {
|
func BenchmarkSearchAllIndexes(b *testing.B) {
|
||||||
indexes = Index{
|
indexes = Index{
|
||||||
Dirs: make([]string, 0, 1000),
|
Dirs: []string{},
|
||||||
Files: make([]string, 0, 1000),
|
Files: []string{},
|
||||||
}
|
}
|
||||||
// Create mock data
|
// Create mock data
|
||||||
createMockData(50, 3) // 1000 dirs, 3 files per dir
|
createMockData(50, 3) // 1000 dirs, 3 files per dir
|
||||||
|
@ -101,37 +101,37 @@ func TestSearchIndexes(t *testing.T) {
|
||||||
{
|
{
|
||||||
search: "audio",
|
search: "audio",
|
||||||
scope: "/new/",
|
scope: "/new/",
|
||||||
expectedResult: []string{"/test/audio.wav"},
|
expectedResult: []string{"test/audio.wav"},
|
||||||
expectedTypes: map[string]map[string]bool{
|
expectedTypes: map[string]map[string]bool{
|
||||||
"/test/audio.wav": map[string]bool{"audio": true, "dir": false},
|
"test/audio.wav": map[string]bool{"audio": true, "dir": false},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
search: "test",
|
search: "test",
|
||||||
scope: "/",
|
scope: "/",
|
||||||
expectedResult: []string{"/test"},
|
expectedResult: []string{"test/"},
|
||||||
expectedTypes: map[string]map[string]bool{
|
expectedTypes: map[string]map[string]bool{
|
||||||
"/test/": map[string]bool{"dir": true},
|
"test/": map[string]bool{"dir": true},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
search: "archive",
|
search: "archive",
|
||||||
scope: "/",
|
scope: "/",
|
||||||
expectedResult: []string{"/new/test/path/archive.zip"},
|
expectedResult: []string{"new/test/path/archive.zip"},
|
||||||
expectedTypes: map[string]map[string]bool{
|
expectedTypes: map[string]map[string]bool{
|
||||||
"/new/test/path/archive.zip": map[string]bool{"archive": true, "dir": false},
|
"new/test/path/archive.zip": map[string]bool{"archive": true, "dir": false},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
search: "video",
|
search: "video",
|
||||||
scope: "/",
|
scope: "/",
|
||||||
expectedResult: []string{
|
expectedResult: []string{
|
||||||
"/new/test/video.mp4",
|
"new/test/video.mp4",
|
||||||
"/new/test/video.MP4",
|
"new/test/video.MP4",
|
||||||
},
|
},
|
||||||
expectedTypes: map[string]map[string]bool{
|
expectedTypes: map[string]map[string]bool{
|
||||||
"/new/test/video.MP4": map[string]bool{"video": true, "dir": false},
|
"new/test/video.MP4": map[string]bool{"video": true, "dir": false},
|
||||||
"/new/test/video.mp4": map[string]bool{"video": true, "dir": false},
|
"new/test/video.mp4": map[string]bool{"video": true, "dir": false},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -150,27 +150,6 @@ func TestSearchIndexes(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_scopedPathNameFilter(t *testing.T) {
|
|
||||||
type args struct {
|
|
||||||
pathName string
|
|
||||||
scope string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
// TODO: Add test cases.
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if got := scopedPathNameFilter(tt.args.pathName, tt.args.scope); got != tt.want {
|
|
||||||
t.Errorf("scopedPathNameFilter() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_isDoc(t *testing.T) {
|
func Test_isDoc(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
extension string
|
extension string
|
||||||
|
|
Loading…
Reference in New Issue