2023-10-03 00:46:57 -04:00
|
|
|
import { H3Event, EventHandlerRequest } from "h3";
|
|
|
|
import { GridFSBucket } from "mongodb";
|
|
|
|
import mongoose, { Document } from "mongoose";
|
2023-12-11 21:13:22 -05:00
|
|
|
import { norm, stringifyStream } from "~/lib/functions";
|
2023-10-03 00:46:57 -04:00
|
|
|
import { IStory } from "~/models/stories";
|
|
|
|
import { IChapter } from "~/models/stories/chapter";
|
|
|
|
import getBucket from "../storyHelpers/getBucket";
|
2023-12-01 17:57:17 -05:00
|
|
|
import { SingleChapterResult } from "~/lib/client/types/slightlyDifferentStory";
|
2023-10-03 00:46:57 -04:00
|
|
|
|
|
|
|
export default async function (
|
|
|
|
story: Document<number, {}, IStory> & IStory,
|
|
|
|
event: H3Event<EventHandlerRequest>,
|
2023-12-01 17:57:17 -05:00
|
|
|
cindex: number,
|
|
|
|
): Promise<SingleChapterResult> {
|
2023-10-03 00:46:57 -04:00
|
|
|
const finObj: any = story.toObject();
|
|
|
|
const cloned: any & { chapters: IChapter[] } = { ...finObj };
|
|
|
|
delete finObj.chapters;
|
|
|
|
const bucket = getBucket();
|
|
|
|
let ds = bucket.openDownloadStreamByName(
|
2023-10-11 16:37:18 -04:00
|
|
|
`/stories/${
|
|
|
|
cloned.chapters[cindex || event.context.chapterIndex || 0].id
|
|
|
|
}.txt`,
|
2023-10-03 00:46:57 -04:00
|
|
|
);
|
|
|
|
let stream = await stringifyStream(ds);
|
|
|
|
finObj.currentChapter = {
|
2023-10-11 16:37:18 -04:00
|
|
|
...cloned.chapters[cindex || event.context.chapterIndex || 0],
|
2023-12-11 21:13:22 -05:00
|
|
|
text: norm(stream),
|
2023-10-03 00:46:57 -04:00
|
|
|
};
|
|
|
|
finObj.totalChapters = story.chapters.length;
|
2023-12-01 17:57:17 -05:00
|
|
|
finObj.chapterNames = story.chapters.map((a) => a.title);
|
2023-10-03 00:46:57 -04:00
|
|
|
return finObj;
|
|
|
|
}
|