error page

This commit is contained in:
tezlm 2022-12-12 06:30:44 -08:00
parent 000000860b
commit 0000009749
2 changed files with 10 additions and 6 deletions

View file

@ -12,6 +12,7 @@ const app = express();
db.prepare("CREATE TABLE IF NOT EXISTS articles (id TEXT PRIMARY KEY, url TEXT, html TEXT)").run();
const index = fs.readFileSync("html/index.html", "utf8");
const error = fs.readFileSync("html/error.html", "utf8");
const notfound = fs.readFileSync("html/404.html", "utf8");
app.get("/read", async (req, res) => {
@ -21,22 +22,26 @@ app.get("/read", async (req, res) => {
const row = db.prepare("SELECT * FROM articles WHERE url = ?").get(url);
if (row) {
if (!force) {
if (force) {
db.prepare("UPDATE articles SET url = null WHERE id = ?").run(row.id);
} else if (row.html) {
return res.redirect(`/read/${row.id}`);
} else {
db.prepare("UPDATE articles SET url = null WHERE id = ?").run(row.id);
}
return res.send(notfound);
}
}
const parsed = await parse(url);
const parsed = await parse(url).catch(() => null);
const id = nanoid();
db.prepare("INSERT INTO articles (id, url, html) VALUES (?, ?, ?)").run(id, url, parsed);
if (!parsed) return res.send(error);
res.redirect(`/read/${id}`);
});
app.get("/read/:id", async (req, res, next) => {
const row = db.prepare("SELECT * FROM articles WHERE id = ?").get(req.params.id);
if (!row) return next();
if (!row.html) return res.send(notfound);
res.send(row.html);
});

View file

@ -6,7 +6,6 @@ import fs from "fs";
import fmtSize from "./size.mjs";
const template = fs.readFileSync("html/article.html", "utf8");
const error = fs.readFileSync("html/error.html", "utf8");
const dompurify = createDOMPurify(new JSDOM('').window);
const headers = {
@ -19,7 +18,7 @@ export default async function parse(url) {
if (content === null) return error;
const doc = new JSDOM(content, { url });
if(!isProbablyReaderable(doc.window.document)) return error;
if(!isProbablyReaderable(doc.window.document)) throw "not readerable";
const reader = new Readability(doc.window.document);
const article = reader.parse();