From b0a6441625ecdd48387bfa7d8bda2172a5b40a94 Mon Sep 17 00:00:00 2001 From: t-takahashi Date: Sun, 30 Jun 2024 22:39:28 +0900 Subject: [PATCH] add test --- core/util/breadcrumbs.go | 26 +++++++++++++++++++ core/util/breadcrumbs_test.go | 45 +++++++++++++++++++++++++++++++++ core/util/{dir.go => object.go} | 2 +- static/index.html | 20 +++++++++------ web/s3_controller.go | 11 +++++--- 5 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 core/util/breadcrumbs.go create mode 100644 core/util/breadcrumbs_test.go rename core/util/{dir.go => object.go} (88%) diff --git a/core/util/breadcrumbs.go b/core/util/breadcrumbs.go new file mode 100644 index 0000000..e6a070c --- /dev/null +++ b/core/util/breadcrumbs.go @@ -0,0 +1,26 @@ +package util + +import ( + "regexp" + "strings" +) + +func GenerateBreadcrumbs(path string) map[string]interface{} { + breadcrumbs := make(map[string]interface{}) + parts := strings.Split(path, "/") + fullPath := "" + for i, part := range parts { + if part == "" { + continue + } + fullPath += "/" + part + + r, _ := regexp.Compile(`.*\..*`) + if i == len(parts)-1 && r.Match([]byte(part)) { + breadcrumbs[part] = fullPath + "?action=dl" + } else { + breadcrumbs[part] = fullPath + } + } + return breadcrumbs +} diff --git a/core/util/breadcrumbs_test.go b/core/util/breadcrumbs_test.go new file mode 100644 index 0000000..662b11b --- /dev/null +++ b/core/util/breadcrumbs_test.go @@ -0,0 +1,45 @@ +package util + +import ( + "reflect" + "testing" +) + +func TestGenerateBreadcrumbs(t *testing.T) { + tests := []struct { + input string + expected map[string]interface{} + }{ + { + input: "/s3/fuga/fugafuga/aaa.txt", + expected: map[string]interface{}{ + "s3": "/s3", + "fuga": "/s3/fuga", + "fugafuga": "/s3/fuga/fugafuga", + "aaa.txt": "/s3/fuga/fugafuga/aaa.txt?action=dl", + }, + }, + { + input: "/s3/hoge/fuga/piyo", + expected: map[string]interface{}{ + "s3": "/s3", + "hoge": "/s3/hoge", + "fuga": "/s3/hoge/fuga", + "piyo": "/s3/hoge/fuga/piyo", + }, + }, + { + input: "/single", + expected: map[string]interface{}{ + "single": "/single", + }, + }, + } + + for _, test := range tests { + result := GenerateBreadcrumbs(test.input) + if !reflect.DeepEqual(result, test.expected) { + t.Errorf("GenerateBreadcrumbs(%q) = %v; expected %v", test.input, result, test.expected) + } + } +} diff --git a/core/util/dir.go b/core/util/object.go similarity index 88% rename from core/util/dir.go rename to core/util/object.go index 2670ee2..f77779d 100644 --- a/core/util/dir.go +++ b/core/util/object.go @@ -9,7 +9,7 @@ import ( "github.com/tttol/mos3/core/model" ) -func GetS3Objects(r *http.Request, path string) ([]model.S3Object, error) { +func GenerateS3Objects(r *http.Request, path string) ([]model.S3Object, error) { dirEntry, err := os.ReadDir(filepath.Join("./upload", path)) if err != nil { slog.Error("ReadDir error", "error", err) diff --git a/static/index.html b/static/index.html index 157ad71..28c5935 100644 --- a/static/index.html +++ b/static/index.html @@ -8,12 +8,7 @@
MOS3
-
-
- - -
- +
@@ -29,14 +24,17 @@ {{end}} {{end}} -
+
New File
+ + +
- {{range .}} + {{range .S3Objects}}

{{if .IsDir}} @@ -61,4 +59,10 @@

+ diff --git a/web/s3_controller.go b/web/s3_controller.go index cd352bb..d07decb 100644 --- a/web/s3_controller.go +++ b/web/s3_controller.go @@ -13,18 +13,23 @@ const uploadDir = "./upload" func S3Handler(w http.ResponseWriter, r *http.Request) { slog.Info("S3Handler is called.") path := r.URL.Path[len("/s3/"):] - if r.URL.Query().Get("ation") == "dl" { + if r.URL.Query().Get("action") == "dl" { download(path) return } - s3Objects, err := util.GetS3Objects(r, path) + s3Objects, err := util.GenerateS3Objects(r, path) if err != nil { slog.Error("GetS3Objects error", "error", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } + dataMap := map[string]interface{}{ + "S3Objects": s3Objects, + "breadcrumbs": "Some additional data", + } + tmpl, err := template.ParseFiles("static/index.html") if err != nil { slog.Error("template file error", "error", err) @@ -32,7 +37,7 @@ func S3Handler(w http.ResponseWriter, r *http.Request) { return } - tmpl.Execute(w, s3Objects) + tmpl.Execute(w, dataMap) } func download(path string) { slog.Info("Download file", "path", path)