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
@ -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<string> {
|
||||
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<string> {
|
||||
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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -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",
|
||||
});
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user