🎉📔 Working draft of OPDS server
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import express, { Request, Response, Router, Express } from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import path, { basename, extname } from "path";
|
||||
import path, { basename, extname, join} from "path";
|
||||
import { isUndefined } from "lodash";
|
||||
|
||||
import { buildAsync } from "calibre-opds";
|
||||
import initMain from "calibre-opds/lib/index";
|
||||
@@ -8,12 +9,29 @@ import { EnumLinkRel, EnumMIME } 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 { isUndefined } from "lodash";
|
||||
|
||||
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
|
||||
app.use(express.static("dist"));
|
||||
|
||||
export function opdsRouter() {
|
||||
const path_of_books = "/Users/rishi/work/threetwo/src/server/comics";
|
||||
console.log(path_of_books);
|
||||
@@ -27,23 +45,17 @@ export function opdsRouter() {
|
||||
[
|
||||
async (feed: Feed) => {
|
||||
feed.books = feed.books || [];
|
||||
await FastGlob(["*.cbr", "*.cbz"], {
|
||||
await FastGlob(["*.cbr", "*.cbz", "*.cb7", "*.cba", "*.cbt"], {
|
||||
cwd: path_of_books,
|
||||
}).each((file) => {
|
||||
const ext = extname(file);
|
||||
const title = basename(file, ext);
|
||||
|
||||
/**
|
||||
* make ur download url
|
||||
*/
|
||||
const href = encodeURI(
|
||||
`/Users/rishi/work/threetwo/src/server/comics/${file}`,
|
||||
`/file/${file}`,
|
||||
);
|
||||
|
||||
/**
|
||||
* mime for file
|
||||
*/
|
||||
const type = "application/octet-stream";
|
||||
const type = lookup(ext) || "application/octet-stream";;
|
||||
|
||||
const entry = Entry.deserialize<Entry>({
|
||||
title,
|
||||
@@ -71,27 +83,30 @@ export function opdsRouter() {
|
||||
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`)
|
||||
})
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
// configure app to use bodyParser for
|
||||
// Getting data from body of requests
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(opdsRouter());
|
||||
|
||||
const port: number = Number(process.env.PORT) || 8050; // set our port
|
||||
|
||||
// Send index.html on root request
|
||||
app.use(express.static("dist"));
|
||||
|
||||
app.get("/", (req: Request, res: Response) => {
|
||||
console.log("sending index.html");
|
||||
res.sendFile("/dist/index.html");
|
||||
});
|
||||
|
||||
app.use(opdsRouter());
|
||||
|
||||
// REGISTER ROUTES
|
||||
// all of the routes will be prefixed with /api
|
||||
|
||||
|
||||
Reference in New Issue
Block a user