Skip to content

Commit

Permalink
Support for deleting files
Browse files Browse the repository at this point in the history
Closes #85
  • Loading branch information
csmith committed Apr 6, 2021
1 parent 4323404 commit 3109097
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 3 deletions.
14 changes: 13 additions & 1 deletion git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
35 changes: 35 additions & 0 deletions handlers_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"
"log"
"mime"
Expand Down Expand Up @@ -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)
}
}
3 changes: 2 additions & 1 deletion handlers_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions resources/templates/delete_file.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{- /*gotype: github.com/mdbot/wiki.DeleteFileArgs*/ -}}
{{template "header" .Common}}
<form action="/files/delete/{{.Common.PageTitle}}" method="post" class="editor">
{{.Common.CsrfField}}
<input type="hidden" id="confirm" name="confirm" value="confirm" />
<div class="form-group">
<label for="message">Reason:</label>
<input id="message" type="text" name="message">
</div>
<button type="submit" class="btn btn-primary" value="Edit">Confirm delete</button>
</form>
{{template "footer" .Common}}
2 changes: 1 addition & 1 deletion resources/templates/listfiles.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
All files:
<ul>
{{range .Files}}
<li><a href="/files/view/{{.Name}}">{{.Name}}</a> ({{.Size | bytes}})</li>
<li><a href="/files/view/{{.Name}}">{{.Name}}</a> ({{.Size | bytes}}) [<a href="/files/delete/{{.Name}}">delete</a>]</li>
{{end}}
</ul>
{{template "footer" .Common}}
12 changes: 12 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 3109097

Please sign in to comment.