From 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 5 Aug 2023 19:26:29 +0200 Subject: ref: Move net/http I was young and naive, fasthttp does not fit my needs and make development slower. I'll move to net/http since it has a wider support and will spare some time on implementation detail things (like CSRF). It will allow me to reduce a bit of the code since there may be lib for handling cookie encryption and auth in general. --- pkg/view/media.go | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) (limited to 'pkg/view/media.go') diff --git a/pkg/view/media.go b/pkg/view/media.go index 6e34fd6..c7d84ec 100644 --- a/pkg/view/media.go +++ b/pkg/view/media.go @@ -1,10 +1,9 @@ package view import ( + "net/http" "strconv" - "github.com/valyala/fasthttp" - "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" "git.sr.ht/~gabrielgio/img/templates" @@ -18,12 +17,12 @@ type ( } ) -func getPagination(ctx *fasthttp.RequestCtx) *repository.Pagination { +func getPagination(w http.ResponseWriter, r *http.Request) *repository.Pagination { var ( size int page int - sizeStr = string(ctx.FormValue("size")) - pageStr = string(ctx.FormValue("page")) + sizeStr = r.FormValue("size") + pageStr = r.FormValue("page") ) if sizeStr == "" { @@ -60,22 +59,22 @@ func NewMediaView( } } -func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error { - p := getPagination(ctx) - token := ext.GetTokenFromCtx(ctx) +func (self *MediaView) Index(w http.ResponseWriter, r *http.Request) error { + p := getPagination(w, r) + token := ext.GetTokenFromCtx(w, r) - userPath, err := self.userRepository.GetPathFromUserID(ctx, token.UserID) + userPath, err := self.userRepository.GetPathFromUserID(r.Context(), token.UserID) if err != nil { return err } p.Path = userPath - medias, err := self.mediaRepository.List(ctx, p) + medias, err := self.mediaRepository.List(r.Context(), p) if err != nil { return err } - settings, err := self.settingsRepository.Load(ctx) + settings, err := self.settingsRepository.Load(r.Context()) if err != nil { return err } @@ -89,43 +88,43 @@ func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error { Settings: settings, } - templates.WritePageTemplate(ctx, page) + templates.WritePageTemplate(w, page) return nil } -func (self *MediaView) GetImage(ctx *fasthttp.RequestCtx) error { - pathHash := string(ctx.FormValue("path_hash")) +func (self *MediaView) GetImage(w http.ResponseWriter, r *http.Request) error { + pathHash := r.FormValue("path_hash") - media, err := self.mediaRepository.Get(ctx, pathHash) + media, err := self.mediaRepository.Get(r.Context(), pathHash) if err != nil { return err } - ctx.Response.Header.SetContentType(media.MIMEType) - fasthttp.ServeFileUncompressed(ctx, media.Path) + w.Header().Set("Content-Type", media.MIMEType) + http.ServeFile(w, r, media.Path) return nil } -func (self *MediaView) GetThumbnail(ctx *fasthttp.RequestCtx) error { - pathHash := string(ctx.FormValue("path_hash")) +func (self *MediaView) GetThumbnail(w http.ResponseWriter, r *http.Request) error { + pathHash := r.FormValue("path_hash") - path, err := self.mediaRepository.GetThumbnailPath(ctx, pathHash) + path, err := self.mediaRepository.GetThumbnailPath(r.Context(), pathHash) if err != nil { - ctx.Redirect("/media/image?path_hash="+pathHash, 307) + http.Redirect(w, r, "/media/image?path_hash="+pathHash, http.StatusTemporaryRedirect) // nolint: nilerr return nil } - ctx.Request.Header.SetContentType("image/jpeg") - fasthttp.ServeFileUncompressed(ctx, path) + w.Header().Set("Content-Type", "image/jpeg") + http.ServeFile(w, r, path) return nil } func (self *MediaView) SetMyselfIn(r *ext.Router) { - r.GET("/media", self.Index) - r.POST("/media", self.Index) + r.GET("/media/", self.Index) + r.POST("/media/", self.Index) - r.GET("/media/image", self.GetImage) - r.GET("/media/thumbnail", self.GetThumbnail) + r.GET("/media/image/", self.GetImage) + r.GET("/media/thumbnail/", self.GetThumbnail) } -- cgit v1.2.3