diff --git a/index.mjs b/index.mjs index 2954074..8f5fef8 100644 --- a/index.mjs +++ b/index.mjs @@ -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); }); diff --git a/js/parse.mjs b/js/parse.mjs index 0cbe74e..4f880ea 100644 --- a/js/parse.mjs +++ b/js/parse.mjs @@ -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();