Files
outline/server/commands/documentImporter.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

110 lines
2.8 KiB
TypeScript

import mime from "mime-types";
import { truncate } from "es-toolkit/compat";
import type { ProsemirrorData } from "@shared/types";
import { DocumentValidation } from "@shared/validations";
import { serializer } from "@server/editor";
import { traceFunction } from "@server/logging/tracing";
import type { User } from "@server/models";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import type { Context } from "@server/types";
import { DocumentConverter } from "@server/converters/DocumentConverter";
import { InvalidRequestError } from "../errors";
type Props = {
user: User;
mimeType: string;
fileName: string;
content: Buffer | string;
ctx: Context;
};
type ImportResult = {
icon?: string;
text: string;
title: string;
state: Buffer;
};
/**
* Converts document content to state and validates size constraints.
*
* @param content The document content as Prosemirror JSON.
* @param title The document title (used in error messages).
* @returns The Y.Doc state buffer.
*/
function convertToState(content: ProsemirrorData, title: string): Buffer {
const ydoc = ProsemirrorHelper.toYDoc(content);
const state = ProsemirrorHelper.toState(ydoc);
if (state.length > DocumentValidation.maxStateLength) {
throw InvalidRequestError(
`The document "${title}" is too large to import, please reduce the length and try again`
);
}
return state;
}
async function documentImporter({
mimeType,
fileName,
content,
user,
ctx,
}: Props): Promise<ImportResult> {
// Find valid extensions and remove them from the title
const extensions = [
"docx",
"md",
"markdown",
"htm",
"html",
"csv",
"tsv",
"mhtml",
"mht",
"eml",
"textpack",
"pdf",
...(mime.extensions[mimeType] ?? []),
];
const fileTitle = fileName.replace(
new RegExp(`\\.(${extensions.join("|")})$`, "i"),
""
);
// Convert document using unified converter
const {
doc,
title: extractedTitle,
icon,
} = await DocumentConverter.convert(content, fileName, mimeType);
// Use extracted title or fall back to filename
let title = extractedTitle || fileTitle;
// Replace external images with attachments
const processedDoc = await ProsemirrorHelper.replaceImagesWithAttachments(
ctx,
doc,
user
);
// Serialize final text and handle empty documents
let text = serializer.serialize(processedDoc).trim();
// Empty paragraphs serialize to escaped newlines/backslashes, treat as empty
if (/^[\\\s]*$/.test(text)) {
text = "";
}
// Truncate title and validate size
title = truncate(title, { length: DocumentValidation.maxTitleLength });
const state = convertToState(processedDoc.toJSON() as ProsemirrorData, title);
return { text, state, title, icon };
}
export default traceFunction({
spanName: "documentImporter",
})(documentImporter);