v4: every image the API serves comes scaled to the size it is shown at — a first visit to the feed loaded 4.8 MB of images out of 5.1 MB in all: an avatar shown at 32 pixels was the upload itself, a 1.4 MB GIF among them, and so were a cover, a custom thumbnail, an attachment and a proxied image, only an image dump having a preview, 960 pixels wide. Every route that answers an image now takes ?w= and sends a WebP copy at most that wide, of the widths shared/image.ts lists, 128, 320, 640 and 960, a width between two taking the one above, and never wider than the image; without it the upload goes out as before, a dump's thumbnail alone being scaled to the widest by default, the custom ones included. serveImage (lib/upload.ts) is where they all end, and the image proxy scales what it fetched the same way, keeping each width in its cache. lib/ffmpeg.ts makes the copies as it made the previews, which they replace: on first request, under uploads/scaled/ at the upload's own path, each given its upload's date and made again when the two differ, so a new avatar or cover needs nobody to delete the old ones. WebP keeps a PNG's transparency and a GIF's animation. A JPEG is turned by its EXIF orientation here, ffmpeg's own rotation of images depending on its version. An animated WebP, which ffmpeg can't read, and an image it fails on go out as they are, a failure being remembered until the upload changes. ?f=compat asks for the copy as a JPEG, or a PNG for what may be transparent: the digest's emails take their thumbnails that way, at 640 pixels, not every mail client reading WebP. On the client, imageProps (utils/shared/urls.ts) gives an image its srcset of those copies and the width it is laid out at, which config/imageSizes.ts states for each place from App.css: Avatar, Thumbnail, which scales an API image and leaves another site's as that site sends it, and the playlists' covers use it. The images of notes and comments still load as uploaded. The copies go with the dump, the custom thumbnail or the attachment they were made from, and generatedMediaSweepJob, a minute after the start then daily, takes those whose upload is gone, the previews under their former name and the temporary files a generation left unfinished.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -35,7 +35,12 @@ describe("FilePreview", () => {
|
||||
const d = file("image/png");
|
||||
renderWithContexts(<FilePreview dump={d} compact />, { session });
|
||||
const img = screen.getByRole("img", { name: "thing" });
|
||||
expect(img).toHaveAttribute("src", dumpThumbnailUrl(d, session.mediaToken));
|
||||
// The copy scaled to a card's preview, on a dense screen.
|
||||
expect(img).toHaveAttribute(
|
||||
"src",
|
||||
`${dumpThumbnailUrl(d, session.mediaToken)}&w=320`,
|
||||
);
|
||||
expect(img).toHaveAttribute("sizes", "128px");
|
||||
expect(img).toHaveAttribute("loading", "lazy");
|
||||
});
|
||||
|
||||
@@ -101,7 +106,7 @@ describe("FilePreview", () => {
|
||||
);
|
||||
expect(container.querySelector("button img")).toHaveAttribute(
|
||||
"src",
|
||||
dumpThumbnailUrl(d),
|
||||
`${dumpThumbnailUrl(d)}&w=320`,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -123,8 +123,9 @@ describe("JournalCard", () => {
|
||||
<JournalCard dump={dump} shape="tall" {...base} />
|
||||
</ul>,
|
||||
);
|
||||
expect(container.querySelector("img")!.getAttribute("src")).toBe(
|
||||
dumpThumbnailUrl(dump),
|
||||
);
|
||||
const img = container.querySelector("img")!;
|
||||
expect(img.getAttribute("src")).toBe(`${dumpThumbnailUrl(dump)}&w=960`);
|
||||
// A tile of one column: the browser takes a narrower copy for it.
|
||||
expect(img.getAttribute("sizes")).toContain("290px");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -75,7 +75,7 @@ describe("RichContentCard", () => {
|
||||
);
|
||||
expect(screen.getByRole("img", { name: "A video" })).toHaveAttribute(
|
||||
"src",
|
||||
"http://localhost:8000/api/dumps/d/thumbnail",
|
||||
"http://localhost:8000/api/dumps/d/thumbnail?w=960",
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -5,14 +5,17 @@ import { AvatarOverrideContext } from "../../../contexts/ws/AvatarOverrideContex
|
||||
import { renderWithContexts } from "../../render.tsx";
|
||||
|
||||
describe("Avatar", () => {
|
||||
test("an image when the user has one, versioned", () => {
|
||||
test("an image when the user has one, versioned, at the size shown", () => {
|
||||
renderWithContexts(
|
||||
<Avatar userId="u1" username="ann" hasAvatar version={7} />,
|
||||
<Avatar userId="u1" username="ann" hasAvatar version={7} size={32} />,
|
||||
);
|
||||
expect(screen.getByRole("img", { name: "ann" })).toHaveAttribute(
|
||||
const img = screen.getByRole("img", { name: "ann" });
|
||||
expect(img).toHaveAttribute(
|
||||
"src",
|
||||
"http://localhost:8000/api/users/u1/avatar?v=7",
|
||||
"http://localhost:8000/api/users/u1/avatar?v=7&w=128",
|
||||
);
|
||||
expect(img).toHaveAttribute("sizes", "32px");
|
||||
expect(img.getAttribute("srcset")).toContain("avatar?v=7&w=320 320w");
|
||||
});
|
||||
|
||||
test("initials otherwise, and after the image fails", () => {
|
||||
|
||||
@@ -11,17 +11,20 @@ describe("Thumbnail", () => {
|
||||
<Thumbnail
|
||||
src="https://img.test/a.png"
|
||||
alt="art"
|
||||
sizes={128}
|
||||
placeholder={placeholder}
|
||||
/>,
|
||||
);
|
||||
const img = screen.getByRole("img", { name: "art" });
|
||||
expect(img).toHaveAttribute("src", "https://img.test/a.png");
|
||||
expect(img).toHaveAttribute("referrerpolicy", "no-referrer");
|
||||
// Another site's image comes as that site sends it.
|
||||
expect(img).not.toHaveAttribute("srcset");
|
||||
fireEvent.error(img);
|
||||
expect(screen.getByRole("img", { name: "art" }).getAttribute("src"))
|
||||
.toContain(
|
||||
"/api/remote/image?url=",
|
||||
);
|
||||
const proxied = screen.getByRole("img", { name: "art" });
|
||||
expect(proxied.getAttribute("src")).toContain("/api/remote/image?url=");
|
||||
// Through the proxy, it is scaled as the site's own are.
|
||||
expect(proxied.getAttribute("srcset")).toContain("a.png&w=128 128w");
|
||||
fireEvent.error(screen.getByRole("img", { name: "art" }));
|
||||
expect(screen.queryByRole("img", { name: "art" })).not.toBeInTheDocument();
|
||||
expect(screen.getByText("S")).toBeInTheDocument();
|
||||
@@ -32,16 +35,24 @@ describe("Thumbnail", () => {
|
||||
<Thumbnail
|
||||
src="http://localhost:8000/api/dumps/d/thumbnail"
|
||||
alt="t"
|
||||
sizes="(max-width: 512px) 100vw, 180px"
|
||||
placeholder={placeholder}
|
||||
/>,
|
||||
);
|
||||
fireEvent.error(screen.getByRole("img", { name: "t" }));
|
||||
const img = screen.getByRole("img", { name: "t" });
|
||||
expect(img).toHaveAttribute(
|
||||
"src",
|
||||
"http://localhost:8000/api/dumps/d/thumbnail?w=960",
|
||||
);
|
||||
expect(img.getAttribute("srcset")).toContain("thumbnail?w=320 320w");
|
||||
expect(img).toHaveAttribute("sizes", "(max-width: 512px) 100vw, 180px");
|
||||
fireEvent.error(img);
|
||||
expect(screen.queryByRole("img", { name: "t" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("no src: straight to the placeholder; a new src tries again", () => {
|
||||
const { rerender } = renderWithContexts(
|
||||
<Thumbnail placeholder={placeholder} alt="t" />,
|
||||
<Thumbnail placeholder={placeholder} alt="t" sizes={128} />,
|
||||
);
|
||||
expect(screen.queryByRole("img", { name: "t" })).not.toBeInTheDocument();
|
||||
rerender(
|
||||
@@ -49,6 +60,7 @@ describe("Thumbnail", () => {
|
||||
src="https://img.test/b.png"
|
||||
placeholder={placeholder}
|
||||
alt="t"
|
||||
sizes={128}
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByRole("img", { name: "t" })).toBeInTheDocument();
|
||||
|
||||
@@ -126,7 +126,7 @@ describe("PlaylistDetail", () => {
|
||||
await screen.findByRole("heading", { name: "Mix" });
|
||||
expect(document.querySelector(".playlist-detail-img")).toHaveAttribute(
|
||||
"src",
|
||||
"http://localhost:8000/api/playlists/p1/cover?v=5000",
|
||||
"http://localhost:8000/api/playlists/p1/cover?v=5000&w=960",
|
||||
);
|
||||
});
|
||||
|
||||
@@ -324,7 +324,7 @@ describe("PlaylistDetail", () => {
|
||||
await waitFor(() =>
|
||||
expect(document.querySelector(".playlist-detail-img")).toHaveAttribute(
|
||||
"src",
|
||||
"http://localhost:8000/api/playlists/p1/cover?v=9000",
|
||||
"http://localhost:8000/api/playlists/p1/cover?v=9000&w=960",
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -523,7 +523,7 @@ describe("UserPublicProfile", () => {
|
||||
expect(document.querySelector(".profile-avatar-wrapper img"))
|
||||
.toHaveAttribute(
|
||||
"src",
|
||||
"http://localhost:8000/api/users/ann/avatar?v=7000",
|
||||
"http://localhost:8000/api/users/ann/avatar?v=7000&w=320",
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -3,6 +3,8 @@ import {
|
||||
dumpFileUrl,
|
||||
dumpThumbnailUrl,
|
||||
dumpUrl,
|
||||
imageProps,
|
||||
isApiImageUrl,
|
||||
isAt,
|
||||
normalizeUrl,
|
||||
playlistImageUrl,
|
||||
@@ -65,4 +67,27 @@ describe("urls", () => {
|
||||
expect(playlistImageUrl({ id: "p", isPublic: false }, null))
|
||||
.toBe(`${API_URL}/api/playlists/p/cover`);
|
||||
});
|
||||
test("imageProps asks for the copies the server makes, by the width shown", () => {
|
||||
const avatar = `${API_URL}/api/users/u1/avatar`;
|
||||
expect(imageProps(avatar, 32)).toEqual({
|
||||
src: `${avatar}?w=128`,
|
||||
srcSet: [128, 320, 640, 960].map((w) => `${avatar}?w=${w} ${w}w`)
|
||||
.join(", "),
|
||||
sizes: "32px",
|
||||
});
|
||||
// Twice the width shown, for a dense screen that reads only `src`.
|
||||
expect(imageProps(avatar, 128).src).toBe(`${avatar}?w=320`);
|
||||
// A URL with a query keeps it, and a width that follows the viewport's
|
||||
// takes the widest copy as its `src`.
|
||||
const cover = `${API_URL}/api/playlists/p1/cover?v=7`;
|
||||
const fluid = imageProps(cover, "(max-width: 512px) 50vw, 250px");
|
||||
expect(fluid.src).toBe(`${cover}&w=960`);
|
||||
expect(fluid.srcSet).toContain(`${cover}&w=640 640w`);
|
||||
expect(fluid.sizes).toBe("(max-width: 512px) 50vw, 250px");
|
||||
});
|
||||
test("isApiImageUrl tells the API's images from another site's", () => {
|
||||
expect(isApiImageUrl(`${API_URL}/api/dumps/d1/thumbnail`)).toBe(true);
|
||||
expect(isApiImageUrl(`${API_URL}/api/remote/image?url=x`)).toBe(true);
|
||||
expect(isApiImageUrl("https://img.test/a.png")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user