backend/controller/latest.js

102 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const { PrismaClient } = require('@prisma/client')
2024-02-15 15:13:12 +00:00
const redis = require('../utils/redis.js')
2023-03-03 16:44:40 +00:00
const prisma = new PrismaClient()
function createSitemapXML(urls) {
const xml = urls.map(url => `
<url>
<loc>${url}</loc>
<changefreq>never</changefreq>
<priority>0.7</priority>
</url>
`).join('');
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${xml}
</urlset>`;
}
function createSitemapIndexXML(sitemaps) {
const xml = sitemaps.map((sitemap, index) => `
<sitemap>
<loc>https://api.preservetube.com/${sitemap}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
</sitemap>
`).join('');
return `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${xml}
</sitemapindex>`;
}
2023-03-03 16:44:40 +00:00
exports.getLatest = async (req, res) => {
2024-02-15 15:13:12 +00:00
let json
const cached = await redis.get('latest')
if (cached) {
json = JSON.parse(cached)
} else {
json = await prisma.videos.findMany({
take: 90,
orderBy: [
{
archived: 'desc'
}
],
select: {
id: true,
title: true,
thumbnail: true,
published: true,
archived: true,
channel: true,
channelId: true,
channelAvatar: true,
channelVerified: true
2023-03-03 16:44:40 +00:00
}
2024-02-15 15:13:12 +00:00
})
await redis.set('latest', JSON.stringify(json), 'EX', 3600)
}
res.json(json)
2024-09-12 17:32:18 +01:00
}
exports.getSitemap = async (req, res) => {
const cachedSitemapIndex = await redis.get('sitemap-index');
if (cachedSitemapIndex) {
2024-09-12 17:32:18 +01:00
res.header('Content-Type', 'application/xml');
return res.send(cachedSitemapIndex);
2024-09-12 17:32:18 +01:00
}
const dbVideos = await prisma.videos.findMany({
select: {
id: true,
},
});
const urls = dbVideos.map((video) => `https://preservetube.com/watch?v=${video.id}`);
const sitemaps = [];
for (let i = 0; i < urls.length; i += 50000) {
const batch = urls.slice(i, i + 50000);
await redis.set(`sitemap-${sitemaps.length}`, createSitemapXML(batch), 'EX', 86400);
sitemaps.push(`sitemap-${sitemaps.length}.xml`);
}
2024-09-12 17:32:18 +01:00
const sitemapIndexXML = createSitemapIndexXML(sitemaps);
await redis.set('sitemap-index', sitemapIndexXML, 'EX', 86400);
2024-09-12 17:32:18 +01:00
res.header('Content-Type', 'application/xml');
res.send(sitemapIndexXML);
};
exports.getSubSitemap = async (req, res) => {
const cachedSitemap = await redis.get(`sitemap-${req.params.index}`);
if (cachedSitemap) {
res.header('Content-Type', 'application/xml');
return res.send(cachedSitemap);
}
res.status(404).send('');
2024-09-12 17:32:18 +01:00
};