diff --git a/main.go b/main.go index a63429b..58b2141 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ func main() { http.HandleFunc("/s3/", web.S3Handler) http.HandleFunc("/uploadpage", web.UploadIndexHandler) http.HandleFunc("/upload", web.UploadHandler) - http.HandleFunc("/delete", web.DeleteHandler) + http.HandleFunc("/remove", web.RemoveHandler) http.HandleFunc("/rename", web.RenameHandler) http.HandleFunc("/mkdir", web.MkdirHandler) http.HandleFunc("/rmdir", web.RmdirHandler) diff --git a/static/index.html b/static/index.html index ed4908d..b0a3dcc 100644 --- a/static/index.html +++ b/static/index.html @@ -33,9 +33,11 @@ New File -
+
- + @@ -56,7 +58,8 @@ - +
{{range .S3Objects}} -
-

+

+
{{if .IsDir}} - + - {{.Name}} + {{.Name}} {{else}} - + - {{.Name}} + {{.Name}} {{end}} -

+
+
+ + + + + +
{{end}} +
+ +
- \ No newline at end of file + diff --git a/web/rm_controller.go b/web/rm_controller.go new file mode 100644 index 0000000..2ef491b --- /dev/null +++ b/web/rm_controller.go @@ -0,0 +1,27 @@ +package web + +import ( + "log/slog" + "net/http" + "os" + "path/filepath" +) + +func RemoveHandler(w http.ResponseWriter, r *http.Request) { + slog.Info("RemoveHandler is called.") + if r.Method != "POST" { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return + } + + path := r.FormValue("path")[len("/s3/"):] + err := os.Remove(filepath.Join(UPLOAD_DIR, path)) + if err != nil { + slog.Error("Failed to remove.", "error", err, "path", path) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + slog.Info("Remove file success.", "path", path) + + http.Redirect(w, r, "/s3", http.StatusFound) +} diff --git a/web/ui_controller.go b/web/ui_controller.go index 0fb3152..cc4029e 100644 --- a/web/ui_controller.go +++ b/web/ui_controller.go @@ -49,21 +49,7 @@ func S3Handler(w http.ResponseWriter, r *http.Request) { tmpl.Execute(w, dataMap) } -func DeleteHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) - return - } - filename := r.FormValue("filename") - err := os.Remove(filepath.Join(UPLOAD_DIR, filename)) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - http.Redirect(w, r, "/", http.StatusFound) -} func RenameHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" {