Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions convex/lib/paperPdfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* A copy bought from OpenAlex is kept under the paper's OpenAlex ID. One fetched by a link is kept under that link,
* so it stands for what the link served and nothing more: nobody's file, and no wrong file a link led to, stands in
* for a paper.
* for a paper. A paper with no link of its own, found by its DOI, is kept under the address its copy came from.
* A file from a person's own Zotero library is theirs: it never goes into the store, and the store is never read for it.
*
* The assistant pays for a copy here (`charge: "self"`). Extraction adds what a copy costs to the paper's own charge
Expand Down Expand Up @@ -290,10 +290,10 @@ export async function paperPdf(ctx: ActionCtx, payer: PdfPayer, paper: PaperRef,
if (paper.url || paper.doi) {
// A file from a person's Zotero library is theirs to read, and a miss on it says nothing about the paper.
const memory = isZotero(paper) ? undefined : searchMemory(ctx, paper.url, options);
const { pdfBlob, error } = await downloadPdf(paper.url ?? "", { doi: paper.doi, openalexId: work ?? undefined, zoteroConnection: paper.zotero, allowHttp: options.allowHttp, maxBytes: options.maxBytes, ...(memory ? { memory } : {}) });
const { pdfBlob, url: foundAt, error } = await downloadPdf(paper.url ?? "", { doi: paper.doi, openalexId: work ?? undefined, zoteroConnection: paper.zotero, allowHttp: options.allowHttp, maxBytes: options.maxBytes, ...(memory ? { memory } : {}) });
const bytes = pdfBlob ? new Uint8Array(await pdfBlob.arrayBuffer()) : null;
if (bytes && isPdf(bytes)) {
const key = isZotero(paper) ? null : await linkKey(paper.url);
const key = isZotero(paper) ? null : await linkKey(paper.url ?? foundAt);
const kept = key !== null && (await keep(key, bytes));
// A caller that sends the stored copy can't go on without one.
if (!options.bytes && !kept) return { error: NOT_KEPT };
Expand Down
15 changes: 15 additions & 0 deletions convex/research/paperPdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ describe("extraction's PDFs from the store", () => {
expect(read).toMatchObject({ error: expect.stringMatching(/too large/) });
});

it("keeps a copy found by DOI for a paper with no link, under the address the copy came from", async () => {
const t = convexTest(schema);
const answer = fetchMock.getMockImplementation()!;
fetchMock.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = new URL(input instanceof Request ? input.url : String(input));
if (url.hostname !== "api.crossref.org") return answer(input, init);
return new Response(JSON.stringify({ message: { DOI: "10.1/nolink", link: [{ URL: PDF, "content-type": "application/pdf" }] } }), { status: 200, headers: { "content-type": "application/json" } });
});

// As the assistant reads: it is sent the stored copy, so a copy that can't be kept is no use to it.
const found = await paperPdf(actionsOf(t), payer("j1"), { openalexId: "W12", doi: "10.1/nolink" }, { ...EXTRACTION, bytes: false });

expect(found).toMatchObject({ key: await byLink(PDF), from: "link", credits: 0 });
});

it("remembers a paper with no free copy, and asks no site about it again until its time", async () => {
const t = convexTest(schema);
const gone = { openalexId: "W10", doi: "10.1/gone", url: "https://example.org/gone.pdf", hasCopy: false };
Expand Down
4 changes: 3 additions & 1 deletion lib/pdf/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface DownloadPdfOptions {
export interface DownloadPdfResult {
success: boolean;
pdfBlob?: Blob;
/** The address the search found the copy at. */
url?: string;
error?: string;
}

Expand Down Expand Up @@ -65,7 +67,7 @@ export async function downloadPdf(
httpsOnly: !options.allowHttp,
},
);
if (result.ok) return { success: true, pdfBlob: new Blob([result.bytes.slice().buffer], { type: "application/pdf" }) };
if (result.ok) return { success: true, pdfBlob: new Blob([result.bytes.slice().buffer], { type: "application/pdf" }), url: result.url };
// A copy that exists and is too large is not a paper with no PDF.
const tooLarge = result.attempts.some((attempt) => attempt.outcome === "too_large");
return { success: false, error: tooLarge ? "This paper's PDF is too large to read here." : WHY[result.reason] };
Expand Down
Loading