aboutsummaryrefslogtreecommitdiff
path: root/pkg/view/media.go
blob: 6d380e25a294884a3bc8de9e9c090b1bd33bb31a (plain)
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
package view

import (
	"strconv"

	"github.com/valyala/fasthttp"

	"git.sr.ht/~gabrielgio/img"
	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
	"git.sr.ht/~gabrielgio/img/pkg/ext"
)

type (
	MediaView struct {
		mediaRepository repository.MediaRepository
		userRepository  repository.UserRepository
	}

	Page struct {
		Medias []*repository.Media
		Next   *repository.Pagination
	}
)

func getPagination(ctx *fasthttp.RequestCtx) *repository.Pagination {
	var (
		size    int
		page    int
		sizeStr = string(ctx.FormValue("size"))
		pageStr = string(ctx.FormValue("page"))
	)

	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
	}

	return &repository.Pagination{
		Page: page,
		Size: size,
	}
}

func NewMediaView(
	mediaRepository repository.MediaRepository,
	userRepository repository.UserRepository,
) *MediaView {
	return &MediaView{
		mediaRepository: mediaRepository,
		userRepository:  userRepository,
	}
}

func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error {
	p := getPagination(ctx)
	token := ext.GetTokenFromCtx(ctx)

	userPath, err := self.userRepository.GetPathFromUserID(ctx, token.UserID)
	if err != nil {
		return err
	}

	p.Path = userPath
	medias, err := self.mediaRepository.List(ctx, p)
	if err != nil {
		return err
	}

	err = img.Render(ctx, "media.html", &img.HTMLView[*Page]{
		Title: "Media",
		Data: &Page{
			Medias: medias,
			Next: &repository.Pagination{
				Size: p.Size,
				Page: p.Page + 1,
			},
		},
	})
	if err != nil {
		return err
	}
	return nil
}

func (self *MediaView) GetImage(ctx *fasthttp.RequestCtx) error {
	pathHash := string(ctx.FormValue("path_hash"))

	media, err := self.mediaRepository.Get(ctx, pathHash)
	if err != nil {
		return err
	}

	ctx.Response.Header.SetContentType(media.MIMEType)
	fasthttp.ServeFileUncompressed(ctx, media.Path)
	return nil
}

func (self *MediaView) GetThumbnail(ctx *fasthttp.RequestCtx) error {
	pathHash := string(ctx.FormValue("path_hash"))

	path, err := self.mediaRepository.GetThumbnailPath(ctx, pathHash)
	if err != nil {
		ctx.Redirect("/media/image?path_hash="+pathHash, 307)
		// nolint: nilerr
		return nil
	}

	ctx.Request.Header.SetContentType("image/jpeg")
	fasthttp.ServeFileUncompressed(ctx, 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)
}