From 310909788b01781171ead2479c11b20f5a1e136a Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 6 Apr 2021 18:27:06 +0100 Subject: [PATCH] Support for deleting files Closes #85 --- git.go | 14 ++++++++++- handlers_file.go | 35 ++++++++++++++++++++++++++ handlers_page.go | 3 ++- main.go | 2 ++ resources/templates/delete_file.gohtml | 12 +++++++++ resources/templates/listfiles.gohtml | 2 +- templates.go | 12 +++++++++ 7 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 resources/templates/delete_file.gohtml diff --git a/git.go b/git.go index b5dc4be..c1db2dc 100644 --- a/git.go +++ b/git.go @@ -351,7 +351,19 @@ func (g *GitBackend) DeletePage(name string, message string, user string) error g.mutex.Lock() defer g.mutex.Unlock() - _, gitPath, err := resolvePath(g.dir, fmt.Sprintf("%s.md", name)) + return g.delete(fmt.Sprintf("%s.md", name), message, user) +} + + +func (g *GitBackend) DeleteFile(name string, message string, user string) error { + g.mutex.Lock() + defer g.mutex.Unlock() + + return g.delete(name, message, user) +} + +func (g *GitBackend) delete(name, message, user string) error { + _, gitPath, err := resolvePath(g.dir, name) if err != nil { return err } diff --git a/handlers_file.go b/handlers_file.go index 871ceb2..86080b4 100644 --- a/handlers_file.go +++ b/handlers_file.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "log" "mime" @@ -105,3 +106,37 @@ func FileHandler(provider FileProvider) http.HandlerFunc { _, _ = io.Copy(writer, reader) } } + +type DeleteFileProvider interface { + DeleteFile(name string, message string, user string) error +} + +func DeleteFileConfirmHandler(t *Templates) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + name := strings.TrimPrefix(r.URL.Path, "/files/delete/") + t.RenderDeleteFile(w, r, name) + } +} + +func DeleteFileHandler(provider DeleteFileProvider) http.HandlerFunc { + return func(writer http.ResponseWriter, request *http.Request) { + name := strings.TrimPrefix(request.URL.Path, "/files/delete/") + confirm := request.FormValue("confirm") + if confirm == "" { + http.Redirect(writer, request, "/files/delete/"+name, http.StatusTemporaryRedirect) + return + } + message := request.FormValue("message") + username := "Anonymoose" + if user := getUserForRequest(request); user != nil { + username = user.Name + } + err := provider.DeleteFile(name, message, username) + if err != nil { + writer.WriteHeader(http.StatusInternalServerError) + return + } + putSessionKey(writer, request, sessionNoticeKey, fmt.Sprintf("Deleted file %s", name)) + http.Redirect(writer, request, "/", http.StatusTemporaryRedirect) + } +} diff --git a/handlers_page.go b/handlers_page.go index 82dac21..d59c7da 100644 --- a/handlers_page.go +++ b/handlers_page.go @@ -111,7 +111,8 @@ func DeletePageHandler(provider DeletePageProvider) http.HandlerFunc { writer.WriteHeader(http.StatusInternalServerError) return } - http.Redirect(writer, request, "/", http.StatusOK) + putSessionKey(writer, request, sessionNoticeKey, fmt.Sprintf("Deleted page %s", name)) + http.Redirect(writer, request, "/", http.StatusTemporaryRedirect) } } diff --git a/main.go b/main.go index ad18b0e..4b9f338 100644 --- a/main.go +++ b/main.go @@ -128,6 +128,8 @@ func main() { wikiRouter.PathPrefix("/view/").Handler(read(ViewPageHandler(templates, renderer, gitBackend))).Methods(http.MethodGet) wikiRouter.PathPrefix("/history/").Handler(read(PageHistoryHandler(templates, gitBackend))).Methods(http.MethodGet) wikiRouter.PathPrefix("/files/view/").Handler(read(FileHandler(gitBackend))).Methods(http.MethodGet) + wikiRouter.PathPrefix("/files/delete/").Handler(write(DeleteFileConfirmHandler(templates))).Methods(http.MethodGet) + wikiRouter.PathPrefix("/files/delete/").Handler(write(DeleteFileHandler(gitBackend))).Methods(http.MethodPost) wikiRouter.PathPrefix("/delete/").Handler(write(DeletePageConfirmHandler(templates))).Methods(http.MethodGet) wikiRouter.PathPrefix("/delete/").Handler(write(DeletePageHandler(gitBackend))).Methods(http.MethodPost) wikiRouter.PathPrefix("/rename/").Handler(write(RenamePageConfirmHandler(gitBackend, templates))).Methods(http.MethodGet) diff --git a/resources/templates/delete_file.gohtml b/resources/templates/delete_file.gohtml new file mode 100644 index 0000000..8a6b242 --- /dev/null +++ b/resources/templates/delete_file.gohtml @@ -0,0 +1,12 @@ +{{- /*gotype: github.com/mdbot/wiki.DeleteFileArgs*/ -}} +{{template "header" .Common}} +
+ {{.Common.CsrfField}} + +
+ + +
+ +
+{{template "footer" .Common}} diff --git a/resources/templates/listfiles.gohtml b/resources/templates/listfiles.gohtml index 024147d..13afe7e 100644 --- a/resources/templates/listfiles.gohtml +++ b/resources/templates/listfiles.gohtml @@ -3,7 +3,7 @@ All files: {{template "footer" .Common}} diff --git a/templates.go b/templates.go index 8034580..818ebc3 100644 --- a/templates.go +++ b/templates.go @@ -131,6 +131,18 @@ func (t *Templates) RenderFileList(w http.ResponseWriter, r *http.Request, files }) } +type DeleteFileArgs struct { + Common CommonArgs +} + +func (t *Templates) RenderDeleteFile(w http.ResponseWriter, r *http.Request, fileName string) { + t.render("delete_file.gohtml", http.StatusOK, w, &DeleteFileArgs{ + Common: t.populateArgs(w, r, CommonArgs{ + PageTitle: fileName, + }), + }) +} + type UploadFileArgs struct { Common CommonArgs }