2023-10-02 15:44:55 -04:00
|
|
|
import { Readable } from "stream";
|
|
|
|
import san from "sanitize-html";
|
2023-10-03 01:24:06 -04:00
|
|
|
import { FormStory } from "~/lib/client/types/form/story";
|
2023-10-02 15:44:55 -04:00
|
|
|
import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
|
|
|
|
import {
|
|
|
|
getBucket,
|
|
|
|
bodyHandler,
|
|
|
|
modelFormChapter,
|
|
|
|
} from "~/lib/server/storyHelpers";
|
|
|
|
import { Story } from "~/models/stories";
|
|
|
|
import { sanitizeConf } from "~/lib/server/constants";
|
|
|
|
import { countWords } from "~/lib/functions";
|
|
|
|
|
|
|
|
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));
|
|
|
|
story.chapters[story.chapters.length - 1].words = countWords(
|
|
|
|
await bodyHandler(c),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
});
|