Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix breadcrumbs.go #11

Merged
merged 1 commit into from
Jul 6, 2024
Merged
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
23 changes: 14 additions & 9 deletions core/util/breadcrumbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import (
"strings"
)

func GenerateBreadcrumbs(path string) map[string]interface{} {
breadcrumbs := make(map[string]interface{})
parts := strings.Split(path, "/")
type Breadcrumb struct {
Name string
Path string
}

func GenerateBreadcrumbs(path string) []Breadcrumb {
var breadcrumbs []Breadcrumb
splitted := strings.Split(path, "/")
fullPath := "/s3"
for i, part := range parts {
if part == "" {
for i, s := range splitted {
if s == "" {
continue
}
fullPath += "/" + part
fullPath += "/" + s

r, _ := regexp.Compile(`.*\..*`)
if i == len(parts)-1 && r.Match([]byte(part)) {
breadcrumbs[part] = fullPath + "?action=dl"
if i == len(splitted)-1 && r.Match([]byte(s)) {
breadcrumbs = append(breadcrumbs, Breadcrumb{Name: s, Path: fullPath + "?action=dl"})
} else {
breadcrumbs[part] = fullPath
breadcrumbs = append(breadcrumbs, Breadcrumb{Name: s, Path: fullPath})
}
}
return breadcrumbs
Expand Down
35 changes: 23 additions & 12 deletions core/util/breadcrumbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,40 @@ import (
func TestGenerateBreadcrumbs(t *testing.T) {
tests := []struct {
input string
expected map[string]interface{}
expected []Breadcrumb
}{
{
input: "/fuga/fugafuga/aaa.txt",
expected: map[string]interface{}{
"fuga": "/s3/fuga",
"fugafuga": "/s3/fuga/fugafuga",
"aaa.txt": "/s3/fuga/fugafuga/aaa.txt?action=dl",
expected: []Breadcrumb{
{Name: "fuga", Path: "/s3/fuga"},
{Name: "fugafuga", Path: "/s3/fuga/fugafuga"},
{Name: "aaa.txt", Path: "/s3/fuga/fugafuga/aaa.txt?action=dl"},
},
},
{
input: "/hoge/fuga/piyo",
expected: map[string]interface{}{
"hoge": "/s3/hoge",
"fuga": "/s3/hoge/fuga",
"piyo": "/s3/hoge/fuga/piyo",
expected: []Breadcrumb{
{Name: "hoge", Path: "/s3/hoge"},
{Name: "fuga", Path: "/s3/hoge/fuga"},
{Name: "piyo", Path: "/s3/hoge/fuga/piyo"},
},
},
{
input: "/s3/hoge",
expected: map[string]interface{}{
"s3": "/s3/s3",
"hoge": "/s3/s3/hoge",
expected: []Breadcrumb{
{Name: "s3", Path: "/s3/s3"},
{Name: "hoge", Path: "/s3/s3/hoge"},
},
},
{
input: "/files/2024/0706/1845/txt/1234.txt",
expected: []Breadcrumb{
{Name: "files", Path: "/s3/files"},
{Name: "2024", Path: "/s3/files/2024"},
{Name: "0706", Path: "/s3/files/2024/0706"},
{Name: "1845", Path: "/s3/files/2024/0706/1845"},
{Name: "txt", Path: "/s3/files/2024/0706/1845/txt"},
{Name: "1234.txt", Path: "/s3/files/2024/0706/1845/txt/1234.txt?action=dl"},
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<div class="flex justify-between">
<div class="text-lg">
<a href="/s3" class="text-cyan-400 underline-offset-2 hover:opacity-60">s3:</a>
{{range $key, $value := .Breadcrumbs}}
<span> / <a href="{{$value}}"
class="text-cyan-400 underline-offset-2 hover:opacity-60">{{$key}}</a></span>
{{range .Breadcrumbs}}
<span> / <a href="{{.Path}}"
class="text-cyan-400 underline-offset-2 hover:opacity-60">{{.Name}}</a></span>
{{end}}
</div>
<div>
Expand Down
Loading