refactor(server/utils): create reusable utilities for various story-related functions

`bodyHandler` takes a request body and returns the chapter content as a string
`formChapterTransform` serializes a chapter-like object supplied in a request body to an actual
	proper chapter object
`getBucket` retrieves the story content gridfs bucket
`replaceGridFS` either creates or updates (deletes + reuploads) a gridfs file
This commit is contained in:
parent 1338e87238
commit c8bdcc0ec3
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
4 changed files with 88 additions and 86 deletions

@ -4,7 +4,7 @@ import { marked } from "marked";
import * as mammoth from "mammoth"; import * as mammoth from "mammoth";
import * as san from "sanitize-html"; import * as san from "sanitize-html";
import { sanitizeConf } from "../constants"; import { sanitizeConf } from "../constants";
import { FormChapter } from "~/lib/client/types/FormStory"; import { FormChapter } from "~/lib/client/types/form/story";
export default async function (bodyObj: FormChapter): Promise<string> { export default async function (bodyObj: FormChapter): Promise<string> {
let str: string = ""; let str: string = "";

@ -1,12 +1,11 @@
import san from "sanitize-html"; import san from "sanitize-html";
import { FormChapter } from "~/lib/client/types/FormStory"; import { FormChapter } from "~/lib/client/types/form/story";
import { countWords } from "~/lib/functions"; import { countWords } from "~/lib/functions";
import { IChapter } from "~/models/stories/chapter"; import { IChapter } from "~/models/stories/chapter";
import { sanitizeConf } from "../constants"; import { sanitizeConf } from "../constants";
import bodyHandler from "./bodyHandler"; import bodyHandler from "./bodyHandler";
export default function (c: FormChapter): IChapter {
export default function(c: FormChapter): IChapter {
let t: IChapter = { let t: IChapter = {
title: c.chapterTitle, title: c.chapterTitle,
summary: san(c.summary, sanitizeConf), summary: san(c.summary, sanitizeConf),
@ -17,7 +16,7 @@ export default function(c: FormChapter): IChapter {
nsfw: c.nsfw, nsfw: c.nsfw,
genre: c.genre, genre: c.genre,
loggedInOnly: c.loggedInOnly, loggedInOnly: c.loggedInOnly,
hidden: c.hidden hidden: c.hidden,
} };
return t; return t;
} }

@ -1,11 +1,14 @@
import getBucket from "./getBucket"; import getBucket from "./getBucket";
import {Readable} from "stream" import { Readable } from "stream";
export default async function replaceGridFS(chapterID: number | undefined, content: string) { export default async function replaceGridFS(
chapterID: number | undefined,
content: string,
) {
let filename = `/stories/${chapterID}.txt`; let filename = `/stories/${chapterID}.txt`;
const bucket = getBucket() const bucket = getBucket();
if(chapterID) { if (chapterID) {
const curs = bucket.find({filename}).limit(1) const curs = bucket.find({ filename }).limit(1);
for await(const d of curs) { for await (const d of curs) {
await bucket.delete(d._id); await bucket.delete(d._id);
} }
} }