Fix #221: image pastes scale to fit, no code box, accurate size, raw serves image
CI / test (pull_request) Successful in 49s
CI / docker (pull_request) Skipped

- paste view: image attachments render in a scalable preview (max-height
  min(70vh, 720px), object-fit contain) and the code box is omitted;
  wrap/copy buttons hidden for image pastes
- SizeHuman/stats summary show the attachment size for file pastes
  (row.Content is empty for file pastes, was showing 0 B)
- /raw/{id} on a file paste streams the attachment bytes inline with its
  sniffed mime and filename instead of an empty text/plain body
This commit is contained in:
fen
2026-09-10 14:16:31 -05:00
parent 7c7e60375c
commit 6d9f9592b5
4 changed files with 77 additions and 5 deletions
+25 -2
View File
@@ -92,6 +92,25 @@ func humanSize(n int) string {
return fmt.Sprintf("%.1f MB", float64(n)/(1024*1024))
}
// #221: file pastes store their bytes in the blob store with empty text
// content; the displayed size is the attachment size, not len(Content).
func pasteDisplaySize(row *store.PasteRow, att *store.Attachment) int64 {
if row.Content == "" && att != nil {
return att.Size
}
return int64(len(row.Content))
}
// attachmentIsImage reports whether a paste-view attachment renders as an
// image (same set the paste template previews inline, #221).
func attachmentIsImage(mime string) bool {
switch strings.ToLower(strings.TrimSpace(mime)) {
case "image/png", "image/jpeg", "image/gif", "image/webp":
return true
}
return false
}
func (u *UI) StaticHandler() http.Handler {
sub, _ := fs.Sub(staticFS, "static")
return http.StripPrefix("/static/", http.FileServer(http.FS(sub)))
@@ -245,20 +264,22 @@ func (h *Handlers) renderPaste(w http.ResponseWriter, row *store.PasteRow, justC
if lang == "" {
lang = "text"
}
summary := fmt.Sprintf("%s · %s · %d views · %s", lang, humanSize(len(row.Content)), row.ViewCount, agoString(row.CreatedAt))
// #38: one optional file attachment per paste; nil when none.
attachment, err := h.Store.GetAttachmentForPaste(row.ID)
if err != nil {
http.Error(w, "db error", 500)
return
}
summary := fmt.Sprintf("%s · %s · %d views · %s", lang, humanSize(int(pasteDisplaySize(row, attachment))), row.ViewCount, agoString(row.CreatedAt))
data := map[string]any{
"Page": "paste",
"ID": row.ID,
"Title": row.Title.String,
"Language": row.Language.String,
"StatsSummary": summary,
"SizeHuman": humanSize(len(row.Content)),
// #221: file pastes store bytes in the blob store; report the
// attachment size, not the (empty) text content length.
"SizeHuman": humanSize(int(pasteDisplaySize(row, attachment))),
"HasPassword": row.PasswordHash.Valid,
"BurnAfterRead": row.BurnAfterRead,
"CustomSlug": row.CustomSlug.String,
@@ -279,6 +300,8 @@ func (h *Handlers) renderPaste(w http.ResponseWriter, row *store.PasteRow, justC
"ReadsTotal": int(row.ReadsLimit.Int64),
"JustCreated": justCreated,
"Attachment": attachment,
// #221: tells the template to drop the code box for image pastes
"AttachmentIsImage": attachment != nil && attachmentIsImage(attachment.Mime),
"Host": "this host",
}
h.renderPage(w, "paste.html", data)