From c8bdcc0ec3677caa80ae10992d907a1ff4d53e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Tue, 3 Oct 2023 00:37:46 -0400 Subject: [PATCH] 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 --- lib/server/storyHelpers/bodyHandler.ts | 76 +++++++++---------- .../storyHelpers/formChapterTransform.ts | 45 ++++++----- lib/server/storyHelpers/getBucket.ts | 18 ++--- lib/server/storyHelpers/replaceGridFS.ts | 35 +++++---- 4 files changed, 88 insertions(+), 86 deletions(-) diff --git a/lib/server/storyHelpers/bodyHandler.ts b/lib/server/storyHelpers/bodyHandler.ts index 3cce1bb..0d22a7e 100644 --- a/lib/server/storyHelpers/bodyHandler.ts +++ b/lib/server/storyHelpers/bodyHandler.ts @@ -1,38 +1,38 @@ -import { extname, resolve } from "path"; -import { readFileSync } from "fs"; -import { marked } from "marked"; -import * as mammoth from "mammoth"; -import * as san from "sanitize-html"; -import { sanitizeConf } from "../constants"; -import { FormChapter } from "~/lib/client/types/FormStory"; - -export default async function (bodyObj: FormChapter): Promise { - let str: string = ""; - if (bodyObj.content) { - str = bodyObj.content; - } else if (bodyObj.file) { - let ext = extname(bodyObj.file).toLowerCase(); - if (ext === "md" || ext === "markdown") - str = marked.parse( - readFileSync(resolve(`tmp/${bodyObj.file}`)).toString(), - ); - else if (ext === "doc" || ext === "docx") - str = ( - await mammoth.convertToHtml( - { path: resolve(`tmp/${bodyObj.file}`) }, - { styleMap: ["b => b", "i => i", "u => u"] }, - ) - ).value; - else - throw createError({ - statusCode: 400, - message: "bad file type", - }); - } else { - throw createError({ - statusCode: 400, - message: "no content", - }); - } - return san(str, sanitizeConf); -} +import { extname, resolve } from "path"; +import { readFileSync } from "fs"; +import { marked } from "marked"; +import * as mammoth from "mammoth"; +import * as san from "sanitize-html"; +import { sanitizeConf } from "../constants"; +import { FormChapter } from "~/lib/client/types/form/story"; + +export default async function (bodyObj: FormChapter): Promise { + let str: string = ""; + if (bodyObj.content) { + str = bodyObj.content; + } else if (bodyObj.file) { + let ext = extname(bodyObj.file).toLowerCase(); + if (ext === "md" || ext === "markdown") + str = marked.parse( + readFileSync(resolve(`tmp/${bodyObj.file}`)).toString(), + ); + else if (ext === "doc" || ext === "docx") + str = ( + await mammoth.convertToHtml( + { path: resolve(`tmp/${bodyObj.file}`) }, + { styleMap: ["b => b", "i => i", "u => u"] }, + ) + ).value; + else + throw createError({ + statusCode: 400, + message: "bad file type", + }); + } else { + throw createError({ + statusCode: 400, + message: "no content", + }); + } + return san(str, sanitizeConf); +} diff --git a/lib/server/storyHelpers/formChapterTransform.ts b/lib/server/storyHelpers/formChapterTransform.ts index 1f2bf1e..3f6dbb1 100644 --- a/lib/server/storyHelpers/formChapterTransform.ts +++ b/lib/server/storyHelpers/formChapterTransform.ts @@ -1,23 +1,22 @@ -import san from "sanitize-html"; -import { FormChapter } from "~/lib/client/types/FormStory"; -import { countWords } from "~/lib/functions"; -import { IChapter } from "~/models/stories/chapter"; -import { sanitizeConf } from "../constants"; -import bodyHandler from "./bodyHandler"; - - -export default function(c: FormChapter): IChapter { - let t: IChapter = { - title: c.chapterTitle, - summary: san(c.summary, sanitizeConf), - notes: san(c.notes, sanitizeConf), - bands: c.bands, - characters: c.characters, - relationships: c.relationships, - nsfw: c.nsfw, - genre: c.genre, - loggedInOnly: c.loggedInOnly, - hidden: c.hidden - } - return t; -} \ No newline at end of file +import san from "sanitize-html"; +import { FormChapter } from "~/lib/client/types/form/story"; +import { countWords } from "~/lib/functions"; +import { IChapter } from "~/models/stories/chapter"; +import { sanitizeConf } from "../constants"; +import bodyHandler from "./bodyHandler"; + +export default function (c: FormChapter): IChapter { + let t: IChapter = { + title: c.chapterTitle, + summary: san(c.summary, sanitizeConf), + notes: san(c.notes, sanitizeConf), + bands: c.bands, + characters: c.characters, + relationships: c.relationships, + nsfw: c.nsfw, + genre: c.genre, + loggedInOnly: c.loggedInOnly, + hidden: c.hidden, + }; + return t; +} diff --git a/lib/server/storyHelpers/getBucket.ts b/lib/server/storyHelpers/getBucket.ts index e84abf9..1f45244 100644 --- a/lib/server/storyHelpers/getBucket.ts +++ b/lib/server/storyHelpers/getBucket.ts @@ -1,9 +1,9 @@ -import { GridFSBucket } from "mongodb"; -import mongoose from "mongoose"; - -export default function () { - // @ts-ignore SHUT UP MEG - return new GridFSBucket(mongoose.connection.db, { - bucketName: "story_text", - }); -} +import { GridFSBucket } from "mongodb"; +import mongoose from "mongoose"; + +export default function () { + // @ts-ignore SHUT UP MEG + return new GridFSBucket(mongoose.connection.db, { + bucketName: "story_text", + }); +} diff --git a/lib/server/storyHelpers/replaceGridFS.ts b/lib/server/storyHelpers/replaceGridFS.ts index 95dc481..9fd179f 100644 --- a/lib/server/storyHelpers/replaceGridFS.ts +++ b/lib/server/storyHelpers/replaceGridFS.ts @@ -1,16 +1,19 @@ -import getBucket from "./getBucket"; -import {Readable} from "stream" -export default async function replaceGridFS(chapterID: number | undefined, content: string) { - let filename = `/stories/${chapterID}.txt`; - const bucket = getBucket() - if(chapterID) { - const curs = bucket.find({filename}).limit(1) - for await(const d of curs) { - await bucket.delete(d._id); - } - } - const readable = new Readable(); - readable.push(content); - readable.push(null); - readable.pipe(bucket.openUploadStream(filename)); -} \ No newline at end of file +import getBucket from "./getBucket"; +import { Readable } from "stream"; +export default async function replaceGridFS( + chapterID: number | undefined, + content: string, +) { + let filename = `/stories/${chapterID}.txt`; + const bucket = getBucket(); + if (chapterID) { + const curs = bucket.find({ filename }).limit(1); + for await (const d of curs) { + await bucket.delete(d._id); + } + } + const readable = new Readable(); + readable.push(content); + readable.push(null); + readable.pipe(bucket.openUploadStream(filename)); +}