fix inpainting thumbnails (#870)

This commit is contained in:
Brandon Thomas authored and GitHub committed 2025-10-09 04:21:03 -04:00
1 parent d6abe9fb88
commit d6c1eaae5d
3 files changed
+47 -12

No files matched your search

@@ -14,6 +14,13 @@ import { TutorialModalButton } from "@storyteller/ui-tutorial-modal";
export interface BaseSelectorImage {
url: string;
mediaToken: string;
// Link to the thumbnail *template* URL of the image
// This is not a URL itself! It must be converted into a URL by replacing parameters.
thumbnailUrlTemplate?: string;
// Link to the full image URL of the full image asset
fullImageUrl?: string;
}
export type BaseImageSelectorProps = {
@@ -41,13 +48,13 @@ export const BaseImageSelector = ({
setSelectedGalleryImages([]);
};
const handleImageSelect = (id: string) => {
const handleImageSelect = (mediaToken: string) => {
// If already selected, deselect it
// Else if not selected and under max limit, select it
if (selectedGalleryImages.includes(id)) {
if (selectedGalleryImages.includes(mediaToken)) {
setSelectedGalleryImages([]);
} else {
setSelectedGalleryImages([id]);
setSelectedGalleryImages([mediaToken]);
}
};
@@ -56,12 +63,14 @@ export const BaseImageSelector = ({
if (selectedItems.length !== 1) {
return;
}
const item = selectedItems[0];
if (!item.fullImage) return;
if (!item.fullImage) {
return
}
const referenceImage: BaseSelectorImage = {
url: item.fullImage,
mediaToken: item.id,
thumbnailUrlTemplate: item.thumbnailUrlTemplate,
};
sendImageEvent(referenceImage);
};
@@ -92,6 +101,7 @@ export const BaseImageSelector = ({
// Attempt to resolve the CDN URL for the uploaded image token; fallback to data URL
(async () => {
let finalUrl = reader.result as string;
let thumbnailUrlTemplate = undefined;
try {
const api = new MediaFilesApi();
const result = await api.GetMediaFileByToken({
@@ -102,6 +112,12 @@ export const BaseImageSelector = ({
result.data.media_links?.cdn_url ||
result.data.public_bucket_url ||
finalUrl;
// NB(bt,2025-10-09): I think `thumbnail_template` is wrong and that
// `maybe_thumbnail_template` is the correct API field, but upstream
// seems to imply otherwise. Let's simply check both keys for now.
thumbnailUrlTemplate =
result.data.media_links?.thumbnail_template ||
(result.data.media_links as any)?.maybe_thumbnail_template;
}
} catch (e) {
console.warn(
@@ -111,8 +127,10 @@ export const BaseImageSelector = ({
}
const referenceImage: BaseSelectorImage = {
url: finalUrl,
mediaToken,
url: finalUrl,
fullImageUrl: finalUrl,
thumbnailUrlTemplate: thumbnailUrlTemplate,
};
toast.success("Image uploaded successfully!");
@@ -71,6 +71,8 @@ export const HistoryStack = ({
({
url: editedImage.cdn_url,
mediaToken: editedImage.media_token,
thumbnailUrlTemplate: editedImage.maybe_thumbnail_template,
fullImageUrl: editedImage.cdn_url,
}) as BaseSelectorImage,
),
};
@@ -111,6 +113,18 @@ export const HistoryStack = ({
prevPendingCountRef.current = current;
}, [pendingPlaceholders.length]);
const getImageThumbnailSource = (image: BaseSelectorImage) => {
if (!!image.thumbnailUrlTemplate) {
// NB: this is the preferred way to populate the history stack thumbnails
return image.thumbnailUrlTemplate.replace("{WIDTH}", "256");
}
if (image.url.startsWith("data:")) {
return image.url;
}
console.warn("Using older image source for history stack image", image);
return `${image.url}?historystack+${sessionRandBuster.current}`;
};
return (
<div className="h-auto w-20 rounded-lg">
<div className="glass rounded-lg p-1.5">
@@ -202,13 +216,10 @@ export const HistoryStack = ({
onClick={() => handleSelectWithPreload(image)}
>
<img
src={
image.url.startsWith("data:")
? image.url
: `${image.url}?historystack+${sessionRandBuster.current}`
}
src={getImageThumbnailSource(image)}
alt=""
crossOrigin="anonymous"
// NB(bt,2025-10-09): We shouldn't need CORS here.
//crossOrigin="anonymous"
className="absolute inset-0 h-full w-full object-cover"
/>
<div
@@ -47,6 +47,9 @@ export interface GalleryItem {
id: string;
label: string;
thumbnail: string | null;
// Thumbnail template is not a usable URL yet. It has some variables
// like `{WIDTH}` which must be replaced downstream.
thumbnailUrlTemplate?: string;
fullImage?: string | null;
createdAt: string;
mediaClass?: string;
@@ -266,6 +269,9 @@ export const GalleryModal = React.memo(
"{WIDTH}",
thumbnail_size.toString()
),
// TODO(bt): Thumbnail template URL may be wrong for videos and other asset types, but I'm
// trying to plumb it through for the inpainting editor, which is just images.
thumbnailUrlTemplate: item.media_links.maybe_thumbnail_template,
fullImage: item.media_links.cdn_url,
createdAt: item.created_at,
mediaClass: