Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): log OneNote resource downloads

Elian Doran02f167d176ee
diff --git a/apps/server/src/services/import/onenote/graph.ts b/apps/server/src/services/import/onenote/graph.ts
index bf12226f34..cd83432f75 100644
--- a/apps/server/src/services/import/onenote/graph.ts
+++ b/apps/server/src/services/import/onenote/graph.ts
@@ -5,6 +5,8 @@
* Graph reference: https://learn.microsoft.com/en-us/graph/api/resources/onenote-api-overview
*/
+import { getLog } from "@triliumnext/core";
+
import { safeFetch } from "../../safe_fetch.js";
import { extractPageId } from "./links.js";
@@ -42,7 +44,9 @@ async function graphFetch(accessToken: string, url: string): Promise<Response> {
// Drain the throttled response so its connection is released before we wait and retry.
await response.body?.cancel();
- await delay(retryDelayMs(response.headers.get("Retry-After"), attempt));
+ const waitMs = retryDelayMs(response.headers.get("Retry-After"), attempt);
+ getLog().info(`OneNote import: Graph throttled (HTTP ${response.status}) on ${url}; retry ${attempt + 1}/${MAX_RETRIES} in ${waitMs}ms`);
+ await delay(waitMs);
}
}
@@ -235,7 +239,9 @@ export async function getResource(accessToken: string, url: string): Promise<{ c
throw new Error(`Failed to fetch OneNote resource (HTTP ${response.status})`);
}
const buffer = new Uint8Array(await response.arrayBuffer());
- return { content: buffer, contentType: response.headers.get("content-type") ?? "application/octet-stream" };
+ const contentType = response.headers.get("content-type") ?? "application/octet-stream";
+ getLog().info(`OneNote import: downloaded resource (${contentType}, ${buffer.length} bytes) from ${url}`);
+ return { content: buffer, contentType };
}
interface RawSection {
diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.ts
index 5bde62ecee..41dfa05fb0 100644
--- a/apps/server/src/services/import/onenote/importer.ts
+++ b/apps/server/src/services/import/onenote/importer.ts
@@ -110,7 +110,7 @@ export async function importSelection({ accessToken, parentNoteId, sections, tas
for (const page of pages) {
const { html: rawHtml, inkml } = await graph.getPageContent(accessToken, page.id);
const html = converter.convertPageHtml(rawHtml);
- const resources = await downloadPageResources(accessToken, html);
+ const resources = await downloadPageResources(accessToken, page.title, html);
fetchedPages.push({ title: page.title, level: page.level, pageId: page.pageId, html, rawHtml, rawInkml: inkml, inkSvg: inkmlToSvg(inkml), resources, createdDateTime: page.createdDateTime, lastModifiedDateTime: page.lastModifiedDateTime });
taskContext.increaseProgressCount();
}
@@ -310,7 +310,7 @@ function renderInkFigure(note: BNote, svg: string): string {
* downloads each once. Failures are logged and skipped so one missing resource doesn't abort the whole
* import; the reference is simply left untouched.
*/
-async function downloadPageResources(accessToken: string, html: string): Promise<DownloadedResource[]> {
+async function downloadPageResources(accessToken: string, pageTitle: string, html: string): Promise<DownloadedResource[]> {
const root = parse(html);
const refs = new Map<string, Omit<DownloadedResource, "content">>();
@@ -328,11 +328,19 @@ async function downloadPageResources(accessToken: string, html: string): Promise
}
}
+ if (refs.size === 0) {
+ return [];
+ }
+
+ const all = Array.from(refs.values());
+ const imageCount = all.filter((ref) => ref.kind === "image").length;
+ getLog().info(`OneNote import: page '${pageTitle}' references ${all.length} resource(s) (${imageCount} image(s), ${all.length - imageCount} attachment(s)); downloading with concurrency ${RESOURCE_DOWNLOAD_CONCURRENCY}`);
+
// Downloads are independent stateless GETs, so fetch them concurrently — but with a bounded pool,
// not all at once: an image-heavy page (hundreds of images) would otherwise open hundreds of
// simultaneous connections, triggering Graph throttling (429) and a large memory spike, which in
// practice dropped most images and left the note nearly empty. A failed download is skipped.
- const resources = await mapWithConcurrency(Array.from(refs.values()), RESOURCE_DOWNLOAD_CONCURRENCY, async (ref) => {
+ const downloaded = await mapWithConcurrency(all, RESOURCE_DOWNLOAD_CONCURRENCY, async (ref) => {
try {
const { content, contentType } = await graph.getResource(accessToken, ref.url);
return { ...ref, mime: ref.mime || contentType, content };
@@ -341,7 +349,12 @@ async function downloadPageResources(accessToken: string, html: string): Promise
return null;
}
});
- return resources.filter((resource): resource is DownloadedResource => resource !== null);
+
+ const resources = downloaded.filter((resource): resource is DownloadedResource => resource !== null);
+ const failed = all.length - resources.length;
+ const totalBytes = resources.reduce((sum, resource) => sum + resource.content.length, 0);
+ getLog().info(`OneNote import: page '${pageTitle}' downloaded ${resources.length}/${all.length} resource(s) (${Math.round(totalBytes / 1024)} KiB)${failed > 0 ? `; ${failed} failed and were skipped` : ""}`);
+ return resources;
}
/** Caps how many page resources (images, attachments) are downloaded from Graph at once. */