lens @ 7a414da9a802d5eeee911b3536790a061e1d7503

 1package view
 2
 3import (
 4	"github.com/valyala/fasthttp"
 5
 6	"git.sr.ht/~gabrielgio/img"
 7	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
 8	"git.sr.ht/~gabrielgio/img/pkg/ext"
 9	"git.sr.ht/~gabrielgio/img/pkg/service"
10)
11
12type (
13	FileSystemView struct {
14		controller service.FileSystemController
15		settings   repository.SettingsRepository
16	}
17	FilePage struct {
18		Page      *service.Page
19		ShowMode  bool
20		ShowOwner bool
21	}
22)
23
24func NewFileSystemView(
25	controller service.FileSystemController,
26	settingsRepository repository.SettingsRepository,
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}