2025-12-07 12:50:07 +00:00

111 lines
3.7 KiB
JavaScript

import { globalHeaders } from "../utils/api.js";
import { getCombinedCachedContent } from "../utils/contentCache.js";
import { importNotionBlogs } from "../objects/blogs.js";
import { importNotionPages } from "../objects/pages.js";
import { importNotionSettings } from "../objects/settings.js";
import { importNotionNavigation } from "../objects/navigation.js";
import { importNotionPositions } from "../objects/positions.js";
import { importNotionCompanies } from "../objects/companies.js";
import { importImages } from "../objects/images.js";
import { importNotionCvs } from "../objects/cv.js";
import { importFiles } from "../objects/files.js";
import { importNotionProjects } from "../objects/projects.js";
import { importVideos } from "../objects/videos.js";
import { handleBlurHashUpdate } from "../utils/imageCache";
// Fetch or return cached content
export async function getCachedContent(env, request) {
// Try to get combined cached content
var cachedContent = await getCombinedCachedContent(env);
const noBlogs =
cachedContent.blogs?.length === 0 || cachedContent.blogs == null;
const noPages =
cachedContent.pages?.length === 0 || cachedContent.pages == null;
const noSettings =
Object.keys(cachedContent.settings).length === 0 ||
cachedContent.settings == null;
const noCompanies =
cachedContent.companies?.length === 0 || cachedContent.companies == null;
const noCvs = cachedContent.cvs?.length === 0 || cachedContent.cvs == null;
const noProjects =
cachedContent.projects?.length === 0 || cachedContent.projects == null;
if (noBlogs || noPages || noSettings || noCompanies || noCvs || noProjects) {
await importNotionNavigation(env);
}
if (noSettings) {
cachedContent.settings = await importNotionSettings(env);
}
if (noBlogs) {
cachedContent.blogs = await importNotionBlogs(env);
cachedContent.images = await importImages(env);
cachedContent.files = await importFiles(env);
cachedContent.videos = await importVideos(env);
await handleBlurHashUpdate(request, env);
}
if (noPages) {
cachedContent.pages = await importNotionPages(env);
cachedContent.images = await importImages(env);
cachedContent.files = await importFiles(env);
cachedContent.videos = await importVideos(env);
await handleBlurHashUpdate(request, env);
}
if (noCvs) {
cachedContent.cvs = await importNotionCvs(env);
cachedContent.files = await importFiles(env);
}
if (noCompanies) {
await importNotionPositions(env);
cachedContent.companies = await importNotionCompanies(env);
cachedContent.images = await importImages(env);
cachedContent.files = await importFiles(env);
cachedContent.videos = await importVideos(env);
await handleBlurHashUpdate(request, env);
}
if (noProjects) {
cachedContent.projects = await importNotionProjects(env);
cachedContent.images = await importImages(env);
cachedContent.files = await importFiles(env);
cachedContent.videos = await importVideos(env);
}
if (cachedContent) {
return new Response(
JSON.stringify({
...cachedContent,
blogs: cachedContent.blogs.filter((blog) => blog.published == true),
}),
{
headers: globalHeaders(request),
}
);
}
return new Response(JSON.stringify({}), { headers: globalHeaders(request) });
}
export async function handleContentRequest(request, env) {
try {
return await getCachedContent(env, request);
} catch (error) {
console.error("Error handling content request:", error);
return new Response(
JSON.stringify({
error: "Failed to fetch content",
message: error.message,
}),
{
status: 500,
headers: globalHeaders(request),
}
);
}
}