chore short link redirect logic

This commit is contained in:
oiov
2025-03-27 11:00:21 +08:00
parent fed63224a4
commit b2eec4a606
5 changed files with 53 additions and 26 deletions
+4 -4
View File
@@ -18,7 +18,7 @@ export async function POST(req: NextRequest) {
browser,
} = await req.json();
if (!slug || !ip) return Response.json(null);
if (!slug || !ip) return Response.json("Missing[0000]");
const res = await getUrlBySuffix(slug);
if (res?.target && res?.active === 1) {
@@ -28,7 +28,7 @@ export async function POST(req: NextRequest) {
const expirationTime = createdAt + expirationMilliseconds;
if (res.expiration !== "-1" && now > expirationTime) {
return Response.json(null);
return Response.json("Expired[0001]");
}
// console.log("[api/s]", device, browser);
@@ -48,8 +48,8 @@ export async function POST(req: NextRequest) {
});
return Response.json(res.target);
}
return Response.json(null);
return Response.json("Disabled[0002]");
} catch (error) {
return Response.json(null);
return Response.json("Error[0003]");
}
}
-16
View File
@@ -26,22 +26,6 @@ export const docsConfig: DocsConfig = {
href: "/docs/short-urls",
icon: "page",
},
// {
// title: "Terms of Service",
// href: "/terms",
// icon: "page",
// },
// {
// title: "Privacy Policy",
// href: "/privacy",
// icon: "page",
// },
// {
// title: "Newsletter",
// href: "/docs/newsletter",
// icon: "page",
// },
],
},
{
+21
View File
@@ -27,3 +27,24 @@ WR.DO provides a simple access statistics feature that can be used to track the
- **Expiration**: The short link will expire after the specified time.
Once the generated short chain becomes invalid, it will not be deleted. When accessing the short chain again, it will be redirected to this page. Users can reset the short chain validity period to activate.
## Problems
### Expired Links
If you see this, it means that this short link has expired.
Please contact the creator of this short link to reactivate, or create a new short link.
### Missing links
If you see this, it means that this short link does not exist.
Please contact the creator of this short link to reactivate, or create a new short link.
### Disabled links
If you see this, it means that this short link has been disabled.
Please contact the creator of this short link to reactivate, or create a new short link.
### Error links
If you see this, it means that this short link has an error. Please contact administrator.
+27 -5
View File
@@ -10,6 +10,13 @@ export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};
const redirectMap = {
"Missing[0000]": "/docs/short-urls#missing-links",
"Expired[0001]": "/docs/short-urls#expired-links",
"Disabled[0002]": "/docs/short-urls#disabled-links",
"Error[0003]": "/docs/short-urls#error-links",
};
// 提取短链接处理逻辑
async function handleShortUrl(req: NextAuthRequest) {
if (!req.url.includes("/s/")) return NextResponse.next();
@@ -43,12 +50,28 @@ async function handleShortUrl(req: NextAuthRequest) {
});
if (!res.ok)
return NextResponse.redirect(`${siteConfig.url}/docs/short-urls`, 302);
return NextResponse.redirect(
`${siteConfig.url}${redirectMap["Error[0003]"]}`,
302,
);
const target = await res.json();
return target
? NextResponse.redirect(target, 302)
: NextResponse.redirect(`${siteConfig.url}/docs/short-urls`, 302);
if (!target || typeof target !== "string") {
return NextResponse.redirect(
`${siteConfig.url}${redirectMap["Error[0003]"]}`,
302,
);
}
if (target in redirectMap) {
return NextResponse.redirect(
`${siteConfig.url}${redirectMap[target]}`,
302,
);
}
return NextResponse.redirect(target, 302);
}
// 提取 slug
@@ -68,7 +91,6 @@ function parseUserAgent(ua: string) {
}
export default auth(async (req) => {
// console.log("请求地址", req.url);
try {
return await handleShortUrl(req);
} catch (error) {
+1 -1
View File
File diff suppressed because one or more lines are too long