mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
* 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>
131 lines
3.7 KiB
TypeScript
131 lines
3.7 KiB
TypeScript
import { chunk, escapeRegExp } from "es-toolkit/compat";
|
|
import { errToString } from "@shared/utils/error";
|
|
import { AttachmentPreset } from "@shared/types";
|
|
import { isInternalUrl } from "@shared/utils/urls";
|
|
import attachmentCreator from "@server/commands/attachmentCreator";
|
|
import env from "@server/env";
|
|
import Logger from "@server/logging/Logger";
|
|
import { trace } from "@server/logging/tracing";
|
|
import type { User } from "@server/models";
|
|
import { Attachment } from "@server/models";
|
|
import FileStorage from "@server/storage/files";
|
|
import type { Context } from "@server/types";
|
|
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
|
|
import parseImages from "@server/utils/parseImages";
|
|
|
|
@trace()
|
|
export class TextHelper {
|
|
/**
|
|
* Converts attachment urls in documents to signed equivalents that allow
|
|
* direct access without a session cookie
|
|
*
|
|
* @param text The text either html or markdown which contains urls to be converted
|
|
* @param teamId The team context
|
|
* @param expiresIn The time that signed urls should expire (in seconds)
|
|
* @returns The replaced text
|
|
*/
|
|
static async attachmentsToSignedUrls(
|
|
text: string,
|
|
teamId: string,
|
|
expiresIn = 3000
|
|
) {
|
|
const attachmentIds = parseAttachmentIds(text);
|
|
if (!attachmentIds.length) {
|
|
return text;
|
|
}
|
|
|
|
const attachments = await Attachment.findAll({
|
|
where: {
|
|
id: attachmentIds,
|
|
teamId,
|
|
},
|
|
});
|
|
|
|
await Promise.all(
|
|
attachments.map(async (attachment) => {
|
|
const signedUrl = await FileStorage.getSignedUrl(
|
|
attachment.key,
|
|
expiresIn
|
|
);
|
|
|
|
text = text.replace(
|
|
new RegExp(escapeRegExp(attachment.redirectUrl), "g"),
|
|
signedUrl
|
|
);
|
|
})
|
|
);
|
|
return text;
|
|
}
|
|
|
|
/**
|
|
* Replaces remote and base64 encoded images in the given text with attachment
|
|
* urls and uploads the images to the storage provider.
|
|
*
|
|
* @param ctx The context
|
|
* @param markdown The text to replace the images in
|
|
* @param user The user context
|
|
* @returns The text with the images replaced
|
|
*/
|
|
static async replaceImagesWithAttachments(
|
|
ctx: Context,
|
|
markdown: string,
|
|
user: User,
|
|
options: {
|
|
/** If true, only process base64 encoded images */
|
|
base64Only?: boolean;
|
|
} = {}
|
|
) {
|
|
let output = markdown;
|
|
const images = parseImages(markdown);
|
|
const timeoutPerImage = Math.floor(
|
|
Math.min(env.REQUEST_TIMEOUT / images.length, 10000)
|
|
);
|
|
const chunks = chunk(images, 10);
|
|
|
|
for (const chunk of chunks) {
|
|
await Promise.all(
|
|
chunk.map(async (image) => {
|
|
// Skip attempting to fetch images that are not valid urls
|
|
if (!URL.canParse(image.src)) {
|
|
return;
|
|
}
|
|
|
|
if (isInternalUrl(image.src)) {
|
|
return;
|
|
}
|
|
if (options.base64Only && !image.src.startsWith("data:")) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const attachment = await attachmentCreator({
|
|
name: image.alt ?? "image",
|
|
url: image.src,
|
|
preset: AttachmentPreset.DocumentAttachment,
|
|
user,
|
|
fetchOptions: {
|
|
timeout: timeoutPerImage,
|
|
},
|
|
ctx,
|
|
});
|
|
|
|
if (attachment) {
|
|
output = output.replace(
|
|
new RegExp(escapeRegExp(image.src), "g"),
|
|
attachment.redirectUrl
|
|
);
|
|
}
|
|
} catch (err) {
|
|
Logger.warn("Failed to download image for attachment", {
|
|
error: errToString(err),
|
|
src: image.src,
|
|
});
|
|
}
|
|
})
|
|
);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
}
|