Skip to content

Commit 8c41c61

Browse files
committed
add sitemap.xml, with torrent urls if unregistered viewing enabled
1 parent bdf48f2 commit 8c41c61

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

api/src/controllers/torrent.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,15 @@ export const listLatest = (tracker) => async (req, res, next) => {
671671
}
672672
};
673673

674+
export const listAll = async (req, res, next) => {
675+
try {
676+
const torrents = await Torrent.find({}, { infoHash: 1 }).lean();
677+
res.json(torrents);
678+
} catch (e) {
679+
next(e);
680+
}
681+
};
682+
674683
export const searchTorrents = (tracker) => async (req, res, next) => {
675684
const { query, category, source, tag, page } = req.query;
676685
try {

api/src/routes/torrent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
deleteTorrent,
66
fetchTorrent,
77
listLatest,
8+
listAll,
89
removeVote,
910
searchTorrents,
1011
toggleFreeleech,
@@ -28,6 +29,7 @@ export default (tracker) => {
2829
router.post("/toggle-freeleech/:infoHash", toggleFreeleech);
2930
router.post("/bookmark/:infoHash", toggleBookmark);
3031
router.get("/latest", listLatest(tracker));
32+
router.get("/all", listAll);
3133
router.get("/search", searchTorrents(tracker));
3234
return router;
3335
};

client/pages/sitemap.xml.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import getConfig from "next/config";
2+
3+
const Sitemap = () => {};
4+
5+
export const getServerSideProps = async ({ req, res }) => {
6+
const {
7+
publicRuntimeConfig: {
8+
SQ_BASE_URL,
9+
SQ_API_URL,
10+
SQ_ALLOW_UNREGISTERED_VIEW,
11+
},
12+
serverRuntimeConfig: { SQ_SERVER_SECRET },
13+
} = getConfig();
14+
15+
const urls = [SQ_BASE_URL, `${SQ_BASE_URL}/login`, `${SQ_BASE_URL}/register`];
16+
17+
if (SQ_ALLOW_UNREGISTERED_VIEW) {
18+
try {
19+
const listRes = await fetch(`${SQ_API_URL}/torrent/all`, {
20+
headers: {
21+
"Content-Type": "application/json",
22+
"X-Forwarded-For":
23+
req.headers["x-forwarded-for"] ?? req.socket.remoteAddress,
24+
"X-Sq-Server-Secret": SQ_SERVER_SECRET,
25+
"X-Sq-Public-Access": true,
26+
},
27+
});
28+
const torrents = await listRes.json();
29+
for (const { infoHash } of torrents) {
30+
urls.push(`${SQ_BASE_URL}/torrent/${infoHash}`);
31+
}
32+
} catch (e) {
33+
console.error(`[sq] could not list torrents: ${e}`);
34+
}
35+
}
36+
37+
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
38+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
39+
${urls
40+
.map(
41+
(url) => `<url>
42+
<loc>${url}</loc>
43+
</url>`
44+
)
45+
.join("\n")}
46+
</urlset>
47+
`;
48+
49+
res.setHeader("Content-Type", "text/xml");
50+
res.write(sitemap);
51+
res.end();
52+
53+
return { props: {} };
54+
};
55+
56+
export default Sitemap;

0 commit comments

Comments
 (0)