Add link previews
This commit is contained in:
@@ -22,11 +22,6 @@ func writeJSON(w http.ResponseWriter, status int, v any) {
|
||||
w.Write(append(b, '\n'))
|
||||
}
|
||||
|
||||
// absBase is the absolute URL of this service, for links and examples.
|
||||
//
|
||||
// It prefers --public-url. Falling back to the request's Host is display-only:
|
||||
// the header is caller-controlled, so it is never used for anything a third
|
||||
// party would see.
|
||||
func (s *Server) absBase(r *http.Request) string {
|
||||
if s.cfg.PublicURL != "" {
|
||||
return s.cfg.PublicURL + s.cfg.BasePath
|
||||
@@ -38,11 +33,14 @@ func (s *Server) absBase(r *http.Request) string {
|
||||
return scheme + "://" + r.Host + s.cfg.BasePath
|
||||
}
|
||||
|
||||
// objectURL builds the download link handed back to an uploader.
|
||||
func (s *Server) objectURL(r *http.Request, id string) string {
|
||||
return s.absBase(r) + "d/" + id
|
||||
}
|
||||
|
||||
func (s *Server) infoURL(r *http.Request, id string) string {
|
||||
return s.absBase(r) + "i/" + id
|
||||
}
|
||||
|
||||
type indexPage struct {
|
||||
page
|
||||
MaxSize string
|
||||
@@ -60,8 +58,6 @@ type indexPage struct {
|
||||
}
|
||||
|
||||
func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
// A remembered token is resolved server-side, so the page can show the real
|
||||
// limits without the cookie ever being readable by a script.
|
||||
lim, err := s.limitsFor(r, s.cookieCredential(r))
|
||||
stale := false
|
||||
switch {
|
||||
@@ -124,12 +120,19 @@ func (s *Server) handleInfo(w http.ResponseWriter, r *http.Request) {
|
||||
// delete attempt, so a mistyped token lands back on the form rather than on a
|
||||
// dead end.
|
||||
func (s *Server) renderInfo(w http.ResponseWriter, r *http.Request, m *store.Meta, status int, errMsg string) {
|
||||
infoURL := s.infoURL(r, m.ID)
|
||||
head := s.page(r, m.Filename, true)
|
||||
// This is the page people paste at each other, so it is the one that has
|
||||
// to unfurl into something readable.
|
||||
head.Preview = objectPreview(m, infoURL)
|
||||
|
||||
s.render(w, status, "info.html", objectPage{
|
||||
page: s.page(r, m.Filename, true),
|
||||
page: head,
|
||||
Meta: m,
|
||||
Size: config.FormatSize(m.Size),
|
||||
Expires: describeExpiry(m.Expires, s.now()),
|
||||
URL: s.objectURL(r, m.ID),
|
||||
InfoURL: infoURL,
|
||||
CanDelete: s.mayDelete(r, m, s.credential(r)),
|
||||
Error: errMsg,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user