Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jun 30, 2024
1 parent ab53659 commit b0a6441
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 12 deletions.
26 changes: 26 additions & 0 deletions core/util/breadcrumbs.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions core/util/breadcrumbs_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion core/util/dir.go → core/util/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 12 additions & 8 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
</head>
<body class="bg-emerald-950 text-white">
<header class="bg-emerald-400 text-white p-4 text-center font-black text-4xl">MOS3</header>
<main class="p-3">
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Upload</button>
</form>

<main class="p-3">
<form action="/mkdir" method="post">
<input type="text" name="dirname" placeholder="Directory name">
<button type="submit">Create Directory</button>
Expand All @@ -29,14 +24,17 @@
{{end}}
{{end}}
</div>
<div class="inline-block bg-green-400 text-white w-auto font-bold py-2 px-2 rounded-full mb-3 hover:opacity-60">
<div id="upload" class="inline-block bg-blue-500 text-white w-auto font-bold py-2 px-2 rounded-full mb-3 hover:opacity-60">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 inline">
<path fillRule="evenodd" d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z" clipRule="evenodd" />
</svg>
New File
</div>
<form id="uploadForm" class="hidden inline" action="/upload" method="post" enctype="multipart/form-data">
<input id="uploadFile" type="file" name="file">
</form>
</div>
{{range .}}
{{range .S3Objects}}
<div class="border-b-2 border-slate-400 py-3 hover:opacity-60">
<p>
{{if .IsDir}}
Expand All @@ -61,4 +59,10 @@
</div>
</main>
</body>
<script>
document.querySelector("#upload").addEventListener("click", () => {
document.querySelector("#uploadFile").click();
document.querySelector("#uploadForm").classList.remove("hidden");
});
</script>
</html>
11 changes: 8 additions & 3 deletions web/s3_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ 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)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

tmpl.Execute(w, s3Objects)
tmpl.Execute(w, dataMap)
}
func download(path string) {
slog.Info("Download file", "path", path)
Expand Down

0 comments on commit b0a6441

Please sign in to comment.