error page
This commit is contained in:
parent
000000860b
commit
0000009749
2 changed files with 10 additions and 6 deletions
13
index.mjs
13
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();
|
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 index = fs.readFileSync("html/index.html", "utf8");
|
||||||
|
const error = fs.readFileSync("html/error.html", "utf8");
|
||||||
const notfound = fs.readFileSync("html/404.html", "utf8");
|
const notfound = fs.readFileSync("html/404.html", "utf8");
|
||||||
|
|
||||||
app.get("/read", async (req, res) => {
|
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);
|
const row = db.prepare("SELECT * FROM articles WHERE url = ?").get(url);
|
||||||
if (row) {
|
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}`);
|
return res.redirect(`/read/${row.id}`);
|
||||||
} else {
|
} 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();
|
const id = nanoid();
|
||||||
db.prepare("INSERT INTO articles (id, url, html) VALUES (?, ?, ?)").run(id, url, parsed);
|
db.prepare("INSERT INTO articles (id, url, html) VALUES (?, ?, ?)").run(id, url, parsed);
|
||||||
|
if (!parsed) return res.send(error);
|
||||||
res.redirect(`/read/${id}`);
|
res.redirect(`/read/${id}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/read/:id", async (req, res, next) => {
|
app.get("/read/:id", async (req, res, next) => {
|
||||||
const row = db.prepare("SELECT * FROM articles WHERE id = ?").get(req.params.id);
|
const row = db.prepare("SELECT * FROM articles WHERE id = ?").get(req.params.id);
|
||||||
if (!row) return next();
|
if (!row) return next();
|
||||||
|
if (!row.html) return res.send(notfound);
|
||||||
res.send(row.html);
|
res.send(row.html);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ import fs from "fs";
|
||||||
import fmtSize from "./size.mjs";
|
import fmtSize from "./size.mjs";
|
||||||
|
|
||||||
const template = fs.readFileSync("html/article.html", "utf8");
|
const template = fs.readFileSync("html/article.html", "utf8");
|
||||||
const error = fs.readFileSync("html/error.html", "utf8");
|
|
||||||
const dompurify = createDOMPurify(new JSDOM('').window);
|
const dompurify = createDOMPurify(new JSDOM('').window);
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
|
@ -19,7 +18,7 @@ export default async function parse(url) {
|
||||||
if (content === null) return error;
|
if (content === null) return error;
|
||||||
|
|
||||||
const doc = new JSDOM(content, { url });
|
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 reader = new Readability(doc.window.document);
|
||||||
const article = reader.parse();
|
const article = reader.parse();
|
||||||
|
|
Loading…
Reference in a new issue