lens @ fbd9c07b220283072e43681144a31675634637e8

  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(r *http.Request) *repository.Pagination {
 21	var (
 22		size       int
 23		page       int
 24		albumID    *uint
 25		sizeStr    = r.FormValue("size")
 26		pageStr    = r.FormValue("page")
 27		albumIDStr = r.FormValue("albumId")
 28	)
 29
 30	if sizeStr == "" {
 31		size = 100
 32	} else if s, err := strconv.Atoi(sizeStr); err != nil {
 33		size = 100
 34	} else {
 35		size = s
 36	}
 37
 38	if pageStr == "" {
 39		page = 0
 40	} else if p, err := strconv.Atoi(pageStr); err != nil {
 41		page = 0
 42	} else {
 43		page = p
 44	}
 45
 46	if albumIDStr == "" {
 47		page = 0
 48	} else if p, err := strconv.Atoi(albumIDStr); err == nil {
 49		id := uint(p)
 50		albumID = &id
 51	}
 52
 53	return &repository.Pagination{
 54		Page:    page,
 55		Size:    size,
 56		AlbumID: albumID,
 57	}
 58}
 59
 60func NewMediaView(
 61	mediaRepository repository.MediaRepository,
 62	userRepository repository.UserRepository,
 63	settingsRepository repository.SettingsRepository,
 64) *MediaView {
 65	return &MediaView{
 66		mediaRepository:    mediaRepository,
 67		userRepository:     userRepository,
 68		settingsRepository: settingsRepository,
 69	}
 70}
 71
 72func (self *MediaView) Index(w http.ResponseWriter, r *http.Request) error {
 73	p := getPagination(r)
 74	token := ext.GetTokenFromCtx(w, r)
 75
 76	userPath, err := self.userRepository.GetPathFromUserID(r.Context(), token.UserID)
 77	if err != nil {
 78		return err
 79	}
 80
 81	p.Path = userPath
 82	medias, err := self.mediaRepository.List(r.Context(), p)
 83	if err != nil {
 84		return err
 85	}
 86
 87	settings, err := self.settingsRepository.Load(r.Context())
 88	if err != nil {
 89		return err
 90	}
 91
 92	page := &templates.MediaPage{
 93		Medias: medias,
 94		Next: &repository.Pagination{
 95			Size: p.Size,
 96			Page: p.Page + 1,
 97		},
 98		Settings: settings,
 99	}
100
101	templates.WritePageTemplate(w, page)
102
103	return nil
104}
105
106func (self *MediaView) GetImage(w http.ResponseWriter, r *http.Request) error {
107	pathHash := r.FormValue("path_hash")
108
109	media, err := self.mediaRepository.Get(r.Context(), pathHash)
110	if err != nil {
111		return err
112	}
113
114	w.Header().Set("Content-Type", media.MIMEType)
115	http.ServeFile(w, r, media.Path)
116	return nil
117}
118
119func (self *MediaView) GetThumbnail(w http.ResponseWriter, r *http.Request) error {
120	pathHash := r.FormValue("path_hash")
121
122	path, err := self.mediaRepository.GetThumbnailPath(r.Context(), pathHash)
123	if err != nil {
124		http.Redirect(w, r, "/media/image?path_hash="+pathHash, http.StatusTemporaryRedirect)
125		// nolint: nilerr
126		return nil
127	}
128
129	w.Header().Set("Content-Type", "image/jpeg")
130	http.ServeFile(w, r, path)
131	return nil
132}
133
134func (self *MediaView) SetMyselfIn(r *ext.Router) {
135	r.GET("/media/", self.Index)
136	r.POST("/media/", self.Index)
137
138	r.GET("/media/image/", self.GetImage)
139	r.GET("/media/thumbnail/", self.GetThumbnail)
140}