Merge pull request 'fix #221: scale image pastes, clean link pill, raw image view, accurate size' (#224) from fix-221 into dev
This commit was merged in pull request #224.
This commit is contained in:
@@ -91,6 +91,17 @@ func (l *limitReader) Read(p []byte) (int, error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isImageMime reports whether the sniffed mime is a raster image the viewer
|
||||||
|
// can render inline (#221). SVG is excluded: it is forced to text/plain on
|
||||||
|
// serving by the active-content rule and must never render as an image.
|
||||||
|
func isImageMime(mime string) bool {
|
||||||
|
switch mime {
|
||||||
|
case "image/png", "image/jpeg", "image/gif", "image/webp":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// handleCreatePasteMultipart implements POST /api/pastes with
|
// handleCreatePasteMultipart implements POST /api/pastes with
|
||||||
// multipart/form-data (#38). Fields mirror the JSON create path; a 'file'
|
// multipart/form-data (#38). Fields mirror the JSON create path; a 'file'
|
||||||
// part makes the paste a file paste (1 file = 1 paste: if text content is
|
// part makes the paste a file paste (1 file = 1 paste: if text content is
|
||||||
|
|||||||
@@ -451,6 +451,22 @@ func (a *apiServer) handleRaw(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "not found", 404)
|
http.Error(w, "not found", 404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// #221: raw view of an image paste serves the image bytes themselves as
|
||||||
|
// an image, not the (empty) text content.
|
||||||
|
if att, err := a.store.GetAttachmentForPaste(row.ID); err == nil && att != nil && isImageMime(att.Mime) {
|
||||||
|
blobs := a.store.Blobs()
|
||||||
|
if blobs != nil {
|
||||||
|
if blob, err := blobs.Get(row.ID + "/" + att.SHA256); err == nil {
|
||||||
|
defer blob.Close()
|
||||||
|
a.store.IncrementViews(row.ID, "", 0) // raw views always count (#49/#95)
|
||||||
|
w.Header().Set("Content-Type", att.Mime)
|
||||||
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", att.Size))
|
||||||
|
http.ServeContent(w, r, "", time.Unix(att.CreatedAt, 0), blob)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// #34: content_type is attacker-controlled via the create API. Serving it
|
// #34: content_type is attacker-controlled via the create API. Serving it
|
||||||
// verbatim let a paste be stored with text/html (or image/svg+xml) and
|
// verbatim let a paste be stored with text/html (or image/svg+xml) and
|
||||||
// render as active content on this origin when fetched from /raw —
|
// render as active content on this origin when fetched from /raw —
|
||||||
|
|||||||
@@ -813,15 +813,35 @@ button[type="submit"]:focus-visible,
|
|||||||
}
|
}
|
||||||
.file-chip .file-chip-remove:hover { color: var(--danger, #c0392b); }
|
.file-chip .file-chip-remove:hover { color: var(--danger, #c0392b); }
|
||||||
.attachment-bar { display: flex; flex-direction: column; gap: 10px; }
|
.attachment-bar { display: flex; flex-direction: column; gap: 10px; }
|
||||||
.attachment-chip {
|
/* #221: the link pill under an image preview stays a small inline chip,
|
||||||
|
left-aligned under the image, not stretched above it. */
|
||||||
|
.attachment-bar .attachment-chip {
|
||||||
display: inline-flex; align-items: center; gap: 12px; align-self: flex-start;
|
display: inline-flex; align-items: center; gap: 12px; align-self: flex-start;
|
||||||
border: 1px solid var(--border); border-radius: var(--radius);
|
border: 1px solid var(--border); border-radius: var(--radius);
|
||||||
padding: 8px 16px; text-decoration: none; color: var(--fg);
|
padding: 6px 14px; text-decoration: none; color: var(--fg);
|
||||||
background: var(--surface-2); font-size: 21.6px;
|
background: var(--surface-2); font-size: 19px;
|
||||||
|
}
|
||||||
|
.attachment-bar .attachment-chip:hover { border-color: var(--accent); }
|
||||||
|
.attachment-bar .attachment-chip .attachment-name {
|
||||||
|
max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||||
|
}
|
||||||
|
/* #221: images scale to fit the viewer box, aspect ratio preserved. */
|
||||||
|
.attachment-preview {
|
||||||
|
max-width: 100%;
|
||||||
|
align-self: flex-start;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
background: var(--surface-2);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.attachment-preview img {
|
||||||
|
display: block;
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 70vh;
|
||||||
|
width: auto;
|
||||||
|
height: auto;
|
||||||
|
object-fit: contain;
|
||||||
}
|
}
|
||||||
.attachment-chip:hover { border-color: var(--accent); }
|
|
||||||
.attachment-chip .attachment-size { color: var(--muted-fg); font-size: 19px; }
|
|
||||||
.attachment-preview img { max-width: 480px; max-height: 360px; border-radius: var(--radius); border: 1px solid var(--border); }
|
|
||||||
|
|
||||||
/* #139: CSP-safe replacements for inline style attributes (style-src 'self') */
|
/* #139: CSP-safe replacements for inline style attributes (style-src 'self') */
|
||||||
.hidden { display: none; }
|
.hidden { display: none; }
|
||||||
|
|||||||
@@ -39,19 +39,27 @@
|
|||||||
{{if .Attachment}}
|
{{if .Attachment}}
|
||||||
<div class="float">
|
<div class="float">
|
||||||
<div class="attachment-bar">
|
<div class="attachment-bar">
|
||||||
|
{{if .AttachmentImage}}
|
||||||
|
<div class="attachment-preview"><img src="/f/{{.Attachment.ID}}/{{.Attachment.Filename}}" alt="{{.Attachment.Filename}}"></div>
|
||||||
|
<a class="attachment-chip" href="/f/{{.Attachment.ID}}/{{.Attachment.Filename}}" data-mime="{{.Attachment.Mime}}">
|
||||||
|
<span class="attachment-name">{{.Attachment.Filename}}</span>
|
||||||
|
<span class="attachment-size">{{.Attachment.SizeHuman}}</span>
|
||||||
|
</a>
|
||||||
|
{{else}}
|
||||||
<a class="attachment-chip" href="/f/{{.Attachment.ID}}/{{.Attachment.Filename}}" data-mime="{{.Attachment.Mime}}">
|
<a class="attachment-chip" href="/f/{{.Attachment.ID}}/{{.Attachment.Filename}}" data-mime="{{.Attachment.Mime}}">
|
||||||
<span class="attachment-name">{{.Attachment.Filename}}</span>
|
<span class="attachment-name">{{.Attachment.Filename}}</span>
|
||||||
<span class="attachment-size">{{.Attachment.SizeHuman}}</span>
|
<span class="attachment-size">{{.Attachment.SizeHuman}}</span>
|
||||||
</a>
|
</a>
|
||||||
{{if or (eq .Attachment.Mime "image/png") (eq .Attachment.Mime "image/jpeg") (eq .Attachment.Mime "image/gif") (eq .Attachment.Mime "image/webp")}}
|
|
||||||
<div class="attachment-preview"><img src="/f/{{.Attachment.ID}}/{{.Attachment.Filename}}" alt="{{.Attachment.Filename}}"></div>
|
<div class="attachment-preview"><img src="/f/{{.Attachment.ID}}/{{.Attachment.Filename}}" alt="{{.Attachment.Filename}}"></div>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
{{if not .AttachmentImage}}
|
||||||
<div class="float">
|
<div class="float">
|
||||||
<div class="code" id="code"><div class="gutter" id="gutter">{{.Gutter}}</div><div class="codebody" id="codebody">{{.ContentHTML}}</div></div>
|
<div class="code" id="code"><div class="gutter" id="gutter">{{.Gutter}}</div><div class="codebody" id="codebody">{{.ContentHTML}}</div></div>
|
||||||
</div>
|
</div>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
<input type="hidden" id="raw-content" value="{{.ContentAttr}}">
|
<input type="hidden" id="raw-content" value="{{.ContentAttr}}">
|
||||||
<script src="/static/paste.js" defer data-paste-id="{{.ID}}"></script>
|
<script src="/static/paste.js" defer data-paste-id="{{.ID}}"></script>
|
||||||
|
|||||||
+18
-2
@@ -252,20 +252,35 @@ func (h *Handlers) renderPaste(w http.ResponseWriter, row *store.PasteRow, justC
|
|||||||
http.Error(w, "db error", 500)
|
http.Error(w, "db error", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// #221: image attachments render the image, not a text/code box. Size
|
||||||
|
// comes from the attachment's actual file size, not the text content.
|
||||||
|
attImage := false
|
||||||
|
if attachment != nil {
|
||||||
|
switch attachment.Mime {
|
||||||
|
case "image/png", "image/jpeg", "image/gif", "image/webp":
|
||||||
|
attImage = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sizeHuman := humanSize(len(row.Content))
|
||||||
|
lineCount := lines
|
||||||
|
if attachment != nil {
|
||||||
|
sizeHuman = attachment.SizeHuman
|
||||||
|
lineCount = 1
|
||||||
|
}
|
||||||
data := map[string]any{
|
data := map[string]any{
|
||||||
"Page": "paste",
|
"Page": "paste",
|
||||||
"ID": row.ID,
|
"ID": row.ID,
|
||||||
"Title": row.Title.String,
|
"Title": row.Title.String,
|
||||||
"Language": row.Language.String,
|
"Language": row.Language.String,
|
||||||
"StatsSummary": summary,
|
"StatsSummary": summary,
|
||||||
"SizeHuman": humanSize(len(row.Content)),
|
"SizeHuman": sizeHuman,
|
||||||
"HasPassword": row.PasswordHash.Valid,
|
"HasPassword": row.PasswordHash.Valid,
|
||||||
"BurnAfterRead": row.BurnAfterRead,
|
"BurnAfterRead": row.BurnAfterRead,
|
||||||
"CustomSlug": row.CustomSlug.String,
|
"CustomSlug": row.CustomSlug.String,
|
||||||
"ContentHTML": template.HTML(langpkg.HighlightCode(row.Content, row.Language.String)), // safe: HighlightCode escapes all non-span text
|
"ContentHTML": template.HTML(langpkg.HighlightCode(row.Content, row.Language.String)), // safe: HighlightCode escapes all non-span text
|
||||||
"ContentAttr": row.Content,
|
"ContentAttr": row.Content,
|
||||||
"Gutter": strings.TrimSuffix(gutter, "\n"),
|
"Gutter": strings.TrimSuffix(gutter, "\n"),
|
||||||
"LineCount": lines,
|
"LineCount": lineCount,
|
||||||
"SizeBytes": len(row.Content),
|
"SizeBytes": len(row.Content),
|
||||||
"CreatedAgo": agoString(row.CreatedAt),
|
"CreatedAgo": agoString(row.CreatedAt),
|
||||||
"CreatedAtUnix": row.CreatedAt,
|
"CreatedAtUnix": row.CreatedAt,
|
||||||
@@ -279,6 +294,7 @@ func (h *Handlers) renderPaste(w http.ResponseWriter, row *store.PasteRow, justC
|
|||||||
"ReadsTotal": int(row.ReadsLimit.Int64),
|
"ReadsTotal": int(row.ReadsLimit.Int64),
|
||||||
"JustCreated": justCreated,
|
"JustCreated": justCreated,
|
||||||
"Attachment": attachment,
|
"Attachment": attachment,
|
||||||
|
"AttachmentImage": attImage,
|
||||||
"Host": "this host",
|
"Host": "this host",
|
||||||
}
|
}
|
||||||
h.renderPage(w, "paste.html", data)
|
h.renderPage(w, "paste.html", data)
|
||||||
|
|||||||
Reference in New Issue
Block a user