Files
OpenFrame/lib/comment-images.ts
yusufipek b9e2006e34 feat(comments): carry a batch of screenshots on one comment
A comment held one image, and the paste handler took the first item off the
clipboard and dropped the rest. Reviewing a cut usually means several
screenshots about the same moment, which meant one comment per screenshot or
one screenshot and a paragraph describing the others. Editing a comment could
not attach anything at all: the edit box had no paste handler, no file picker
and no way to remove what was already there.

A comment now carries up to five images, in the composer, in a reply and in
the editor. One paste stages every image on the clipboard, the file picker
takes a multiple selection, and a drop lands on whichever editor is open. Over
the cap the extras are refused out loud rather than dropped quietly. A single
image still fills the width; several tile into a grid, and either opens full
screen on click.

The images move into their own table. `comments.imageUrl` stays and follows the
first of them, so a reader that has not been updated keeps working, and the
migration copies the existing attachments across so the new table is complete
from the first read. Every path that resolves a URL back to a comment now asks
the new table: R2 cleanup, the orphan sweep, the storage accounting and the
reference checks that decide whether an object can be deleted. Left on the old
column they would have treated images two through five as unreferenced and
swept them.

Detaching an image while editing only breaks the link. The file stays in R2 and
in the assets pane, which is where it is deleted from and where its bytes are
already billed.
2026-08-20 11:01:33 +03:00

54 lines
1.8 KiB
TypeScript

import { SAFE_IMAGE_PROXY_PATH } from '@/lib/image-upload-validation';
/**
* How many images a single comment (or reply) may carry. Pasting a batch of
* screenshots is the normal case, so the cap is there to bound the upload
* burst and the row width, not to make the feature scarce.
*/
export const MAX_COMMENT_IMAGES = 5;
export type CommentImageUrlsResult = { urls: string[] } | { error: string };
/**
* Normalize whatever a client sent for a comment's images into an ordered,
* de-duplicated list of upload URLs.
*
* Accepts the legacy single `imageUrl` alongside the `imageUrls` list so an
* older client keeps working. Returns a message rather than throwing, because
* every caller turns it straight into a 400.
*/
export function parseCommentImageUrls(input: {
imageUrl?: unknown;
imageUrls?: unknown;
}): CommentImageUrlsResult {
const { imageUrl, imageUrls } = input;
let raw: unknown[];
if (imageUrls !== undefined && imageUrls !== null) {
if (!Array.isArray(imageUrls)) {
return { error: 'imageUrls must be an array of uploaded image URLs' };
}
raw = imageUrls;
} else if (imageUrl !== undefined && imageUrl !== null) {
raw = [imageUrl];
} else {
raw = [];
}
const urls: string[] = [];
for (const value of raw) {
if (typeof value !== 'string' || !SAFE_IMAGE_PROXY_PATH.test(value)) {
return { error: 'Image URL must reference an uploaded image file' };
}
// The same file twice would trip the unique index on comment_images and
// charge the account twice for one object, so collapse it here instead.
if (!urls.includes(value)) urls.push(value);
}
if (urls.length > MAX_COMMENT_IMAGES) {
return { error: `A comment can have at most ${MAX_COMMENT_IMAGES} images` };
}
return { urls };
}