Skip to content

Commit

Permalink
fix upload logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jul 2, 2024
1 parent 8ef3892 commit 252d4e8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{{end}}
</div>
<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">
<a href="/uploadpage">
<a href="/uploadpage?currentPath={{.CurrentPath}}">
<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>
Expand Down
14 changes: 12 additions & 2 deletions static/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
<header class="bg-emerald-400 text-white p-4 text-center font-black text-4xl"><a href="/s3">MOS3</a></header>
<main class="p-3">
<div class="mx-auto w-3/4 p-3 bg-emerald-800 rounded-xl">
<div class="flex justify-between">
<form id="uploadForm" class="inline" action="/upload" method="post" enctype="multipart/form-data">
<p class="text-3xl mb-5">Upload file</p>
<p>Add a file. The file will be uploaded to {{.CurrentPath}}</p>
<div>
<form id="uploadForm" class="flex justify-between" action="/upload" method="post" enctype="multipart/form-data">
<input id="uploadFile" type="file" name="file">
<input type="hidden" name="currentPath" value="{{.CurrentPath}}">
<div id="upload" class="bg-blue-500 text-white font-bold px-3 py-2 rounded-full mb-3 hover:opacity-60 inline-block">
<a href="javascript:document.querySelector('#uploadForm').submit();">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6 inline">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5m-13.5-9L12 3m0 0 4.5 4.5M12 3v13.5" />
</svg>
upload
</a>
</div>
</form>
</div>
</div>
Expand Down
9 changes: 8 additions & 1 deletion web/ui_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ func S3Handler(w http.ResponseWriter, r *http.Request) {
return
}

var currentPath string
if path == "" {
currentPath = "/"
} else {
currentPath = path
}

dataMap := map[string]interface{}{
"S3Objects": s3Objects,
"Breadcrumbs": util.GenerateBreadcrumbs(path),
"CurrentPath": path,
"CurrentPath": currentPath,
}

tmpl, err := template.ParseFiles("static/index.html")
Expand Down
8 changes: 6 additions & 2 deletions web/upload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ func UploadIndexHandler(w http.ResponseWriter, r *http.Request) {
return
}

tmpl.Execute(w, nil)
dataMap := map[string]interface{}{
"CurrentPath": r.URL.Query().Get("currentPath"),
}
tmpl.Execute(w, dataMap)
}

func UploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
Expand All @@ -45,5 +49,5 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
return
}

http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, filepath.Join("/s3", path), http.StatusFound)
}

0 comments on commit 252d4e8

Please sign in to comment.