Add reference images from previous page and story characters for consist
This commit is contained in:
+38
-5
@@ -82,13 +82,46 @@ export async function getStoryWithPagesBySlug(slug: string): Promise<{ story: St
|
||||
}
|
||||
|
||||
export async function getStoryCharacterImages(storyId: string): Promise<string[]> {
|
||||
const storyPages = await db.select({ characterImageUrls: pages.characterImageUrls })
|
||||
const storyPages = await db.select({
|
||||
characterImageUrls: pages.characterImageUrls,
|
||||
pageNumber: pages.pageNumber
|
||||
})
|
||||
.from(pages)
|
||||
.where(eq(pages.storyId, storyId));
|
||||
.where(eq(pages.storyId, storyId))
|
||||
.orderBy(pages.pageNumber);
|
||||
|
||||
// Flatten all character URLs from all pages and remove duplicates
|
||||
const allUrls = storyPages.flatMap(page => page.characterImageUrls);
|
||||
return [...new Set(allUrls)]; // Remove duplicates
|
||||
// Flatten all character URLs from all pages, keeping order by page number
|
||||
const allUrls: string[] = [];
|
||||
const seenUrls = new Set<string>();
|
||||
|
||||
for (const page of storyPages) {
|
||||
for (const url of page.characterImageUrls) {
|
||||
if (!seenUrls.has(url)) {
|
||||
seenUrls.add(url);
|
||||
allUrls.push(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allUrls;
|
||||
}
|
||||
|
||||
export async function getLastPageImage(storyId: string): Promise<string | null> {
|
||||
const allPages = await db.select({ generatedImageUrl: pages.generatedImageUrl, pageNumber: pages.pageNumber })
|
||||
.from(pages)
|
||||
.where(eq(pages.storyId, storyId))
|
||||
.orderBy(pages.pageNumber);
|
||||
|
||||
if (allPages.length === 0) return null;
|
||||
|
||||
// Find the last page that has a generated image
|
||||
for (let i = allPages.length - 1; i >= 0; i--) {
|
||||
if (allPages[i].generatedImageUrl) {
|
||||
return allPages[i].generatedImageUrl;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function getNextPageNumber(storyId: string): Promise<number> {
|
||||
|
||||
Reference in New Issue
Block a user