summaryrefslogtreecommitdiff
path: root/player-server/internal/api/handlers_share.go
blob: 8e631595a5f2199e03df7de5637ef00b53d6aac5 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package api

import (
	"errors"
	"net/http"
	"strings"
	"time"

	"codeberg.org/snonux/player/internal/service"
	"codeberg.org/snonux/player/internal/web"
)

// ------------------------------------------------------------------
// Share routes
// ------------------------------------------------------------------

func (s *Server) handleCreateShare(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	id, err := pathID(r, "id")
	if err != nil || id == 0 {
		badRequest(w, "invalid media id")
		return
	}
	// Use the injected clock so tests can pin "now" and assert deterministic
	// share-expiry semantics (e.g. assert that expiresAt is exactly
	// ShareDefaultExpiryDays * 24h after the mock clock's T).
	expiresAt := s.clk.Now().Add(time.Duration(s.cfg.ShareDefaultExpiryDays) * 24 * time.Hour)
	share, err := s.media.Share.CreateShare(r.Context(), userIDFromContext(r), id, expiresAt)
	if err != nil {
		handleError(w, err)
		return
	}
	writeJSON(w, http.StatusOK, share)
}

func (s *Server) handleListShares(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	id, err := pathID(r, "id")
	if err != nil || id == 0 {
		badRequest(w, "invalid media id")
		return
	}
	shares, err := s.media.Share.ListShares(r.Context(), id, userIDFromContext(r))
	if err != nil {
		handleError(w, err)
		return
	}
	writeJSON(w, http.StatusOK, shares)
}

func (s *Server) handleRevokeShare(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	token := r.PathValue("token")
	if token == "" {
		badRequest(w, "token required")
		return
	}
	if err := s.media.Share.RevokeShare(r.Context(), token, userIDFromContext(r)); err != nil {
		handleError(w, err)
		return
	}
	writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}

func (s *Server) handleSharePage(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	token := r.PathValue("token")
	res, err := s.media.Share.GetSharedMedia(r.Context(), token)
	if err != nil || res == nil {
		if err != nil && errors.Is(err, service.ErrShareExpired) {
			http.Error(w, "gone", http.StatusGone)
			return
		}
		http.Error(w, "not found", http.StatusNotFound)
		return
	}

	w.Header().Set("Cache-Control", "no-store")
	w.Header().Set("Vary", "Accept")

	accept := r.Header.Get("Accept")
	if strings.Contains(accept, "application/json") {
		writeJSON(w, http.StatusOK, res)
		return
	}

	// Sanitize the media filename before embedding it in the share page to
	// prevent XSS via HTML injection and to cap memory growth from enormous
	// filenames (DoS). SanitizeFileName truncates to MaxFileNameLength runes
	// and HTML-escapes the result; encoding/json also Unicode-escapes </>
	// inside string values, so both layers reinforce each other.
	if res.Media != nil {
		res.Media.FileName = web.SanitizeFileName(res.Media.FileName)
	}

	// Render the HTML view via the dedicated renderer. This keeps the
	// handler focused on transport concerns (status codes, headers) and
	// keeps templating in the internal/web package.
	page, err := s.shareRenderer.Render(res)
	if err != nil {
		s.logger.Error("render share page", "err", err)
		http.Error(w, "internal error", http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	http.ServeContent(w, r, page.Name, page.ModTime, strings.NewReader(page.HTML))
}

func (s *Server) handleShareThumbnail(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	token := r.PathValue("token")
	fr, err := s.media.Share.GetSharedThumbnail(r.Context(), token)
	if err != nil {
		if errors.Is(err, service.ErrShareExpired) {
			http.Error(w, "gone", http.StatusGone)
			return
		}
		if errors.Is(err, service.ErrShareNotFound) || errors.Is(err, service.ErrMediaNotFound) {
			http.Error(w, "not found", http.StatusNotFound)
			return
		}
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if fr == nil {
		http.Error(w, "not found", http.StatusNotFound)
		return
	}
	w.Header().Set("Cache-Control", "no-cache")
	s.serveFileResult(w, r, fr, false)
}

func (s *Server) handleShareStream(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	token := r.PathValue("token")
	res, err := s.media.Share.StreamSharedMedia(r.Context(), token)
	if err != nil {
		if errors.Is(err, service.ErrShareExpired) {
			http.Error(w, "gone", http.StatusGone)
			return
		}
		if errors.Is(err, service.ErrShareNotFound) || errors.Is(err, service.ErrMediaNotFound) {
			http.Error(w, "not found", http.StatusNotFound)
			return
		}
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if res == nil {
		http.Error(w, "not found", http.StatusNotFound)
		return
	}
	s.serveFileResult(w, r, res, false)
}

func (s *Server) handleShareDownload(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	token := r.PathValue("token")
	fr, err := s.media.Share.StreamSharedMedia(r.Context(), token)
	if err != nil {
		if errors.Is(err, service.ErrShareExpired) {
			http.Error(w, "gone", http.StatusGone)
			return
		}
		if errors.Is(err, service.ErrShareNotFound) || errors.Is(err, service.ErrMediaNotFound) {
			http.Error(w, "not found", http.StatusNotFound)
			return
		}
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if fr == nil {
		http.Error(w, "not found", http.StatusNotFound)
		return
	}
	s.serveFileResult(w, r, fr, true)
}

func (s *Server) handleMyShares(w http.ResponseWriter, r *http.Request) {
	if !requireService(w, s.media.Share) {
		return
	}
	shares, err := s.media.Share.ListMyShares(r.Context(), userIDFromContext(r))
	if err != nil {
		handleError(w, err)
		return
	}
	writeJSON(w, http.StatusOK, shares)
}