Rinze/src/routes/[link].js

32 lines
1012 B
JavaScript
Raw Normal View History

2021-04-28 04:13:36 +00:00
var linksCache = {}
const shortenerBase = process.env.SHORTENER_BASE || 'https://link.nanao.moe';
2022-07-24 16:25:35 +00:00
export async function GET({ params }) {
2021-04-28 04:13:36 +00:00
var path = params.link;
if (linksCache[path]) {
if (linksCache[path] != 'none') {
return {
status: 302,
headers: {
"Location": linksCache[path]
},
}
}
}
var fetchres = await fetch(shortenerBase + "/" + path, { redirect: "manual" });
var unshortenedUrl = fetchres.headers.get('Location');
if (unshortenedUrl == shortenerBase + "/") {
linksCache[path] = "none";
} else {
return {
status: 302,
headers: {
"Location": unshortenedUrl
},
}
}
}