2023-10-02 15:44:55 -04:00
|
|
|
import { Readable } from "stream";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { FormStory } from "@client/types/form/story";
|
2023-12-29 16:32:32 -05:00
|
|
|
import { isLoggedIn } from "@server/middlewareButNotReally";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { getBucket, bodyHandler, modelFormChapter } from "@server/storyHelpers";
|
|
|
|
import { Story } from "@models/stories";
|
|
|
|
import { countWords } from "@functions";
|
2023-10-02 15:44:55 -04:00
|
|
|
|
|
|
|
export default eventHandler(async (ev) => {
|
|
|
|
isLoggedIn(ev);
|
|
|
|
const bucket = getBucket();
|
|
|
|
const body = await readBody<FormStory>(ev);
|
|
|
|
const story = new Story({
|
|
|
|
title: body.title,
|
|
|
|
author: ev.context.currentUser!._id,
|
2023-10-11 15:31:02 -04:00
|
|
|
coAuthor: body.coAuthor,
|
2023-10-02 15:44:55 -04:00
|
|
|
views: 0,
|
|
|
|
reviews: 0,
|
|
|
|
downloads: 0,
|
|
|
|
ficmas: body.ficmas || null,
|
|
|
|
challenge: body.challenge || null,
|
|
|
|
completed: body.completed,
|
|
|
|
});
|
|
|
|
for (const c of body.chapters) {
|
|
|
|
story.chapters.push(modelFormChapter(c));
|
2023-12-29 20:11:07 -05:00
|
|
|
story.chapters[story.chapters.length - 1].words = countWords(await bodyHandler(c));
|
2023-10-02 15:44:55 -04:00
|
|
|
}
|
|
|
|
await story.save();
|
|
|
|
|
|
|
|
for (let i = 0; i < story.chapters.length; i++) {
|
|
|
|
let c = story.chapters[i];
|
|
|
|
const content = await bodyHandler(body.chapters[i]);
|
|
|
|
const readable = new Readable();
|
|
|
|
readable.push(content);
|
|
|
|
readable.push(null);
|
|
|
|
readable.pipe(bucket.openUploadStream(`/stories/${c.id}.txt`));
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
url: `/story/${story._id}/1`,
|
|
|
|
story,
|
|
|
|
};
|
|
|
|
});
|