Skip to content

Commit

Permalink
Merge pull request #974 from MuckRock/allanlasser/issue836
Browse files Browse the repository at this point in the history
Attempted fix for private PDF 403 error
  • Loading branch information
allanlasser authored Dec 13, 2024
2 parents 1b81cab + 41ebce7 commit c489fe6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/lib/components/viewer/ViewerContext.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ layouts, stories, and tests.
import { getContext, onMount, setContext } from "svelte";
import { type Writable, writable } from "svelte/store";
import { pageFromHash, pdfUrl, shouldPaginate } from "$lib/api/documents";
import {
pageFromHash,
pdfUrl,
shouldPaginate,
assetUrl,
} from "$lib/api/documents";
import * as pdfjs from "pdfjs-dist/legacy/build/pdf.mjs";
if (!pdfjs.GlobalWorkerOptions.workerSrc) {
Expand Down Expand Up @@ -162,8 +167,8 @@ layouts, stories, and tests.
}
}
onMount(() => {
// we might move this to a load function
let retriesOn403Error = 0;
function loadPDF(asset_url: URL) {
if (!task) {
task = pdfjs.getDocument({ url: asset_url });
$pdf = task.promise;
Expand All @@ -172,12 +177,25 @@ layouts, stories, and tests.
$progress = p;
};
task.promise.catch((error) => {
console.error(error);
$currentErrors = [...$currentErrors, error];
throw error;
task.promise.catch(async (error) => {
if (error.status === 403 && retriesOn403Error < 5) {
// try to load the document again using a fresh asset_url
const fresh_asset_url = await assetUrl(document);
task = null;
retriesOn403Error++;
loadPDF(fresh_asset_url);
} else {
console.error(error);
$currentErrors = [...$currentErrors, error];
throw error;
}
});
}
}
onMount(() => {
// we might move this to a load function
loadPDF(asset_url);
});
afterNavigate(() => {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/components/viewer/stories/Viewer.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import doc from "@/test/fixtures/documents/document-expanded.json";
import txt from "@/test/fixtures/documents/document.txt.json";
import { searchWithin } from "@/test/handlers/documents";
import { simulatePDF403Error } from "@/test/handlers/viewer";
import { pdfUrl } from "@/lib/api/documents";
const document = doc as Document;
Expand Down Expand Up @@ -102,6 +104,21 @@
}}
/>

<Story
name="With 403 Error"
parameters={{
msw: { handlers: [simulatePDF403Error(pdfUrl(document).href)] },
}}
args={{
...args,
mode: "document",
document: {
...document,
access: "private",
},
}}
/>

<style>
.vh {
height: 100vh;
Expand Down
6 changes: 6 additions & 0 deletions src/test/handlers/viewer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { rest } from "msw";

export const simulatePDF403Error = (asset_url: string) =>
rest.get(asset_url, (req, res, ctx) =>
res(ctx.status(403, "Not Found"), ctx.json({ detail: "Not found." })),
);

0 comments on commit c489fe6

Please sign in to comment.