-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1529df0
commit 869c1bb
Showing
18 changed files
with
159 additions
and
186 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
import { GetCloudDocumentResponse } from "@/types"; | ||
import { NextResponse } from "next/server"; | ||
import { findUserDocument } from "@/repositories/document"; | ||
|
||
const PUBLIC_URL = process.env.PUBLIC_URL; | ||
const PDF_WORKER_URL = process.env.PDF_WORKER_URL; | ||
|
||
export async function GET(request: Request) { | ||
try { | ||
if (!PUBLIC_URL) return NextResponse.json({ error: { title: "Something went wrong", subtitle: "Please set up the environment variable PUBLIC_URL" }, }, { status: 500 }); | ||
const url = new URL(request.url); | ||
const search = url.searchParams; | ||
const handle = url.pathname.split("/").pop(); | ||
const revision = search.get('v'); | ||
|
||
const metadata = await fetch(`${PUBLIC_URL}/api/documents/${handle}/metadata`); | ||
const { data, error } = await metadata.json() as GetCloudDocumentResponse; | ||
if (error || !data || data.private) return fetch(request.url.replace('/pdf', '/embed')); | ||
const name = data.name; | ||
if (!revision) url.searchParams.set('v', data?.head || ""); | ||
if (PDF_WORKER_URL) url.hostname = PDF_WORKER_URL; | ||
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 = ''; } | ||
else url.pathname = `/api/pdf/${handle}`; | ||
if (url.hostname === 'localhost') url.protocol = 'http:'; | ||
const response = await fetch(url.toString(), { cache: 'force-cache' }); | ||
response.headers.set("Content-Disposition", `inline; filename="${name}.pdf"`); | ||
if (!response.ok) throw new Error("Couldn't generate PDF"); | ||
response.headers.set("Content-Disposition", `inline; filename="${encodeURIComponent(document.name)}.pdf"`); | ||
return response; | ||
} catch (error) { | ||
console.log(error); | ||
return NextResponse.json({ error: { title: "Something went wrong", subtitle: "Please try again later" } }, { status: 500 }); | ||
console.error(error); | ||
const url = new URL(request.url); | ||
if (url.hostname === 'localhost') url.protocol = 'http:'; | ||
url.pathname = url.pathname.replace('/pdf', '/embed'); | ||
const response = await fetch(url.toString()); | ||
const html = await response.text(); | ||
return new Response(html, { status: response.status, headers: { "Content-Type": "text/html" } }); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.