lens @ 5f660b309bc695277c67223520499fcc13f3c59f

  1package view
  2
  3import (
  4	"net/http"
  5	"strconv"
  6
  7	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
  8	"git.sr.ht/~gabrielgio/img/pkg/ext"
  9	"git.sr.ht/~gabrielgio/img/templates"
 10)
 11
 12type (
 13	MediaView struct {
 14		mediaRepository    repository.MediaRepository
 15		userRepository     repository.UserRepository
 16		settingsRepository repository.SettingsRepository
 17	}
 18)
 19
 20func getPagination(w http.ResponseWriter, r *http.Request) *repository.Pagination {
 21	var (
 22		size    int
 23		page    int
 24		sizeStr = r.FormValue("size")
 25		pageStr = r.FormValue("page")
 26	)
 27
 28	if sizeStr == "" {
 29		size = 100
 30	} else if s, err := strconv.Atoi(sizeStr); err != nil {
 31		size = 100
 32	} else {
 33		size = s
 34	}
 35
 36	if pageStr == "" {
 37		page = 0
 38	} else if p, err := strconv.Atoi(pageStr); err != nil {
 39		page = 0
 40	} else {
 41		page = p
 42	}
 43
 44	return &repository.Pagination{
 45		Page: page,
 46		Size: size,
 47	}
 48}
 49
 50func NewMediaView(
 51	mediaRepository repository.MediaRepository,
 52	userRepository repository.UserRepository,
 53	settingsRepository repository.SettingsRepository,
 54) *MediaView {
 55	return &MediaView{
 56		mediaRepository:    mediaRepository,
 57		userRepository:     userRepository,
 58		settingsRepository: settingsRepository,
 59	}
 60}
 61
 62func (self *MediaView) Index(w http.ResponseWriter, r *http.Request) error {
 63	p := getPagination(w, r)
 64	token := ext.GetTokenFromCtx(w, r)
 65
 66	userPath, err := self.userRepository.GetPathFromUserID(r.Context(), token.UserID)
 67	if err != nil {
 68		return err
 69	}
 70
 71	p.Path = userPath
 72	medias, err := self.mediaRepository.List(r.Context(), p)
 73	if err != nil {
 74		return err
 75	}
 76
 77	settings, err := self.settingsRepository.Load(r.Context())
 78	if err != nil {
 79		return err
 80	}
 81
 82	page := &templates.MediaPage{
 83		Medias: medias,
 84		Next: &repository.Pagination{
 85			Size: p.Size,
 86			Page: p.Page + 1,
 87		},
 88		Settings: settings,
 89	}
 90
 91	templates.WritePageTemplate(w, page)
 92
 93	return nil
 94}
 95
 96func (self *MediaView) GetImage(w http.ResponseWriter, r *http.Request) error {
 97	pathHash := r.FormValue("path_hash")
 98
 99	media, err := self.mediaRepository.Get(r.Context(), pathHash)
100	if err != nil {
101		return err
102	}
103
104	w.Header().Set("Content-Type", media.MIMEType)
105	http.ServeFile(w, r, media.Path)
106	return nil
107}
108
109func (self *MediaView) GetThumbnail(w http.ResponseWriter, r *http.Request) error {
110	pathHash := r.FormValue("path_hash")
111
112	path, err := self.mediaRepository.GetThumbnailPath(r.Context(), pathHash)
113	if err != nil {
114		http.Redirect(w, r, "/media/image?path_hash="+pathHash, http.StatusTemporaryRedirect)
115		// nolint: nilerr
116		return nil
117	}
118
119	w.Header().Set("Content-Type", "image/jpeg")
120	http.ServeFile(w, r, path)
121	return nil
122}
123
124func (self *MediaView) SetMyselfIn(r *ext.Router) {
125	r.GET("/media/", self.Index)
126	r.POST("/media/", self.Index)
127
128	r.GET("/media/image/", self.GetImage)
129	r.GET("/media/thumbnail/", self.GetThumbnail)
130}