feat(*.php): refactor, clean up and abstract dynamic page code
This commit is contained in:
parent
2f9038dccb
commit
60be0ed01d
30 changed files with 318 additions and 401 deletions
|
@ -1,84 +1,35 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/../../vendor/autoload.php";
|
||||
require __DIR__ . "/../../server/utils/init.php";
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use App\Classes\MovieFetcher;
|
||||
use voku\helper\HtmlMin;
|
||||
|
||||
$requestUri = $_SERVER["REQUEST_URI"];
|
||||
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
||||
$postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
||||
$postgrestApiKey = $_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
||||
|
||||
if (!preg_match('/^watching\/movies\/[\w-]+$/', $url)) {
|
||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||
exit();
|
||||
}
|
||||
|
||||
$cacheKey = "movie_" . md5($url);
|
||||
$movie = null;
|
||||
$useRedis = false;
|
||||
$fetcher = new MovieFetcher();
|
||||
$movie = $fetcher->fetch($url);
|
||||
|
||||
try {
|
||||
if (extension_loaded('redis')) {
|
||||
$redis = new Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$useRedis = true;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis not available: " . $e->getMessage());
|
||||
}
|
||||
|
||||
if ($useRedis && $redis->exists($cacheKey)) {
|
||||
$movie = json_decode($redis->get($cacheKey), true);
|
||||
} else {
|
||||
$apiUrl = "$postgrestUrl/optimized_movies?url=eq./" . $url;
|
||||
$client = new Client();
|
||||
|
||||
try {
|
||||
$response = $client->request("GET", $apiUrl, [
|
||||
"headers" => [
|
||||
"Accept" => "application/json",
|
||||
"Authorization" => "Bearer {$postgrestApiKey}",
|
||||
],
|
||||
]);
|
||||
|
||||
$movieData = json_decode($response->getBody(), true);
|
||||
|
||||
if (!$movieData) {
|
||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||
exit();
|
||||
}
|
||||
|
||||
$movie = $movieData[0];
|
||||
|
||||
if ($useRedis) {
|
||||
$redis->setex($cacheKey, 3600, json_encode($movie));
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log($e->getMessage());
|
||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||
exit();
|
||||
}
|
||||
if (!$movie) {
|
||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||
exit();
|
||||
}
|
||||
|
||||
$movie["description"] = parseMarkdown($movie["description"]);
|
||||
$pageTitle = htmlspecialchars(
|
||||
"Movies • " . $movie["title"],
|
||||
ENT_QUOTES,
|
||||
"UTF-8"
|
||||
);
|
||||
$pageDescription = truncateText(htmlspecialchars(
|
||||
strip_tags($movie["description"]),
|
||||
ENT_QUOTES,
|
||||
"UTF-8"
|
||||
), 250);
|
||||
$pageTitle = htmlspecialchars("Movies • " . $movie["title"], ENT_QUOTES, "UTF-8");
|
||||
$pageDescription = truncateText(htmlspecialchars(strip_tags($movie["description"]), ENT_QUOTES, "UTF-8"), 250);
|
||||
$ogImage = htmlspecialchars($movie["backdrop"], ENT_QUOTES, "UTF-8");
|
||||
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
||||
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
||||
|
||||
ob_start();
|
||||
|
||||
header("Cache-Control: public, max-age=3600");
|
||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
||||
?>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue