Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add remove logic #8

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 40 additions & 12 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
New File
</a>
</div>
<div id="mkdir" class="inline-block bg-yellow-500 text-white font-bold py-2 px-2 rounded-full mb-3 hover:opacity-60 cursor-pointer">
<div id="mkdir"
class="inline-block bg-yellow-500 text-white font-bold py-2 px-2 rounded-full mb-3 hover:opacity-60 cursor-pointer">
<span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 inline">
<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" />
Expand All @@ -56,7 +58,8 @@
</svg>
</span>
<span>
<input type="text" name="dirname" class="text-black rounded-full px-2 py-1" placeholder="Directory name">
<input type="text" name="dirname" class="text-black rounded-full px-2 py-1"
placeholder="Directory name">
<button type="submit"
class="inline-block bg-yellow-500 text-white w-auto font-bold py-1 px-2 rounded-full hover:opacity-60">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
Expand All @@ -71,19 +74,19 @@
</p>
</div>
{{range .S3Objects}}
<div class="border-b-2 border-slate-400 py-3 hover:opacity-60">
<p>
<div class="border-b-2 border-slate-400 py-3 flex justify-between">
<div>
{{if .IsDir}}
<span class="pl-2 mr-2">
<span class="pl-2 mr-2 hover:opacity-60">
<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="M2.25 12.75V12A2.25 2.25 0 0 1 4.5 9.75h15A2.25 2.25 0 0 1 21.75 12v.75m-8.69-6.44-2.12-2.12a1.5 1.5 0 0 0-1.061-.44H4.5A2.25 2.25 0 0 0 2.25 6v12a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9a2.25 2.25 0 0 0-2.25-2.25h-5.379a1.5 1.5 0 0 1-1.06-.44Z" />
</svg>
</span>
<span><a href="{{.FullPath}}" class="text-lg">{{.Name}}</a></span>
<span class="hover:opacity-60"><a href="{{.FullPath}}" class="text-lg">{{.Name}}</a></span>
{{else}}
<span class="pl-2 mr-2">
<span class="pl-2 mr-2 hover:opacity-60">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"
class="size-6 inline">
<path
Expand All @@ -92,18 +95,43 @@
d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z" />
</svg>
</span>
<span><a href="{{.FullPath}}?action=dl" class="text-lg">{{.Name}}</a></span>
<span class="hover:opacity-60"><a href="{{.FullPath}}?action=dl" class="text-lg">{{.Name}}</a></span>
{{end}}
</p>
</div>
<div>
<a href="#" class="hover:opacity-60 remove-link" data-target="{{.Name}}" data-path="{{.FullPath}}">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
</a>
</div>
</div>
{{end}}
<form id="removeForm" method="post" action="/remove">
<input type="hidden" name="path" value="">
</form>
</div>
</main>
</body>
<script>
document.querySelector("#mkdir").addEventListener("click", () => {
document.querySelector("#mkdirDiv").classList.remove("hidden");
});
});

const remove = (event) => {
event.preventDefault();
const targetName = event.currentTarget.dataset.target;
if (window.confirm(`Are you sure to remove ${targetName}?`)) {
document.querySelector(`#removeForm input[name="path"]`).value = event.currentTarget.dataset.path;
document.querySelector(`#removeForm`).submit();
}
}

document.querySelectorAll('.remove-link').forEach(link => {
link.addEventListener('click', remove);
});
</script>

</html>
</html>
28 changes: 28 additions & 0 deletions web/rm_controller.go
Original file line number Diff line number Diff line change
@@ -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)
}
108 changes: 108 additions & 0 deletions web/rm_controller_test.go
Original file line number Diff line number Diff line change
@@ -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)
// }
// }
14 changes: 0 additions & 14 deletions web/ui_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Loading