From 240ea4a127641d1e58d807e5b106b58a27287ca2 Mon Sep 17 00:00:00 2001 From: TOL Date: Sat, 6 Jul 2024 16:37:51 +0900 Subject: [PATCH] add remove logic (#8) * add remove logic * fix: handle invalid request method in RemoveHandler --- main.go | 2 +- static/index.html | 52 +++++++++++++----- web/rm_controller.go | 28 ++++++++++ web/rm_controller_test.go | 108 ++++++++++++++++++++++++++++++++++++++ web/ui_controller.go | 14 ----- 5 files changed, 177 insertions(+), 27 deletions(-) create mode 100644 web/rm_controller.go create mode 100644 web/rm_controller_test.go 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..efcc9a8 --- /dev/null +++ b/web/rm_controller.go @@ -0,0 +1,28 @@ +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" { + slog.Error("Invalid request method", "method", r.Method) + 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/rm_controller_test.go b/web/rm_controller_test.go new file mode 100644 index 0000000..d7921e8 --- /dev/null +++ b/web/rm_controller_test.go @@ -0,0 +1,108 @@ +package web + +// import ( +// "net/http" +// "net/http/httptest" +// "os" +// "path/filepath" +// "strings" +// "testing" +// ) + +// func TestRemoveHandler(t *testing.T) { +// // TEST_UPLOAD_DIR := "/test" + +// tempDir, err := os.MkdirTemp("", "test-upload-dir") +// if err != nil { +// t.Fatal(err) +// } +// defer os.RemoveAll(tempDir) + +// // Override TEST_UPLOAD_DIR to use the temporary directory +// // uploadDir := TEST_UPLOAD_DIR +// // defer func() { TEST_UPLOAD_DIR = uploadDir }() +// // TEST_UPLOAD_DIR = tempDir + +// // Create a test file to be removed +// testFilePath := filepath.Join(UPLOAD_DIR, "testfile.txt") +// dst, err := os.Create(testFilePath) +// if err != nil { +// t.Fatal(err) +// } +// defer dst.Close() + +// err = os.WriteFile(testFilePath, []byte("test content"), 0644) +// if err != nil { +// t.Fatal(err) +// } + +// // Prepare the request +// formData := strings.NewReader("path=/s3/testfile.txt") +// req, err := http.NewRequest("POST", "/s3/remove", formData) +// if err != nil { +// t.Fatal(err) +// } +// req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + +// // Record the response +// responseRecorder := httptest.NewRecorder() +// handler := http.HandlerFunc(RemoveHandler) + +// // Call the handler +// handler.ServeHTTP(responseRecorder, req) + +// // Check the status code +// if status := responseRecorder.Code; status != http.StatusFound { +// t.Errorf("handler returned wrong status code: got %v want %v", +// status, http.StatusFound) +// } + +// // Check the file is removed +// if _, err := os.Stat(testFilePath); !os.IsNotExist(err) { +// t.Errorf("expected file to be removed, but it still exists: %v", err) +// } +// } + +// func TestRemoveHandler_InvalidMethod(t *testing.T) { +// // Prepare the request with invalid method +// req, err := http.NewRequest("GET", "/s3/remove", nil) +// if err != nil { +// t.Fatal(err) +// } + +// // Record the response +// rr := httptest.NewRecorder() +// handler := http.HandlerFunc(RemoveHandler) + +// // Call the handler +// handler.ServeHTTP(rr, req) + +// // Check the status code +// if status := rr.Code; status != http.StatusMethodNotAllowed { +// t.Errorf("handler returned wrong status code: got %v want %v", +// status, http.StatusMethodNotAllowed) +// } +// } + +// func TestRemoveHandler_FileNotFound(t *testing.T) { +// // Prepare the request +// formData := strings.NewReader("path=/s3/nonexistentfile.txt") +// req, err := http.NewRequest("POST", "/s3/remove", formData) +// if err != nil { +// t.Fatal(err) +// } +// req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + +// // Record the response +// rr := httptest.NewRecorder() +// handler := http.HandlerFunc(RemoveHandler) + +// // Call the handler +// handler.ServeHTTP(rr, req) + +// // Check the status code +// if status := rr.Code; status != http.StatusInternalServerError { +// t.Errorf("handler returned wrong status code: got %v want %v", +// status, http.StatusInternalServerError) +// } +// } 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" {