lens @ 398ae343e6a33fb81f82380b17081b8f9bae328a

 1package view
 2
 3import (
 4	"github.com/valyala/fasthttp"
 5
 6	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
 7	"git.sr.ht/~gabrielgio/img/pkg/ext"
 8	"git.sr.ht/~gabrielgio/img/pkg/service"
 9	"git.sr.ht/~gabrielgio/img/templates"
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	templates.WritePageTemplate(ctx, &templates.FilePage{
49		Page:      page,
50		ShowMode:  settings.ShowMode,
51		ShowOwner: settings.ShowOwner,
52	})
53
54	return nil
55}
56
57func (self *FileSystemView) SetMyselfIn(r *ext.Router) {
58	r.GET("/", self.Index)
59	r.POST("/", self.Index)
60	r.GET("/fs/", self.Index)
61	r.POST("/fs/", self.Index)
62}