Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions internal/webindexer/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type LocalBackend struct {

var _ FileSource = &LocalBackend{}

func (l *LocalBackend) Read(path string) ([]Item, bool, error) {
var items []Item
func (l *LocalBackend) Read(path string) ([]*Item, bool, error) {
var items []*Item
log.Debugf("Listing files in %s", path)
files, err := os.ReadDir(path)
if err != nil {
Expand All @@ -38,7 +38,7 @@ func (l *LocalBackend) Read(path string) ([]Item, bool, error) {
log.Infof("Skipping indexing of %s (found skipindex file %s), will include in parent directory", path, file.Name())
// Return empty items but mark as not having noindex file
// This will prevent indexing this directory but still include it in the parent
return []Item{}, false, nil
return []*Item{}, false, nil
}
}
}
Expand Down Expand Up @@ -76,15 +76,13 @@ func (l *LocalBackend) Read(path string) ([]Item, bool, error) {
}
}

size := humanizeBytes(stat.Size())
modified := stat.ModTime().Format(l.cfg.DateFormat)

itemName := file.Name()
item := Item{
item := &Item{
Name: itemName,
Size: size,
LastModified: modified,
Size: stat.Size(),
LastModified: stat.ModTime(),
IsDir: stat.IsDir(),
HasMetadata: true,
}

items = append(items, item)
Expand Down
15 changes: 8 additions & 7 deletions internal/webindexer/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type S3API interface {

var _ FileSource = &S3Backend{}

func (s *S3Backend) Read(prefix string) ([]Item, bool, error) {
func (s *S3Backend) Read(prefix string) ([]*Item, bool, error) {
// Ensure the prefix has a trailing slash for s3 keys
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
Expand Down Expand Up @@ -62,11 +62,11 @@ func (s *S3Backend) Read(prefix string) ([]Item, bool, error) {
log.Infof("Skipping indexing of %s/%s (found skipindex file %s), will include in parent directory", s.bucket, prefix, fileName)
// Return empty items but mark as not having noindex file
// This will prevent indexing this directory but still include it in the parent
return []Item{}, false, nil
return []*Item{}, false, nil
}
}

var items []Item
var items []*Item
// Process all other files
for _, content := range resp.Contents {
if shouldSkip(*content.Key, s.cfg.IndexFile, s.cfg.Skips) {
Expand All @@ -76,11 +76,12 @@ func (s *S3Backend) Read(prefix string) ([]Item, bool, error) {
// Get the relative name by removing the prefix
itemName := strings.TrimPrefix(*content.Key, prefix)

item := Item{
item := &Item{
Name: itemName,
Size: humanizeBytes(*content.Size),
LastModified: content.LastModified.Format(s.cfg.DateFormat),
Size: *content.Size,
LastModified: *content.LastModified,
IsDir: false,
HasMetadata: true,
}

items = append(items, item)
Expand Down Expand Up @@ -117,7 +118,7 @@ func (s *S3Backend) Read(prefix string) ([]Item, bool, error) {
}

dirName := strings.TrimPrefix(*commonPrefix.Prefix, prefix)
item := Item{
item := &Item{
Name: dirName,
IsDir: true,
}
Expand Down
10 changes: 5 additions & 5 deletions internal/webindexer/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"unicode"
)

func (i *Indexer) sort(items *[]Item) {
func (i *Indexer) sort(items *[]TemplateItem) {
switch i.Cfg.SortByValue() {
case SortByDate:
orderByLastModified(items)
Expand All @@ -27,21 +27,21 @@ func (i *Indexer) sort(items *[]Item) {
}
}

func orderByName(items *[]Item) {
func orderByName(items *[]TemplateItem) {
sort.SliceStable(*items, func(i, j int) bool {
return (*items)[i].Name < (*items)[j].Name
})
}

func orderByLastModified(items *[]Item) {
func orderByLastModified(items *[]TemplateItem) {
sort.SliceStable(*items, func(i, j int) bool {
return (*items)[i].LastModified > (*items)[j].LastModified
})
}

// orderByNaturalName sorts items by their names with numbers ordered
// naturally. e.g. 1,2,10 instead of 1,10,2 or 0.8.2 before 0.8.10
func orderByNaturalName(items *[]Item) {
func orderByNaturalName(items *[]TemplateItem) {
sort.SliceStable(*items, func(i, j int) bool {
return cmpNatural((*items)[i].Name, (*items)[j].Name)
})
Expand Down Expand Up @@ -95,7 +95,7 @@ func cmpNatural(a, b string) bool {
return len(aSegments) < len(bSegments)
}

func orderDirsFirst(items *[]Item) {
func orderDirsFirst(items *[]TemplateItem) {
sort.SliceStable(*items, func(i, j int) bool {
if (*items)[i].IsDir && !(*items)[j].IsDir {
return true
Expand Down
12 changes: 7 additions & 5 deletions internal/webindexer/sort_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package webindexer

import "testing"
import (
"testing"
)

func TestOrderByName(t *testing.T) {
items := []Item{
items := []TemplateItem{
{Name: "banana"},
{Name: "apple"},
{Name: "cherry"},
Expand All @@ -19,7 +21,7 @@ func TestOrderByName(t *testing.T) {
}

func TestOrderByLastModified(t *testing.T) {
items := []Item{
items := []TemplateItem{
{Name: "banana", LastModified: "2020-01-03"},
{Name: "apple", LastModified: "2020-01-01"},
{Name: "cherry", LastModified: "2020-01-02"},
Expand All @@ -35,7 +37,7 @@ func TestOrderByLastModified(t *testing.T) {
}

func TestOrderByNaturalName(t *testing.T) {
items := []Item{
items := []TemplateItem{
{Name: "item10"},
{Name: "item2"},
{Name: "item1"},
Expand All @@ -51,7 +53,7 @@ func TestOrderByNaturalName(t *testing.T) {
}

func TestOrderDirsFirst(t *testing.T) {
items := []Item{
items := []TemplateItem{
{Name: "file.txt", IsDir: false},
{Name: "folder", IsDir: true},
{Name: "another_folder", IsDir: true},
Expand Down
8 changes: 3 additions & 5 deletions internal/webindexer/templates/themes/default.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@
</tr>
{{ if .ParentURL }}
<tr>
<td class="filename"><a href="{{ .ParentURL }}">
<td class="filename" colspan="3"><a href="{{ .ParentURL }}">
<span class="icon">🔼</span>Go Up</a>
</td>
<td>-</td>
<td>-</td>
</tr>
{{end}}
{{range .Items}}
Expand All @@ -85,14 +83,14 @@
<a href="{{.URL}}">{{.Name}}</a>
</td>
<td class="size">
{{if not .IsDir}}
{{if .Size }}
{{.Size}}
{{else}}
-
{{end}}
</td>
<td class="date">
{{if not .IsDir}}
{{if .LastModified}}
{{.LastModified}}
{{else}}
-
Expand Down
8 changes: 3 additions & 5 deletions internal/webindexer/templates/themes/dracula.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@
</tr>
{{ if .ParentURL }}
<tr>
<td class="filename"><a href="{{ .ParentURL }}">
<td class="filename" colspan="3"><a href="{{ .ParentURL }}">
<span class="icon">🔼</span>Go Up</a>
</td>
<td>-</td>
<td>-</td>
</tr>
{{end}}
{{range .Items}}
Expand All @@ -144,14 +142,14 @@
<a href="{{.URL}}">{{.Name}}</a>
</td>
<td class="size">
{{if not .IsDir}}
{{if .Size}}
{{.Size}}
{{else}}
-
{{end}}
</td>
<td class="date">
{{if not .IsDir}}
{{if .LastModified}}
{{.LastModified}}
{{else}}
-
Expand Down
8 changes: 3 additions & 5 deletions internal/webindexer/templates/themes/nord.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,9 @@
</tr>
{{ if .ParentURL }}
<tr>
<td class="filename"><a href="{{ .ParentURL }}">
<td class="filename" colspan="3"><a href="{{ .ParentURL }}">
<span class="icon">🔼</span>Go Up</a>
</td>
<td>-</td>
<td>-</td>
</tr>
{{end}}
{{range .Items}}
Expand All @@ -155,14 +153,14 @@
<a href="{{.URL}}">{{.Name}}</a>
</td>
<td class="size">
{{if not .IsDir}}
{{if .Size}}
{{.Size}}
{{else}}
-
{{end}}
</td>
<td class="date">
{{if not .IsDir}}
{{if .LastModified}}
{{.LastModified}}
{{else}}
-
Expand Down
8 changes: 3 additions & 5 deletions internal/webindexer/templates/themes/solarized.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@
</tr>
{{ if .ParentURL }}
<tr>
<td class="filename"><a href="{{ .ParentURL }}">
<td class="filename" colspan="3"><a href="{{ .ParentURL }}">
<span class="icon">🔼</span>Go Up</a>
</td>
<td>-</td>
<td>-</td>
</tr>
{{end}}
{{range .Items}}
Expand All @@ -148,14 +146,14 @@
<a href="{{.URL}}">{{.Name}}</a>
</td>
<td class="size">
{{if not .IsDir}}
{{if .Size}}
{{.Size}}
{{else}}
-
{{end}}
</td>
<td class="date">
{{if not .IsDir}}
{{if .LastModified}}
{{.LastModified}}
{{else}}
-
Expand Down
Loading
Loading