1package view
2
3import (
4 "net/http"
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(w http.ResponseWriter, r *http.Request) error {
35 var (
36 pathValue = r.FormValue("path")
37 token = ext.GetTokenFromCtx(r)
38 )
39
40 page, err := self.fsService.GetPage(r.Context(), token.UserID, pathValue)
41 if err != nil {
42 return err
43 }
44
45 settings, err := self.settings.Load(r.Context())
46 if err != nil {
47 return err
48 }
49
50 templates.WritePageTemplate(w, &templates.FilePage{
51 Page: page,
52 ShowMode: settings.ShowMode,
53 ShowOwner: settings.ShowOwner,
54 })
55
56 return nil
57}
58
59func (self *FileSystemView) SetMyselfIn(r *ext.Router) {
60 r.GET("/", self.Index)
61 r.POST("/", self.Index)
62 r.GET("/fs/", self.Index)
63 r.POST("/fs/", self.Index)
64}