lens @ fbcac585cf626917e2baf1d0065c7b632341ba01

 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		fsService 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		fsService: controller,
30		settings:  settingsRepository,
31	}
32}
33
34func (self *FileSystemView) Index(ctx *fasthttp.RequestCtx) error {
35	pathValue := string(ctx.FormValue("path"))
36	token := ext.GetTokenFromCtx(ctx)
37
38	page, err := self.fsService.GetPage(ctx, token.UserID, pathValue)
39	if err != nil {
40		return err
41	}
42
43	settings, err := self.settings.Load(ctx)
44	if err != nil {
45		return err
46	}
47
48	err = img.Render(ctx, "fs.html", &img.HTMLView[*FilePage]{
49		Title: pathValue,
50		Data: &FilePage{
51			Page:      page,
52			ShowMode:  settings.ShowMode,
53			ShowOwner: settings.ShowOwner,
54		},
55	})
56	if err != nil {
57		return err
58	}
59	return nil
60}
61
62func (self *FileSystemView) SetMyselfIn(r *ext.Router) {
63	r.GET("/", self.Index)
64	r.POST("/", self.Index)
65	r.GET("/fs/", self.Index)
66	r.POST("/fs/", self.Index)
67}