1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package view
import (
"mime"
"net/http"
"path/filepath"
"git.sr.ht/~gabrielgio/img/pkg/database/repository"
"git.sr.ht/~gabrielgio/img/pkg/ext"
"git.sr.ht/~gabrielgio/img/pkg/service"
"git.sr.ht/~gabrielgio/img/templates"
)
type (
FileSystemView struct {
fsService service.FileSystemController
settings repository.SettingsRepository
}
FilePage struct {
Page *service.Page
ShowMode bool
ShowOwner bool
}
)
func NewFileSystemView(
controller service.FileSystemController,
settingsRepository repository.SettingsRepository,
) *FileSystemView {
return &FileSystemView{
fsService: controller,
settings: settingsRepository,
}
}
func (self *FileSystemView) Index(w http.ResponseWriter, r *http.Request) error {
var (
pathValue = r.FormValue("path")
user = ext.GetUserFromCtx(r)
)
fullpath, err := self.fsService.GetFullpath(r.Context(), user.ID, pathValue)
if err != nil {
return err
}
isFile, err := self.fsService.IsFile(r.Context(), fullpath)
if err != nil {
return err
}
if isFile {
mimetype := mime.TypeByExtension(filepath.Ext(fullpath))
w.Header().Set("Content-Type", mimetype)
http.ServeFile(w, r, fullpath)
return nil
}
page, err := self.fsService.GetPage(r.Context(), pathValue, fullpath)
if err != nil {
return err
}
settings, err := self.settings.Load(r.Context())
if err != nil {
return err
}
templates.WritePageTemplate(w, &templates.FilePage{
Page: page,
ShowMode: settings.ShowMode,
ShowOwner: settings.ShowOwner,
}, user.IsAdmin)
return nil
}
func (self *FileSystemView) SetMyselfIn(r *ext.Router) {
r.GET("/", self.Index)
r.POST("/", self.Index)
r.GET("/fs", self.Index)
r.POST("/fs", self.Index)
}
|