Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
Fix PDF fallback when the preprint is not in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosfl committed Aug 5, 2020
1 parent 99405da commit 3a82a0c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/components/extension-fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default function ExtensionFallback() {
<Suspense fallback={<SuspenseLoading>Loading PDF</SuspenseLoading>}>
<PdfViewer
docId={preprintId}
pdfUrl={pdfUrl}
loading={<SuspenseLoading>Loading PDF</SuspenseLoading>}
/>
</Suspense>
Expand All @@ -80,6 +81,7 @@ export default function ExtensionFallback() {
<Suspense fallback={<SuspenseLoading>Loading PDF</SuspenseLoading>}>
<PdfViewer
docId={identifier}
pdfUrl={pdfUrl}
loading={<SuspenseLoading>Loading PDF</SuspenseLoading>}
/>
</Suspense>
Expand Down
4 changes: 2 additions & 2 deletions src/components/pdf-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CSS_MAX_WIDTH = 900; // keep in sync with CSS
* This implement an infinite scroll mechanism so that we never load more than a
* few pages at the time
*/
export default function PdfViewer({ docId, loading }) {
export default function PdfViewer({ docId, pdfUrl, loading }) {
const containerEl = useRef(null);
const getWidth = () => {
const el = containerEl.current;
Expand Down Expand Up @@ -79,7 +79,7 @@ export default function PdfViewer({ docId, loading }) {
<Document
file={`${process.env.API_URL}/api/pdf?preprintId=${encodeURIComponent(
docId
)}`}
)}${pdfUrl ? `&pdfUrl=${encodeURIComponent(pdfUrl)}` : ''}`}
loading={loading}
onLoadSuccess={async pdf => {
let dims = [];
Expand Down
23 changes: 14 additions & 9 deletions src/routes/api-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,21 @@ router.get(
* due to cross origin restriction we need to proxy the PDF
*/
router.get('/pdf', async (req, res, next) => {
let pdfUrl;
const preprintId = req.query.preprintId;
try {
const body = await req.db.get(`preprint:${preprintId}`);
pdfUrl = getPdfUrl(body);
if (!pdfUrl) {
return next(createError(500, `Could not determine PDF from prereview: ${JSON.stringify(body)}`));
let { preprintId, pdfUrl } = req.query;

// If we already have the pdfUrl, there's no need to look in the database.
// The pdfUrl is present in the case where the preprint is not in the database yet,
// but we were able to resolve it.
if (!pdfUrl) {
try {
const body = await req.db.get(`preprint:${preprintId}`);
pdfUrl = getPdfUrl(body);
if (!pdfUrl) {
return next(createError(500, `Could not determine PDF from prereview: ${JSON.stringify(body)}`));
}
} catch (err) {
return next(createError(500, `Prereview query failed: ${preprintId} ${JSON.stringify(err)}`));
}
} catch (err) {
return next(createError(500, `Prereview query failed: ${preprintId} ${JSON.stringify(err)}`));
}

if (!pdfUrl) {
Expand Down

0 comments on commit 3a82a0c

Please sign in to comment.