🧹 Cleaning up

This commit is contained in:
2021-07-19 13:21:15 -07:00
parent 885c68531c
commit 42de05b36e
3 changed files with 103 additions and 33 deletions

View File

@@ -1,32 +1,26 @@
import express, { Request, Response, Router, Express } from "express";
import bodyParser from "body-parser";
import path, { basename, extname, join} from "path";
import { basename, extname, join } from "path";
import { lookup } from "mime-types";
import { promises as fs } from "fs";
import { responseStream } from "http-response-stream";
import { isUndefined } from "lodash";
import { buildAsync } from "calibre-opds";
import initMain from "calibre-opds/lib/index";
import { EnumLinkRel, EnumMIME } from "opds-extra/lib/const";
import { EnumLinkRel } from "opds-extra/lib/const";
import { async as FastGlob } from "@bluelovers/fast-glob/bluebird";
import { Entry, Feed } from "opds-extra/lib/v1";
import { Link } from "opds-extra/lib/v1/core";
import { lookup } from 'mime-types';
import { promises as fs } from 'fs';
import { responseStream } from 'http-response-stream';
// call express
const app: Express = express(); // define our app using express
const router = Router();
// configure app to use bodyParser for
// Getting data from body of requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const port: number = Number(process.env.PORT) || 8050; // set our port
// Send index.html on root request
@@ -34,7 +28,6 @@ app.use(express.static("dist"));
export function opdsRouter() {
const path_of_books = "/Users/rishi/work/threetwo/src/server/comics";
console.log(path_of_books);
router.use("/opds", async (req, res, next) => {
return buildAsync(
initMain({
@@ -51,11 +44,9 @@ export function opdsRouter() {
const ext = extname(file);
const title = basename(file, ext);
const href = encodeURI(
`/file/${file}`,
);
const href = encodeURI(`/file/${file}`);
const type = lookup(ext) || "application/octet-stream";;
const type = lookup(ext) || "application/octet-stream";
const entry = Entry.deserialize<Entry>({
title,
@@ -78,25 +69,24 @@ export function opdsRouter() {
},
],
).then((feed) => {
console.log(feed);
res.setHeader("Content-Type", "application/xml");
return res.end(feed.toXML());
});
});
router.use("/file/*", async (req, res) => {
const file: string = req.params[0];
const ext = extname(file);
if ([".cbr", ".cbz", ".cb7", ".cba", ".cbt"].includes(ext)) {
const content = await fs.readFile(join(path_of_books, file));
const mime = lookup(ext) || "application/octet-stream";
res.set('Content-Type', mime);
return responseStream(res, content)
}
res.status(404).end(`'${file}' not exists`)
})
router.use("/file/*", async (req, res) => {
const file: string = req.params[0];
const ext = extname(file);
if ([".cbr", ".cbz", ".cb7", ".cba", ".cbt"].includes(ext)) {
const content = await fs.readFile(join(path_of_books, file));
const mime = lookup(ext) || "application/octet-stream";
res.set("Content-Type", mime);
return responseStream(res, content);
}
res.status(404).end(`'${file}' not exists`);
});
return router;
}