lens @ c19bd326ab93e79bbeebc32ad0a8efa2ed48d1b5

 1package view
 2
 3import (
 4	"github.com/valyala/fasthttp"
 5
 6	"git.sr.ht/~gabrielgio/img"
 7	"git.sr.ht/~gabrielgio/img/pkg/components/filesystem"
 8	"git.sr.ht/~gabrielgio/img/pkg/components/settings"
 9	"git.sr.ht/~gabrielgio/img/pkg/ext"
10)
11
12type (
13	FileSystemView struct {
14		controller filesystem.Controller
15		settings   settings.Repository
16	}
17	FilePage struct {
18		Page      *filesystem.Page
19		ShowMode  bool
20		ShowOwner bool
21	}
22)
23
24func NewFileSystemView(
25	controller filesystem.Controller,
26	settingsRepository settings.Repository,
27) *FileSystemView {
28	return &FileSystemView{
29		controller: controller,
30		settings:   settingsRepository,
31	}
32}
33
34func (self *FileSystemView) Index(ctx *fasthttp.RequestCtx) error {
35	pathValue := string(ctx.FormValue("path"))
36
37	page, err := self.controller.GetPage(pathValue)
38	if err != nil {
39		return err
40	}
41
42	settings, err := self.settings.Load(ctx)
43	if err != nil {
44		return err
45	}
46
47	err = img.Render(ctx, "fs.html", &img.HTMLView[*FilePage]{
48		Title: pathValue,
49		Data: &FilePage{
50			Page:      page,
51			ShowMode:  settings.ShowMode,
52			ShowOwner: settings.ShowOwner,
53		},
54	})
55	if err != nil {
56		return err
57	}
58	return nil
59}
60
61func (self *FileSystemView) SetMyselfIn(r *ext.Router) {
62	r.GET("/", self.Index)
63	r.POST("/", self.Index)
64	r.GET("/fs/", self.Index)
65	r.POST("/fs/", self.Index)
66}