fix: return original images from external image api
This commit is contained in:
parent
95d4bccceb
commit
65aac6c14d
12
app/api/external/image/generate/route.ts
vendored
12
app/api/external/image/generate/route.ts
vendored
@ -351,6 +351,8 @@ export async function POST(request: Request) {
|
|||||||
const actualModel = generationResult.usedModel;
|
const actualModel = generationResult.usedModel;
|
||||||
const hostedMarkdown = generationResult.markdown;
|
const hostedMarkdown = generationResult.markdown;
|
||||||
const images = generationResult.images;
|
const images = generationResult.images;
|
||||||
|
const originalImageUrls = images.map((image) => image.originalUrl || image.url);
|
||||||
|
const thumbnailImageUrls = images.map((image) => image.thumbnailUrl);
|
||||||
|
|
||||||
const persistResult = await saveGeneratedImages(
|
const persistResult = await saveGeneratedImages(
|
||||||
hostedMarkdown,
|
hostedMarkdown,
|
||||||
@ -384,6 +386,8 @@ export async function POST(request: Request) {
|
|||||||
model: serializeExternalImageApiModel(actualModel),
|
model: serializeExternalImageApiModel(actualModel),
|
||||||
markdown: hostedMarkdown,
|
markdown: hostedMarkdown,
|
||||||
images,
|
images,
|
||||||
|
imageUrls: originalImageUrls,
|
||||||
|
thumbnailUrls: thumbnailImageUrls,
|
||||||
attempts: generationResult.attempts,
|
attempts: generationResult.attempts,
|
||||||
},
|
},
|
||||||
generatedImageIds: persistResult.savedImageIds,
|
generatedImageIds: persistResult.savedImageIds,
|
||||||
@ -400,6 +404,14 @@ export async function POST(request: Request) {
|
|||||||
data: {
|
data: {
|
||||||
requestId: logId,
|
requestId: logId,
|
||||||
model: serializeExternalImageApiModel(actualModel),
|
model: serializeExternalImageApiModel(actualModel),
|
||||||
|
imageUrl: originalImageUrls[0] || null,
|
||||||
|
image_url: originalImageUrls[0] || null,
|
||||||
|
imageUrls: originalImageUrls,
|
||||||
|
image_urls: originalImageUrls,
|
||||||
|
originalUrls: originalImageUrls,
|
||||||
|
original_urls: originalImageUrls,
|
||||||
|
thumbnailUrls: thumbnailImageUrls,
|
||||||
|
thumbnail_urls: thumbnailImageUrls,
|
||||||
images,
|
images,
|
||||||
generatedImageIds: persistResult.savedImageIds,
|
generatedImageIds: persistResult.savedImageIds,
|
||||||
generatedAssetIds: persistResult.savedAssetIds,
|
generatedAssetIds: persistResult.savedAssetIds,
|
||||||
|
|||||||
@ -13,7 +13,11 @@ import {
|
|||||||
normalizeExternalImageApiConfig,
|
normalizeExternalImageApiConfig,
|
||||||
type ExternalImageApiAccessKey,
|
type ExternalImageApiAccessKey,
|
||||||
} from "@/lib/external-image-api/config";
|
} from "@/lib/external-image-api/config";
|
||||||
import { extractImageUrlsFromText } from "@/lib/image-url";
|
import {
|
||||||
|
extractImageUrlsFromText,
|
||||||
|
extractOriginalImageUrlFromAlt,
|
||||||
|
inferOriginalImageUrl,
|
||||||
|
} from "@/lib/image-url";
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
|
|
||||||
type ImageModelEntry = Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>[number];
|
type ImageModelEntry = Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>[number];
|
||||||
@ -53,7 +57,10 @@ type ExternalImageApiGeneratedImage = {
|
|||||||
id: number;
|
id: number;
|
||||||
url: string;
|
url: string;
|
||||||
originalUrl: string;
|
originalUrl: string;
|
||||||
|
original_url: string;
|
||||||
thumbnailUrl: string;
|
thumbnailUrl: string;
|
||||||
|
thumbnail_url: string;
|
||||||
|
previewUrl: string;
|
||||||
imageSize: string;
|
imageSize: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -485,15 +492,28 @@ function filterCompatibleCandidates(
|
|||||||
return acceptedCandidates;
|
return acceptedCandidates;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseGeneratedImageMarkdownMeta(alt: string) {
|
function parseGeneratedImageMarkdownMeta(alt: string, fallbackUrl = "") {
|
||||||
const metaMatch = alt.match(/thumb\|\|\|([^|]+)\|\|\|(.+)$/i);
|
const normalizedAlt = normalizeText(alt);
|
||||||
if (!metaMatch) {
|
const fallbackOriginalUrl = inferOriginalImageUrl(normalizeText(fallbackUrl));
|
||||||
return null;
|
const originalUrl =
|
||||||
|
extractOriginalImageUrlFromAlt(normalizedAlt, fallbackUrl) || fallbackOriginalUrl;
|
||||||
|
let imageSize = "2K";
|
||||||
|
|
||||||
|
if (normalizedAlt.includes("thumb|||")) {
|
||||||
|
const parts = normalizedAlt
|
||||||
|
.substring(normalizedAlt.indexOf("thumb|||") + 8)
|
||||||
|
.split("|||");
|
||||||
|
imageSize = normalizeText(parts[1]) || imageSize;
|
||||||
|
} else if (normalizedAlt.includes("thumb$$")) {
|
||||||
|
const parts = normalizedAlt
|
||||||
|
.substring(normalizedAlt.indexOf("thumb$$") + 7)
|
||||||
|
.split("$$");
|
||||||
|
imageSize = normalizeText(parts[1]) || imageSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
originalUrl: normalizeText(metaMatch[1]),
|
originalUrl: normalizeText(originalUrl) || fallbackOriginalUrl,
|
||||||
imageSize: normalizeText(metaMatch[2]),
|
imageSize,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -525,8 +545,9 @@ export async function ensureHostedImageMarkdown(
|
|||||||
const raw = match[0];
|
const raw = match[0];
|
||||||
const alt = match[1] || "";
|
const alt = match[1] || "";
|
||||||
const thumbnailSourceUrl = normalizeText(match[2]);
|
const thumbnailSourceUrl = normalizeText(match[2]);
|
||||||
const metadata = parseGeneratedImageMarkdownMeta(alt);
|
const metadata = parseGeneratedImageMarkdownMeta(alt, thumbnailSourceUrl);
|
||||||
const originalSourceUrl = metadata?.originalUrl || thumbnailSourceUrl;
|
const originalSourceUrl =
|
||||||
|
metadata.originalUrl || inferOriginalImageUrl(thumbnailSourceUrl);
|
||||||
const resolvedImageSize = metadata?.imageSize || imageSize;
|
const resolvedImageSize = metadata?.imageSize || imageSize;
|
||||||
const start = match.index ?? -1;
|
const start = match.index ?? -1;
|
||||||
if (start < 0) continue;
|
if (start < 0) continue;
|
||||||
@ -552,18 +573,18 @@ export function extractGeneratedImagesFromMarkdown(markdown: string) {
|
|||||||
return matches.map((match, index) => {
|
return matches.map((match, index) => {
|
||||||
const alt = match[1] || "";
|
const alt = match[1] || "";
|
||||||
const thumbnailUrl = match[2] || "";
|
const thumbnailUrl = match[2] || "";
|
||||||
let originalUrl = thumbnailUrl;
|
const metadata = parseGeneratedImageMarkdownMeta(alt, thumbnailUrl);
|
||||||
let imageSize = "2K";
|
const originalUrl =
|
||||||
const metadata = parseGeneratedImageMarkdownMeta(alt);
|
metadata.originalUrl || inferOriginalImageUrl(thumbnailUrl) || thumbnailUrl;
|
||||||
if (metadata) {
|
const imageSize = metadata.imageSize || "2K";
|
||||||
originalUrl = metadata.originalUrl || thumbnailUrl;
|
|
||||||
imageSize = metadata.imageSize || imageSize;
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
id: index + 1,
|
id: index + 1,
|
||||||
url: originalUrl,
|
url: originalUrl,
|
||||||
originalUrl,
|
originalUrl,
|
||||||
|
original_url: originalUrl,
|
||||||
thumbnailUrl,
|
thumbnailUrl,
|
||||||
|
thumbnail_url: thumbnailUrl,
|
||||||
|
previewUrl: thumbnailUrl,
|
||||||
imageSize,
|
imageSize,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user