2024-08-17 20:21:23 +02:00
|
|
|
import { readFile, mkdir, writeFile } from "node:fs/promises";
|
2024-08-04 16:15:09 +02:00
|
|
|
import { join } from "node:path";
|
|
|
|
|
import { md } from "$lib/utils";
|
2024-08-05 14:38:44 +02:00
|
|
|
import type { Actions, PageServerLoad } from "./$types";
|
2024-08-14 19:33:41 +02:00
|
|
|
import { API_BASE_PREFIX, NGINX_BASE_PREFIX } from "$lib/utils";
|
2024-08-17 19:29:10 +02:00
|
|
|
import { render } from "svelte/server";
|
|
|
|
|
import BlogIndex from "$lib/templates/blog/BlogIndex.svelte";
|
|
|
|
|
import BlogArticle from "$lib/templates/blog/BlogArticle.svelte";
|
2024-08-03 18:07:27 +02:00
|
|
|
|
2024-08-17 19:29:10 +02:00
|
|
|
export const load: PageServerLoad = async ({ params, fetch, cookies }) => {
|
2024-08-14 19:33:41 +02:00
|
|
|
const websiteOverviewData = await fetch(
|
|
|
|
|
`${API_BASE_PREFIX}/website_overview?id=eq.${params.websiteId}`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
|
|
|
|
}
|
2024-08-04 16:15:09 +02:00
|
|
|
}
|
2024-08-14 19:33:41 +02:00
|
|
|
);
|
2024-08-04 16:15:09 +02:00
|
|
|
|
|
|
|
|
const websiteOverview = await websiteOverviewData.json();
|
|
|
|
|
|
2024-08-07 19:13:39 +02:00
|
|
|
generateStaticFiles(websiteOverview);
|
2024-08-04 17:46:41 +02:00
|
|
|
|
2024-08-18 13:48:36 +02:00
|
|
|
const websitePreviewUrl = `${NGINX_BASE_PREFIX}/previews/${websiteOverview.id}/index.html`;
|
2024-08-10 22:20:57 +02:00
|
|
|
|
2024-08-04 17:46:41 +02:00
|
|
|
return {
|
2024-08-10 22:20:57 +02:00
|
|
|
websiteOverview,
|
|
|
|
|
websitePreviewUrl
|
2024-08-04 17:46:41 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-05 14:38:44 +02:00
|
|
|
export const actions: Actions = {
|
2024-08-10 22:20:57 +02:00
|
|
|
publishWebsite: async ({ request }) => {
|
2024-08-04 17:46:41 +02:00
|
|
|
const data = await request.formData();
|
|
|
|
|
const websiteOverview = JSON.parse(data.get("website-overview") as string);
|
|
|
|
|
|
2024-08-07 19:13:39 +02:00
|
|
|
generateStaticFiles(websiteOverview, false);
|
2024-08-10 22:20:57 +02:00
|
|
|
|
|
|
|
|
return { success: true, message: "Successfully published website" };
|
2024-08-04 17:46:41 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-07 19:13:39 +02:00
|
|
|
const generateStaticFiles = async (websiteData: any, isPreview: boolean = true) => {
|
2024-08-17 19:29:10 +02:00
|
|
|
const { head, body } = render(BlogIndex, {
|
|
|
|
|
props: {
|
|
|
|
|
title: websiteData.title,
|
|
|
|
|
logoType: websiteData.logo_type,
|
|
|
|
|
logo: websiteData.logo_text,
|
|
|
|
|
mainContent: md.render(websiteData.main_content ?? ""),
|
|
|
|
|
articles: websiteData.articles ?? [],
|
|
|
|
|
footerAdditionalText: websiteData.additional_text ?? ""
|
|
|
|
|
}
|
2024-08-04 16:15:09 +02:00
|
|
|
});
|
2024-08-17 19:29:10 +02:00
|
|
|
|
|
|
|
|
const indexFileContents = head.concat(body);
|
2024-08-04 17:46:41 +02:00
|
|
|
|
|
|
|
|
let uploadDir = "";
|
|
|
|
|
|
|
|
|
|
if (isPreview) {
|
2024-08-18 13:48:36 +02:00
|
|
|
uploadDir = join("/", "var", "www", "archtika-websites", "previews", websiteData.id);
|
2024-08-04 17:46:41 +02:00
|
|
|
} else {
|
2024-08-18 13:48:36 +02:00
|
|
|
uploadDir = join("/", "var", "www", "archtika-websites", websiteData.id);
|
2024-08-04 17:46:41 +02:00
|
|
|
}
|
2024-08-04 16:15:09 +02:00
|
|
|
|
|
|
|
|
await mkdir(uploadDir, { recursive: true });
|
|
|
|
|
await writeFile(join(uploadDir, "index.html"), indexFileContents);
|
|
|
|
|
await mkdir(join(uploadDir, "articles"), { recursive: true });
|
|
|
|
|
|
2024-08-08 20:31:38 +02:00
|
|
|
for (const article of websiteData.articles ?? []) {
|
2024-08-04 16:15:09 +02:00
|
|
|
const articleFileName = article.title.toLowerCase().split(" ").join("-");
|
|
|
|
|
|
2024-08-17 19:29:10 +02:00
|
|
|
const { head, body } = render(BlogArticle, {
|
|
|
|
|
props: {
|
|
|
|
|
title: article.title,
|
|
|
|
|
logoType: websiteData.logo_type,
|
|
|
|
|
logo: websiteData.logo_text,
|
2024-08-17 20:21:23 +02:00
|
|
|
coverImage: article.cover_image
|
2024-08-18 13:48:36 +02:00
|
|
|
? `${API_BASE_PREFIX === "/api" ? "https://demo.archtika.com/api" : API_BASE_PREFIX}/rpc/retrieve_file?id=${article.cover_image}`
|
2024-08-17 20:21:23 +02:00
|
|
|
: "",
|
2024-08-17 19:29:10 +02:00
|
|
|
publicationDate: article.publication_date,
|
|
|
|
|
mainContent: md.render(article.main_content ?? ""),
|
|
|
|
|
footerAdditionalText: websiteData.additional_text ?? ""
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const articleFileContents = head.concat(body);
|
2024-08-03 18:07:27 +02:00
|
|
|
|
2024-08-04 17:46:41 +02:00
|
|
|
await writeFile(join(uploadDir, "articles", `${articleFileName}.html`), articleFileContents);
|
2024-08-03 18:07:27 +02:00
|
|
|
}
|
2024-08-17 20:21:23 +02:00
|
|
|
|
2024-08-17 20:51:23 +02:00
|
|
|
const styles = await readFile(`${process.cwd()}/template-styles/blog-styles.css`, {
|
2024-08-17 20:21:23 +02:00
|
|
|
encoding: "utf-8"
|
|
|
|
|
});
|
|
|
|
|
await writeFile(join(uploadDir, "styles.css"), styles);
|
2024-08-03 18:07:27 +02:00
|
|
|
};
|