dkforest

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

commit 62e02c9e36c34384fcb656b63208541a28c36622
parent e92b51fcb6645dd76aa9493242e3331f6ed7b650
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 12 Jun 2023 20:49:11 -0700

move code

Diffstat:
Mpkg/web/handlers/admin.go | 29+++++++++++++++++++++++++++++
Mpkg/web/handlers/handlers.go | 27---------------------------
2 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/pkg/web/handlers/admin.go b/pkg/web/handlers/admin.go @@ -4,7 +4,9 @@ import ( dutils "dkforest/pkg/database/utils" "dkforest/pkg/managers" "dkforest/pkg/web/handlers/interceptors" + "fmt" "github.com/jinzhu/gorm" + "io" "net/http" "regexp" "strings" @@ -707,3 +709,30 @@ func StreamUsersHandler(c echo.Context) error { } return c.String(http.StatusOK, out) } + +func FiledropDownloadHandler(c echo.Context) error { + filename := c.Param("filename") + db := c.Get("database").(*database.DkfDB) + filedrop, err := db.GetFiledropByFileName(filename) + if err != nil { + return c.Redirect(http.StatusFound, "/") + } + if !filedrop.Exists() { + logrus.Error(filename + " does not exists") + return c.Redirect(http.StatusFound, "/") + } + + osFile, decrypter, err := filedrop.GetContent() + if err != nil { + return echo.NotFoundHandler(c) + } + defer osFile.Close() + + c.Response().Header().Set(echo.HeaderContentDisposition, fmt.Sprintf("%s; filename=%q", "attachment", filedrop.OrigFileName)) + + if _, err := io.Copy(c.Response().Writer, decrypter); err != nil { + logrus.Error(err) + } + c.Response().Flush() + return nil +} diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -855,33 +855,6 @@ func UploadsDownloadHandler(c echo.Context) error { return nil } -func FiledropDownloadHandler(c echo.Context) error { - filename := c.Param("filename") - db := c.Get("database").(*database.DkfDB) - filedrop, err := db.GetFiledropByFileName(filename) - if err != nil { - return c.Redirect(http.StatusFound, "/") - } - if !filedrop.Exists() { - logrus.Error(filename + " does not exists") - return c.Redirect(http.StatusFound, "/") - } - - osFile, decrypter, err := filedrop.GetContent() - if err != nil { - return echo.NotFoundHandler(c) - } - defer osFile.Close() - - c.Response().Header().Set(echo.HeaderContentDisposition, fmt.Sprintf("%s; filename=%q", "attachment", filedrop.OrigFileName)) - - if _, err := io.Copy(c.Response().Writer, decrypter); err != nil { - logrus.Error(err) - } - c.Response().Flush() - return nil -} - func GetFileContentType(out io.ReadSeeker) (string, error) { // Only the first 512 bytes are used to sniff the content type.