Skip to content

Commit

Permalink
add .pdf suffix to pdf shares
Browse files Browse the repository at this point in the history
  • Loading branch information
ibastawisi committed Jun 4, 2024
1 parent e3b0753 commit d073d8b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/app/pdf/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ export async function GET(request: Request) {
try {
const url = new URL(request.url);
const search = url.searchParams;
const handle = url.pathname.split("/").pop();
const handle = url.pathname.split("/").pop()?.split(".pdf")[0];
const revision = search.get('v');
if (!handle) throw new Error("No handle provided");
const document = await findUserDocument(handle, revision);
if (!document || document.private) throw new Error("Document not found");
if (!revision) url.searchParams.set('v', document.head);
if (PDF_WORKER_URL) { url.hostname = PDF_WORKER_URL; url.port = ''; }
if (PDF_WORKER_URL) {
url.hostname = PDF_WORKER_URL;
url.port = '';
url.pathname = url.pathname.split(".pdf")[0];
}
else url.pathname = `/api/pdf/${handle}`;
if (url.hostname === 'localhost') url.protocol = 'http:';
const response = await fetch(url.toString(), { cache: 'force-cache' });
Expand All @@ -23,7 +27,7 @@ export async function GET(request: Request) {
console.error(error);
const url = new URL(request.url);
if (url.hostname === 'localhost') url.protocol = 'http:';
url.pathname = url.pathname.replace('/pdf', '/embed');
url.pathname = url.pathname.replace('/pdf', '/embed').split(".pdf")[0];
const response = await fetch(url.toString());
const html = await response.text();
return new Response(html, { status: response.status, headers: { "Content-Type": "text/html" } });
Expand Down
4 changes: 4 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
url: `https://matheditor.me/view/${document.handle || document.id}`,
lastModified: document.updatedAt,
})),
...publishedDocuments.map((document) => ({
url: `https://matheditor.me/pdf/${document.handle || document.id}.pdf`,
lastModified: document.updatedAt,
})),
]
}
1 change: 1 addition & 0 deletions src/components/DocumentActions/Share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const ShareDocument: React.FC<{ userDocument: UserDocument, variant?: 'menuitem'
url.pathname = `/${format}/${handle || id}`;
if (revision && revision !== cloudDocument?.head) url.searchParams.append("v", revision);
if (format === "pdf") {
url.pathname += ".pdf";
const scale = formdata.get("scale") as string;
const landscape = formdata.get("landscape") as string;
const format = formdata.get("format") as string;
Expand Down

0 comments on commit d073d8b

Please sign in to comment.