-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathserver.ts
More file actions
205 lines (176 loc) · 6.24 KB
/
server.ts
File metadata and controls
205 lines (176 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { Server } from "r2-streamer-js";
import * as express from "express";
import * as path from "path";
import recursive from "recursive-readdir";
interface PublicationEntry {
title: string;
filename: string;
type: "epub" | "pdf";
hosted?: boolean;
viewers: { title: string; url: string }[];
}
async function start() {
const publications: PublicationEntry[] = [
// Built-in demo publications (hosted remotely)
{
title: "Alice's Adventures in Wonderland",
filename: "alice (hosted)",
type: "pdf",
hosted: true,
viewers: [
{
title: "PDF Viewer",
url: `/viewer/index_pdf.html?url=https://alicepdf.dita.digital/alice.json`,
},
],
},
{
title: "Alice's Adventures in Wonderland",
filename: "alice (hosted)",
type: "epub",
hosted: true,
viewers: [
{
title: "DITA Reader",
url: `/viewer/index_dita.html?url=https://alice.dita.digital/manifest.json`,
},
],
},
];
const server = new Server({
disableDecryption: true,
disableOPDS: true,
disableReaders: true,
disableRemotePubUrl: true,
maxPrefetchLinks: 5,
});
// ── Serve viewer files ──────────────────────────────────────────────
server.expressUse(
"/viewer",
//@ts-ignore
express.static(path.join(__dirname, "../viewer"), { fallthrough: true })
);
//@ts-ignore
server.expressUse("/viewer", express.static(path.join(__dirname, "../dist")));
// ── Landing page ────────────────────────────────────────────────────
// ── PDF serving ─────────────────────────────────────────────────────
// Serve PDFs as static files
const pdfsPath = path.join(__dirname, "./epubs"); // PDFs live alongside EPUBs
//@ts-ignore
server.expressUse("/pdfs", express.static(pdfsPath));
// Generate a simple RWPM manifest for a PDF file
// Use path-based URL (/pdf-manifest/filename.pdf) to avoid query string conflicts
server.expressUse("/pdf-manifest", (req: any, res: any, next: any) => {
// Extract filename from path: /pdf-manifest/daisy.pdf → daisy.pdf
const file = decodeURIComponent(req.path.replace(/^\//, ""));
if (!file) {
next();
return;
}
const filename = path.basename(file);
const title = filename.replace(/\.pdf$/i, "").replace(/[_-]/g, " ");
const pdfHref = `/pdfs/${file}`;
const manifestHref = `/pdf-manifest/${encodeURIComponent(file)}`;
const manifest = {
"@context": "https://readium.org/webpub-manifest/context.jsonld",
metadata: {
"@type": "https://schema.org/Book",
conformsTo: "https://readium.org/webpub-manifest/profiles/pdf",
title: title,
identifier: `urn:pdf:${filename}`,
},
links: [
{ rel: "self", href: manifestHref, type: "application/webpub+json" },
{ rel: "alternate", href: pdfHref, type: "application/pdf" },
],
readingOrder: [
{
href: pdfHref,
type: "application/pdf",
title: title,
},
],
resources: [{ href: pdfHref, type: "application/pdf" }],
};
res.json(manifest);
});
// ── Publications API ────────────────────────────────────────────────
server.expressUse("/api/publications", (req: any, res: any) => {
res.json(publications);
});
// ── Scan local files ────────────────────────────────────────────────
const epubsPath = path.join(__dirname, "./epubs");
// Scan EPUBs
recursive(epubsPath, ["!*.epub"], function (err, files) {
if (err) {
console.error("Error scanning EPUBs:", err);
return;
}
const filePaths = files.map((fileName) => path.join(fileName));
const publicationURLs = server.addPublications(filePaths);
console.log(`📚 Found ${publicationURLs.length} EPUB(s)`);
// Add EPUBs to our publications list
filePaths.forEach((filePath, i) => {
const filename = path.basename(filePath);
const title = filename.replace(/\.epub$/i, "").replace(/[_-]/g, " ");
const manifestUrl = publicationURLs[i];
publications.push({
title,
filename,
type: "epub",
viewers: [
{
title: "DITA Reader",
url: `/viewer/index_dita.html?url=${manifestUrl}`,
},
{
title: "Minimal",
url: `/viewer/index_minimal.html?url=${manifestUrl}`,
},
{
title: "API Test",
url: `/viewer/index_api.html?url=${manifestUrl}`,
},
{
title: "Sample Read",
url: `/viewer/index_sampleread.html?url=${manifestUrl}`,
},
],
});
});
});
// Scan PDFs
recursive(epubsPath, ["!*.pdf"], function (err, files) {
if (err) {
console.error("Error scanning PDFs:", err);
return;
}
console.log(`📄 Found ${files.length} PDF(s)`);
files.forEach((filePath) => {
const relativePath = path
.relative(epubsPath, filePath)
.replace(/\\/g, "/");
const filename = path.basename(filePath);
const title = filename.replace(/\.pdf$/i, "").replace(/[_-]/g, " ");
publications.push({
title,
filename,
type: "pdf",
viewers: [
{
title: "PDF Viewer",
url: `/viewer/index_pdf.html?url=/pdf-manifest/${encodeURIComponent(relativePath)}`,
},
],
});
});
});
// ── Start server ────────────────────────────────────────────────────
const data = await server.start(4444, false);
console.log(
`\n🚀 R2D2BC Library: http://localhost:${data.urlPort}/viewer/index.html\n`
);
}
(async () => {
await start();
})();