2023-10-03 00:37:46 -04:00
|
|
|
import getBucket from "./getBucket";
|
|
|
|
import { Readable } from "stream";
|
2023-12-29 20:53:29 -05:00
|
|
|
export default async function replaceGridFS(chapterID: number | undefined, content: string) {
|
2023-10-03 00:37:46 -04:00
|
|
|
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));
|
|
|
|
}
|