Skip to content

Commit 6753e58

Browse files
committed
feat: calculate sizes and last modified for directories
1 parent 0e38ef0 commit 6753e58

File tree

11 files changed

+124
-91
lines changed

11 files changed

+124
-91
lines changed

internal/webindexer/local.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type LocalBackend struct {
1616

1717
var _ FileSource = &LocalBackend{}
1818

19-
func (l *LocalBackend) Read(path string) ([]Item, bool, error) {
20-
var items []Item
19+
func (l *LocalBackend) Read(path string) ([]*Item, bool, error) {
20+
var items []*Item
2121
log.Debugf("Listing files in %s", path)
2222
files, err := os.ReadDir(path)
2323
if err != nil {
@@ -38,7 +38,7 @@ func (l *LocalBackend) Read(path string) ([]Item, bool, error) {
3838
log.Infof("Skipping indexing of %s (found skipindex file %s), will include in parent directory", path, file.Name())
3939
// Return empty items but mark as not having noindex file
4040
// This will prevent indexing this directory but still include it in the parent
41-
return []Item{}, false, nil
41+
return []*Item{}, false, nil
4242
}
4343
}
4444
}
@@ -76,15 +76,13 @@ func (l *LocalBackend) Read(path string) ([]Item, bool, error) {
7676
}
7777
}
7878

79-
size := humanizeBytes(stat.Size())
80-
modified := stat.ModTime().Format(l.cfg.DateFormat)
81-
8279
itemName := file.Name()
83-
item := Item{
80+
item := &Item{
8481
Name: itemName,
85-
Size: size,
86-
LastModified: modified,
82+
Size: stat.Size(),
83+
LastModified: stat.ModTime(),
8784
IsDir: stat.IsDir(),
85+
HasMetadata: true,
8886
}
8987

9088
items = append(items, item)

internal/webindexer/s3.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type S3API interface {
2323

2424
var _ FileSource = &S3Backend{}
2525

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

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

79-
item := Item{
79+
item := &Item{
8080
Name: itemName,
81-
Size: humanizeBytes(*content.Size),
82-
LastModified: content.LastModified.Format(s.cfg.DateFormat),
81+
Size: *content.Size,
82+
LastModified: *content.LastModified,
8383
IsDir: false,
84+
HasMetadata: true,
8485
}
8586

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

119120
dirName := strings.TrimPrefix(*commonPrefix.Prefix, prefix)
120-
item := Item{
121+
item := &Item{
121122
Name: dirName,
122123
IsDir: true,
123124
}

internal/webindexer/sort.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"unicode"
77
)
88

9-
func (i *Indexer) sort(items *[]Item) {
9+
func (i *Indexer) sort(items *[]TemplateItem) {
1010
switch i.Cfg.SortByValue() {
1111
case SortByDate:
1212
orderByLastModified(items)
@@ -27,21 +27,21 @@ func (i *Indexer) sort(items *[]Item) {
2727
}
2828
}
2929

30-
func orderByName(items *[]Item) {
30+
func orderByName(items *[]TemplateItem) {
3131
sort.SliceStable(*items, func(i, j int) bool {
3232
return (*items)[i].Name < (*items)[j].Name
3333
})
3434
}
3535

36-
func orderByLastModified(items *[]Item) {
36+
func orderByLastModified(items *[]TemplateItem) {
3737
sort.SliceStable(*items, func(i, j int) bool {
3838
return (*items)[i].LastModified > (*items)[j].LastModified
3939
})
4040
}
4141

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

98-
func orderDirsFirst(items *[]Item) {
98+
func orderDirsFirst(items *[]TemplateItem) {
9999
sort.SliceStable(*items, func(i, j int) bool {
100100
if (*items)[i].IsDir && !(*items)[j].IsDir {
101101
return true

internal/webindexer/sort_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package webindexer
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
func TestOrderByName(t *testing.T) {
6-
items := []Item{
8+
items := []TemplateItem{
79
{Name: "banana"},
810
{Name: "apple"},
911
{Name: "cherry"},
@@ -19,7 +21,7 @@ func TestOrderByName(t *testing.T) {
1921
}
2022

2123
func TestOrderByLastModified(t *testing.T) {
22-
items := []Item{
24+
items := []TemplateItem{
2325
{Name: "banana", LastModified: "2020-01-03"},
2426
{Name: "apple", LastModified: "2020-01-01"},
2527
{Name: "cherry", LastModified: "2020-01-02"},
@@ -35,7 +37,7 @@ func TestOrderByLastModified(t *testing.T) {
3537
}
3638

3739
func TestOrderByNaturalName(t *testing.T) {
38-
items := []Item{
40+
items := []TemplateItem{
3941
{Name: "item10"},
4042
{Name: "item2"},
4143
{Name: "item1"},
@@ -51,7 +53,7 @@ func TestOrderByNaturalName(t *testing.T) {
5153
}
5254

5355
func TestOrderDirsFirst(t *testing.T) {
54-
items := []Item{
56+
items := []TemplateItem{
5557
{Name: "file.txt", IsDir: false},
5658
{Name: "folder", IsDir: true},
5759
{Name: "another_folder", IsDir: true},

internal/webindexer/templates/themes/default.html.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@
8585
<a href="{{.URL}}">{{.Name}}</a>
8686
</td>
8787
<td class="size">
88-
{{if not .IsDir}}
88+
{{if .Size }}
8989
{{.Size}}
9090
{{else}}
9191
-
9292
{{end}}
9393
</td>
9494
<td class="date">
95-
{{if not .IsDir}}
95+
{{if .LastModified}}
9696
{{.LastModified}}
9797
{{else}}
9898
-

internal/webindexer/templates/themes/dracula.html.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@
144144
<a href="{{.URL}}">{{.Name}}</a>
145145
</td>
146146
<td class="size">
147-
{{if not .IsDir}}
147+
{{if .Size}}
148148
{{.Size}}
149149
{{else}}
150150
-
151151
{{end}}
152152
</td>
153153
<td class="date">
154-
{{if not .IsDir}}
154+
{{if .LastModified}}
155155
{{.LastModified}}
156156
{{else}}
157157
-

internal/webindexer/templates/themes/nord.html.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@
155155
<a href="{{.URL}}">{{.Name}}</a>
156156
</td>
157157
<td class="size">
158-
{{if not .IsDir}}
158+
{{if .Size}}
159159
{{.Size}}
160160
{{else}}
161161
-
162162
{{end}}
163163
</td>
164164
<td class="date">
165-
{{if not .IsDir}}
165+
{{if .LastModified}}
166166
{{.LastModified}}
167167
{{else}}
168168
-

internal/webindexer/templates/themes/solarized.html.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@
148148
<a href="{{.URL}}">{{.Name}}</a>
149149
</td>
150150
<td class="size">
151-
{{if not .IsDir}}
151+
{{if .Size}}
152152
{{.Size}}
153153
{{else}}
154154
-
155155
{{end}}
156156
</td>
157157
<td class="date">
158-
{{if not .IsDir}}
158+
{{if .LastModified}}
159159
{{.LastModified}}
160160
{{else}}
161161
-

0 commit comments

Comments
 (0)