diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-02-26 19:54:48 +0100 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-18 16:30:36 +0200 |
commit | c8e1328164e9ffbd681c3c0e449f1e6b9856b896 (patch) | |
tree | faee639a4c55c5dc3bfc59a5400026822c40221d /pkg/view/media.go | |
download | lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.gz lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.bz2 lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.zip |
feat: Inicial commit
It contains rough template for the server and runners.
It contains rough template for the server and runners.
Diffstat (limited to 'pkg/view/media.go')
-rw-r--r-- | pkg/view/media.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/pkg/view/media.go b/pkg/view/media.go new file mode 100644 index 0000000..22f950d --- /dev/null +++ b/pkg/view/media.go @@ -0,0 +1,101 @@ +package view + +import ( + "strconv" + + "github.com/valyala/fasthttp" + + "git.sr.ht/~gabrielgio/img" + "git.sr.ht/~gabrielgio/img/pkg/components/media" + "git.sr.ht/~gabrielgio/img/pkg/ext" +) + +type ( + MediaView struct { + mediaRepository media.Repository + } + + Page struct { + Medias []*media.Media + Next *media.Pagination + } +) + +func getPagination(ctx *fasthttp.RequestCtx) *media.Pagination { + var ( + size int + page int + sizeStr = string(ctx.FormValue("size")) + pageStr = string(ctx.FormValue("page")) + ) + + if sizeStr == "" { + size = 100 + } else if s, err := strconv.Atoi(sizeStr); err != nil { + size = 100 + } else { + size = s + } + + if pageStr == "" { + page = 0 + } else if p, err := strconv.Atoi(pageStr); err != nil { + page = 0 + } else { + page = p + } + + return &media.Pagination{ + Page: page, + Size: size, + } +} + +func NewMediaView(mediaRepository media.Repository) *MediaView { + return &MediaView{ + mediaRepository: mediaRepository, + } +} + +func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error { + p := getPagination(ctx) + medias, err := self.mediaRepository.List(ctx, p) + if err != nil { + return err + } + + err = img.Render(ctx, "media.html", &img.HTMLView[*Page]{ + Title: "Media", + Data: &Page{ + Medias: medias, + Next: &media.Pagination{ + Size: p.Size, + Page: p.Page + 1, + }, + }, + }) + if err != nil { + return err + } + return nil +} + +func (self *MediaView) GetImage(ctx *fasthttp.RequestCtx) error { + pathHash := string(ctx.FormValue("path_hash")) + + media, err := self.mediaRepository.Get(ctx, pathHash) + if err != nil { + return err + } + + ctx.Response.Header.SetContentType(media.MIMEType) + ctx.SendFile(media.Path) + return nil +} + +func (self *MediaView) SetMyselfIn(r *ext.Router) { + r.GET("/media", self.Index) + r.POST("/media", self.Index) + + r.GET("/media/image", self.GetImage) +} |