dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 49795529fe37c53a1bdc53926a573989b2daed9b
parent 58ace677efeb1e9ef1b1375ed8511b7b44786920
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed,  1 Feb 2023 22:17:00 -0800

delete user download from admin interface (to remove captcha)

Diffstat:
Mpkg/database/tableDownloads.go | 5+++++
Mpkg/web/handlers/admin.go | 13+++++++++++++
Mpkg/web/public/views/pages/admin/downloads.gohtml | 27+++++++++++++++++----------
Mpkg/web/web.go | 1+
4 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/pkg/database/tableDownloads.go b/pkg/database/tableDownloads.go @@ -25,3 +25,8 @@ func UserNbDownloaded(userID UserID, filename string) (out int64) { DB.Table("downloads").Where("user_id = ? AND filename = ?", userID, filename).Count(&out) return } + +func DeleteDownloadByID(downloadID int64) (err error) { + err = DB.Unscoped().Delete(Download{}, "id = ?", downloadID).Error + return +} diff --git a/pkg/web/handlers/admin.go b/pkg/web/handlers/admin.go @@ -184,6 +184,19 @@ func AdminDownloadsHandler(c echo.Context) error { return c.Render(http.StatusOK, "admin.downloads", data) } +func AdminDeleteDownloadHandler(c echo.Context) error { + downloadID, err := utils.ParseInt64(c.Param("downloadID")) + if err != nil { + return c.Render(http.StatusOK, "flash", + FlashResponse{"download id not found", c.Request().Referer(), "alert-danger"}) + } + + if err := database.DeleteDownloadByID(downloadID); err != nil { + logrus.Error(err) + } + return c.Redirect(http.StatusFound, c.Request().Referer()) +} + // AdminSettingsHandler ... func AdminSettingsHandler(c echo.Context) error { var data adminSettingsData diff --git a/pkg/web/public/views/pages/admin/downloads.gohtml b/pkg/web/public/views/pages/admin/downloads.gohtml @@ -6,21 +6,28 @@ <th>File</th> <th>User</th> <th>Created at</th> + <th class="text-right" style="width: 70px;">Actions</th> </tr> </thead> <tbody> {{ range .Data.Downloads }} - <tr> - <td>{{ .Filename }}</td> - <td> - <a href="?u={{ .UserID }}">{{ .User.Username }}</a> - </td> - <td>{{ .CreatedAt.Format "Jan 02, 2006 15:04:05" }}</td> - </tr> + <tr> + <td>{{ .Filename }}</td> + <td> + <a href="?u={{ .UserID }}">{{ .User.Username }}</a> + </td> + <td>{{ .CreatedAt.Format "Jan 02, 2006 15:04:05" }}</td> + <td class="text-right"> + <form class="d-inline" action="/admin/downloads/{{ .ID }}/delete" method="post"> + <input type="hidden" name="csrf" value="{{ $.CSRF }}" /> + <button type="submit" class="btn btn-danger btn-sm" title="Delete">X</button> + </form> + </td> + </tr> {{ else }} - <tr> - <td colspan="2"><em>No invoices to display</em></td> - </tr> + <tr> + <td colspan="2"><em>No downloads to display</em></td> + </tr> {{ end }} </tbody> </table> diff --git a/pkg/web/web.go b/pkg/web/web.go @@ -251,6 +251,7 @@ func getMainServer(i18nBundle *i18n.Bundle, renderer *tmp.Templates) echo.Handle adminGroup.GET("/file-drop/:uuid/tmp-reconstruct", handlers.FileDropTmpReconstructHandler) adminGroup.GET("/admin/file-drop/:filename", handlers.FiledropDownloadHandler) adminGroup.GET("/admin/downloads", handlers.AdminDownloadsHandler) + adminGroup.POST("/admin/downloads/:downloadID/delete", handlers.AdminDeleteDownloadHandler) adminGroup.GET("/admin/gists", handlers.AdminGistsHandler) adminGroup.GET("/admin/gists/new", handlers.AdminNewGistHandler) adminGroup.POST("/admin/gists/new", handlers.AdminNewGistHandler)