feat(reading): clean and refactor routing for books -> reading to separate paths

This commit is contained in:
Cory Dransfeldt 2025-05-16 13:03:55 -07:00
parent 8d9455940e
commit 2666d6ed67
No known key found for this signature in database
21 changed files with 51 additions and 89 deletions

View file

@ -9,18 +9,12 @@
$requestUri = $_SERVER["REQUEST_URI"];
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
if (strpos($url, "music/artists/") !== 0) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (strpos($url, "music/artists/") !== 0) redirectTo404();
$fetcher = new ArtistFetcher();
$artist = $fetcher->fetch($url);
if (!$artist) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (!$artist) redirectTo404();
$artist["description"] = parseMarkdown($artist["description"]);
$pageTitle = htmlspecialchars("Artists • " . $artist["name"], ENT_QUOTES, "UTF-8");

View file

@ -1,7 +1,7 @@
<?php
require __DIR__ . "/../vendor/autoload.php";
require __DIR__ . "/../server/utils/init.php";
require __DIR__ . "/../../vendor/autoload.php";
require __DIR__ . "/../../server/utils/init.php";
use App\Classes\BookFetcher;
use voku\helper\HtmlMin;
@ -9,37 +9,12 @@
$requestUri = $_SERVER["REQUEST_URI"];
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
if (preg_match('/^books\/years\/(\d{4})$/', $url, $matches)) {
$year = $matches[1];
$filePath = __DIR__ . "/years/{$year}.html";
if (file_exists($filePath)) {
echo file_get_contents($filePath);
exit();
} else {
echo file_get_contents(__DIR__ . "/../404/index.html");
exit();
}
}
if ($url === "books/years" || $url === "books") {
$indexPath = $url === "books" ? "index.html" : __DIR__ . "/../404/index.html";
readfile($indexPath);
exit();
}
if (!preg_match('/^books\/[\w-]+$/', $url)) {
echo file_get_contents(__DIR__ . "/../404/index.html");
exit();
}
if (!preg_match('/^reading\/books\/([\dXx-]+)$/', $url)) redirectTo404();
$fetcher = new BookFetcher();
$book = $fetcher->fetch($url);
if (!$book) {
echo file_get_contents(__DIR__ . "/../404/index.html");
exit();
}
if (!$book) redirectTo404();
$book["description"] = parseMarkdown($book["description"]);
$pageTitle = htmlspecialchars("Books • {$book["title"]} by {$book["author"]}", ENT_QUOTES, "UTF-8");

View file

@ -9,18 +9,12 @@
$requestUri = $_SERVER["REQUEST_URI"];
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
if (!preg_match('/^music\/genres\/[\w-]+$/', $url)) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (!preg_match('/^music\/genres\/[\w-]+$/', $url)) redirectTo404();
$fetcher = new GenreFetcher();
$genre = $fetcher->fetch($url);
if (!$genre) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (!$genre) redirectTo404();
$pageTitle = htmlspecialchars("Genres • " . $genre["name"], ENT_QUOTES, "UTF-8");
$pageDescription = truncateText(

View file

@ -9,18 +9,12 @@
$requestUri = $_SERVER["REQUEST_URI"];
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
if (!preg_match('/^watching\/movies\/[\w-]+$/', $url)) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (!preg_match('/^watching\/movies\/[\w-]+$/', $url)) redirectTo404();
$fetcher = new MovieFetcher();
$movie = $fetcher->fetch($url);
if (!$movie) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (!$movie) redirectTo404();
$movie["description"] = parseMarkdown($movie["description"]);
$pageTitle = htmlspecialchars("Movies • " . $movie["title"], ENT_QUOTES, "UTF-8");

View file

@ -9,18 +9,12 @@
$requestUri = $_SERVER["REQUEST_URI"];
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
if (!preg_match('/^watching\/shows\/[\w-]+$/', $url)) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (!preg_match('/^watching\/shows\/[\w-]+$/', $url)) redirectTo404();
$fetcher = new ShowFetcher();
$show = $fetcher->fetch($url);
if (!$show) {
echo file_get_contents(__DIR__ . "/../../404/index.html");
exit();
}
if (!$show) redirectTo404();
$pageTitle = htmlspecialchars("Show • " . $show["title"], ENT_QUOTES, "UTF-8");
$pageDescription = truncateText(

View file

@ -14,10 +14,7 @@
exit();
}
if (!preg_match('/^tags\/(.+?)(?:\/(\d+))?$/', $url, $matches)) {
echo file_get_contents(__DIR__ . "/../404/index.html");
exit();
}
if (!preg_match('/^tags\/(.+?)(?:\/(\d+))?$/', $url, $matches)) redirectTo404();
if (isset($matches[2]) && (int)$matches[2] === 1) {
header("Location: /tags/{$matches[1]}", true, 301);
@ -26,10 +23,7 @@
$tag = strtolower(urldecode($matches[1]));
if (!preg_match('/^[\p{L}\p{N} _\.\-\&]+$/u', $tag)) {
echo file_get_contents(__DIR__ . "/../404/index.html");
exit();
}
if (!preg_match('/^[\p{L}\p{N} _\.\-\&]+$/u', $tag)) redirectTo404();
$page = isset($matches[2]) ? max(1, (int)$matches[2]) : 1;
$pageSize = 20;
@ -37,7 +31,7 @@
$tagged = $fetcher->fetch($tag, $page, $pageSize);
if (!$tagged || count($tagged) === 0) {
echo file_get_contents(__DIR__ . '/../404/index.html');
header("Location: /404/", true, 302);
exit();
}

View file

@ -51,7 +51,7 @@
{%- when 'books' -%}
{%- assign overviewBook = books.all | filterBooksByStatus: 'started' | reverse | first %}
{%- assign ogImage = ogImageBaseUrl | append: overviewBook.image -%}
{%- when 'books-year' -%}
{%- when 'reading-year' -%}
{%- assign pageTitle = 'Books' | append: ' • ' | append: year.value | append: ' • ' | append: globals.site_name -%}
{%- capture pageDescription -%}
Here's what I read in {{ year.value }}.