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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
package view
import (
"net/http"
"strconv"
"git.sr.ht/~gabrielgio/img/pkg/database/repository"
"git.sr.ht/~gabrielgio/img/pkg/ext"
"git.sr.ht/~gabrielgio/img/templates"
)
type (
MediaView struct {
mediaRepository repository.MediaRepository
userRepository repository.UserRepository
settingsRepository repository.SettingsRepository
}
)
func getPagination(r *http.Request) *repository.Pagination {
var (
size int
page int
albumID *uint
sizeStr = r.FormValue("size")
pageStr = r.FormValue("page")
albumIDStr = r.FormValue("albumId")
)
if sizeStr == "" {
size = 100
} else if s, err := strconv.Atoi(sizeStr); err != nil {
size = 100
} else {
size = s
}
if pageStr == "" {
page = 0
} else if p, err := strconv.Atoi(pageStr); err != nil {
page = 0
} else {
page = p
}
if albumIDStr == "" {
albumID = nil
} else if p, err := strconv.Atoi(albumIDStr); err == nil {
id := uint(p)
albumID = &id
}
return &repository.Pagination{
Page: page,
Size: size,
AlbumID: albumID,
}
}
func NewMediaView(
mediaRepository repository.MediaRepository,
userRepository repository.UserRepository,
settingsRepository repository.SettingsRepository,
) *MediaView {
return &MediaView{
mediaRepository: mediaRepository,
userRepository: userRepository,
settingsRepository: settingsRepository,
}
}
func (self *MediaView) Index(w http.ResponseWriter, r *http.Request) error {
p := getPagination(r)
user := ext.GetUserFromCtx(r)
userPath, err := self.userRepository.GetPathFromUserID(r.Context(), user.ID)
if err != nil {
return err
}
p.Path = userPath
medias, err := self.mediaRepository.List(r.Context(), p)
if err != nil {
return err
}
settings, err := self.settingsRepository.Load(r.Context())
if err != nil {
return err
}
page := &templates.MediaPage{
Medias: medias,
Next: &repository.Pagination{
Size: p.Size,
Page: p.Page + 1,
},
Settings: settings,
}
templates.WritePageTemplate(w, page, user.IsAdmin)
return nil
}
func (self *MediaView) GetImage(w http.ResponseWriter, r *http.Request) error {
pathHash := r.FormValue("path_hash")
media, err := self.mediaRepository.Get(r.Context(), pathHash)
if err != nil {
return err
}
w.Header().Set("Content-Type", media.MIMEType)
http.ServeFile(w, r, media.Path)
return nil
}
func (self *MediaView) GetThumbnail(w http.ResponseWriter, r *http.Request) error {
pathHash := r.FormValue("path_hash")
path, err := self.mediaRepository.GetThumbnailPath(r.Context(), pathHash)
if err != nil {
http.Redirect(w, r, "/media/image?path_hash="+pathHash, http.StatusTemporaryRedirect)
// nolint: nilerr
return nil
}
w.Header().Set("Content-Type", "image/jpeg")
http.ServeFile(w, r, path)
return nil
}
func (self *MediaView) SetMyselfIn(r *ext.Router) {
r.GET("/media", self.Index)
r.POST("/media", self.Index)
r.GET("/media/image", self.GetImage)
r.GET("/media/thumbnail", self.GetThumbnail)
}
|