72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { prisma } from "@/lib/prisma";
|
|
|
|
const BASE_URL = `${process.env.FEED_URL}`
|
|
|
|
function escapeXml(value: string): string {
|
|
return value
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/'/g, "'");
|
|
}
|
|
|
|
export async function GET() {
|
|
const items = await prisma.artwork.findMany({
|
|
where: { published: true },
|
|
orderBy: { createdAt: "desc" },
|
|
take: 10,
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
description: true,
|
|
altText: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
},
|
|
});
|
|
|
|
const lastBuildDate =
|
|
items[0]?.updatedAt?.toUTCString() ?? new Date().toUTCString();
|
|
|
|
const itemXml = items
|
|
.map((item) => {
|
|
const title = escapeXml(item.name || "Artwork");
|
|
const description = escapeXml(
|
|
item.description || item.altText || item.name || "Artwork"
|
|
);
|
|
const link = `${BASE_URL}/artworks/single/${item.id}`;
|
|
const pubDate = item.createdAt.toUTCString();
|
|
|
|
return [
|
|
"<item>",
|
|
`<title>${title}</title>`,
|
|
`<link>${link}</link>`,
|
|
`<guid isPermaLink="true">${link}</guid>`,
|
|
`<description>${description}</description>`,
|
|
`<pubDate>${pubDate}</pubDate>`,
|
|
"</item>",
|
|
].join("");
|
|
})
|
|
.join("");
|
|
|
|
const xml = [
|
|
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
'<rss version="2.0">',
|
|
"<channel>",
|
|
"<title>Gaertan Art - Latest Artworks</title>",
|
|
`<link>${BASE_URL}</link>`,
|
|
"<description>Ten newest artworks from Gaertan Art.</description>",
|
|
`<lastBuildDate>${lastBuildDate}</lastBuildDate>`,
|
|
itemXml,
|
|
"</channel>",
|
|
"</rss>",
|
|
].join("");
|
|
|
|
return new Response(xml, {
|
|
headers: {
|
|
"Content-Type": "application/rss+xml; charset=utf-8",
|
|
},
|
|
});
|
|
}
|