Skip to content

Commit d69810a

Browse files
authored
feat(web): preview office files, fonts, and mermaid diagrams (pingdotgg#21362)
1 parent 3de45c6 commit d69810a

54 files changed

Lines changed: 8370 additions & 93 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/ipc/methods/window.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ vi.mock("electron", () => ({
2020
import * as DesktopBackendConfiguration from "../../backend/DesktopBackendConfiguration.ts";
2121
import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
2222
import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
23+
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
24+
import * as ElectronDialog from "../../electron/ElectronDialog.ts";
25+
import * as ElectronWindow from "../../electron/ElectronWindow.ts";
2326
import {
2427
getLocalEnvironmentBootstraps,
2528
getRelayTelemetryEnabled,
29+
pickProjectFavicon,
2630
probeRemoteEditors,
2731
} from "./window.ts";
2832

@@ -192,6 +196,77 @@ describe("getRelayTelemetryEnabled", () => {
192196
);
193197
});
194198

199+
describe("pickProjectFavicon", () => {
200+
const pickerLayer = (
201+
pickFiles: () => Effect.Effect<Array<string>>,
202+
settings?: DesktopAppSettings.DesktopSettings,
203+
) =>
204+
Layer.mergeAll(
205+
Layer.mock(ElectronDialog.ElectronDialog)({ pickFiles }),
206+
Layer.mock(ElectronWindow.ElectronWindow)({
207+
focusedMainOrFirst: Effect.succeedNone,
208+
}),
209+
DesktopAppSettings.layerTest(settings),
210+
);
211+
212+
it.effect("opens a single-image picker from the project directory", () =>
213+
Effect.gen(function* () {
214+
const pickFiles = vi.fn(() => Effect.succeed(["/pictures/icon.png"]));
215+
const result = yield* pickProjectFavicon
216+
.handler("/project")
217+
.pipe(Effect.provide(pickerLayer(pickFiles)));
218+
219+
assert.strictEqual(result, "/pictures/icon.png");
220+
assert.deepEqual(pickFiles.mock.calls, [
221+
[
222+
{
223+
owner: Option.none(),
224+
defaultPath: Option.some("/project"),
225+
multiple: false,
226+
filters: [
227+
{
228+
name: "Images",
229+
extensions: [
230+
"avif",
231+
"bmp",
232+
"gif",
233+
"ico",
234+
"heic",
235+
"jpeg",
236+
"jpg",
237+
"png",
238+
"svg",
239+
"tif",
240+
"tiff",
241+
"webp",
242+
"icns",
243+
],
244+
},
245+
],
246+
},
247+
],
248+
]);
249+
}),
250+
);
251+
252+
it.effect("does not open a picker while the local environment is off", () =>
253+
Effect.gen(function* () {
254+
const pickFiles = vi.fn(() => Effect.succeed(["/pictures/icon.png"]));
255+
const result = yield* pickProjectFavicon.handler("/project").pipe(
256+
Effect.provide(
257+
pickerLayer(pickFiles, {
258+
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
259+
localEnvironmentEnabled: false,
260+
}),
261+
),
262+
);
263+
264+
assert.strictEqual(result, null);
265+
assert.strictEqual(pickFiles.mock.calls.length, 0);
266+
}),
267+
);
268+
});
269+
195270
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
196271
"finds remote editors installed without PATH launchers",
197272
() =>

‎apps/mobile/src/components/ComposerContextSheet.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function ComposerContextSheet(props: {
111111
: undefined;
112112
if (record && !("payload" in record) && (record.kind === "image" || record.kind === "file")) {
113113
const mimeType = videoMimeType(record) ?? record.mimeType;
114-
const previewKind = filePreviewKind(record);
114+
const previewKind = filePreviewKind(record, { richDocuments: false });
115115
const resource = {
116116
_tag: "attachment" as const,
117117
attachmentId: record.attachmentId,

‎apps/mobile/src/lib/attachmentDocument.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function useAttachmentDocument(input: {
2929
readonly environmentId: EnvironmentId | null;
3030
readonly attachment: FileBackedComposerAttachment | null;
3131
}) {
32-
const kind = filePreviewKind(input);
32+
const kind = filePreviewKind(input, { richDocuments: false });
3333
const delimiter = filePreviewDelimiter(input);
3434
const shareController = useRef<AbortController | null>(null);
3535
useEffect(() => () => shareController.current?.abort(), []);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, it } from "vite-plus/test";
2+
import { filePreviewKind } from "@t3tools/shared/filePreview";
3+
4+
it.each([
5+
[
6+
"report.docx",
7+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
8+
"unsupported",
9+
],
10+
[
11+
"budget.xlsx",
12+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
13+
"unsupported",
14+
],
15+
[
16+
"deck.pptx",
17+
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
18+
"unsupported",
19+
],
20+
["report.docx", "text/plain", "text"],
21+
["report.docx", "application/octet-stream", "unsupported"],
22+
["chart.mmd", "text/vnd.mermaid", "text"],
23+
["chart.mermaid", "text/plain", "text"],
24+
["chart.mmd", "application/octet-stream", "unsupported"],
25+
["font.ttf", "font/ttf", "unsupported"],
26+
] as const)("preserves mobile's existing path for %s (%s)", (name, mimeType, expected) => {
27+
expect(filePreviewKind({ name, mimeType }, { richDocuments: false })).toBe(expected);
28+
});

‎apps/mobile/src/lib/composerContext.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function composerDocumentAttachmentRecord(
7878
): ComposerDocumentAttachment | null {
7979
if (!record || "payload" in record || record.kind !== "file") return null;
8080
if (videoMimeType(record) !== null) return null;
81-
const kind = filePreviewKind(record);
81+
const kind = filePreviewKind(record, { richDocuments: false });
8282
if (kind === "image" || kind === "pdf" || kind === "video") return null;
8383
return record;
8484
}

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,49 @@ describe("AssetAccess", () => {
915915
kind: "file",
916916
path: canonicalCssPath,
917917
});
918+
for (const extension of ["otf", "ttf", "woff", "woff2"]) {
919+
const fontName = `preview.${extension}`;
920+
const fontPath = path.join(root, fontName);
921+
yield* fileSystem.writeFileString(fontPath, "font bytes");
922+
const canonicalFontPath = yield* fileSystem.realPath(fontPath);
923+
for (const resource of [
924+
{ _tag: "workspace-file" as const, threadId: ThreadId.make("thread-1"), path: htmlPath },
925+
{ _tag: "draft-workspace-file" as const, cwd: root, path: "report.html" },
926+
]) {
927+
const url = yield* issueAssetUrl({ resource, workspaceRoot: root });
928+
const suffix = url.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
929+
expect(yield* resolveAsset(suffix.slice(0, suffix.indexOf("/")), fontName)).toEqual({
930+
kind: "file",
931+
path: canonicalFontPath,
932+
});
933+
}
934+
}
918935
expect(yield* resolveAsset(token, "../secret.txt")).toBeNull();
919936
expect(yield* resolveAsset(token, ".env")).toBeNull();
920937
expect(yield* resolveAsset(`${token}tampered`, "report.html")).toBeNull();
938+
for (const extension of [
939+
"docx",
940+
"xlsx",
941+
"pptx",
942+
"bmp",
943+
"heic",
944+
"tif",
945+
"tiff",
946+
"weba",
947+
"mmd",
948+
"mermaid",
949+
]) {
950+
const siblingName = `private.${extension}`;
951+
yield* fileSystem.writeFileString(path.join(root, siblingName), "private sibling");
952+
for (const resource of [
953+
{ _tag: "workspace-file" as const, threadId: ThreadId.make("thread-1"), path: htmlPath },
954+
{ _tag: "draft-workspace-file" as const, cwd: root, path: "report.html" },
955+
]) {
956+
const url = yield* issueAssetUrl({ resource, workspaceRoot: root });
957+
const suffix = url.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
958+
expect(yield* resolveAsset(suffix.slice(0, suffix.indexOf("/")), siblingName)).toBeNull();
959+
}
960+
}
921961
}).pipe(Effect.provide(layerTest)),
922962
);
923963

@@ -955,6 +995,58 @@ describe("AssetAccess", () => {
955995
}).pipe(Effect.provide(layerTest)),
956996
);
957997

998+
it.effect("issues exact workspace URLs for image, office, and font previews", () =>
999+
Effect.gen(function* () {
1000+
const fileSystem = yield* FileSystem.FileSystem;
1001+
const path = yield* Path.Path;
1002+
const root = yield* fileSystem.makeTempDirectoryScoped({
1003+
prefix: "t3-asset-image-workspace-",
1004+
});
1005+
const assetsDirectory = path.join(root, "assets");
1006+
yield* fileSystem.makeDirectory(assetsDirectory, { recursive: true });
1007+
yield* fileSystem.writeFileString(
1008+
path.join(assetsDirectory, "private.docx"),
1009+
"private document",
1010+
);
1011+
for (const extension of [
1012+
"png",
1013+
"bmp",
1014+
"heic",
1015+
"tif",
1016+
"tiff",
1017+
"docx",
1018+
"xlsx",
1019+
"pptx",
1020+
"ttf",
1021+
"otf",
1022+
"woff",
1023+
"woff2",
1024+
]) {
1025+
const name = `preview.${extension}`;
1026+
const filePath = path.join(assetsDirectory, name);
1027+
const siblingName = `other.${extension}`;
1028+
yield* fileSystem.writeFileString(filePath, "preview");
1029+
yield* fileSystem.writeFileString(
1030+
path.join(assetsDirectory, siblingName),
1031+
"private sibling",
1032+
);
1033+
const canonicalPath = yield* fileSystem.realPath(filePath);
1034+
for (const resource of [
1035+
{ _tag: "workspace-file" as const, threadId: ThreadId.make("thread-1"), path: filePath },
1036+
{ _tag: "draft-workspace-file" as const, cwd: root, path: `assets/${name}` },
1037+
]) {
1038+
const result = yield* issueAssetUrl({ resource, workspaceRoot: root });
1039+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
1040+
const token = suffix.slice(0, suffix.indexOf("/"));
1041+
expect(yield* resolveAsset(token, name)).toEqual({ kind: "file", path: canonicalPath });
1042+
expect(yield* resolveAsset(token, siblingName)).toBeNull();
1043+
expect(yield* resolveAsset(token, "private.docx")).toBeNull();
1044+
expect(yield* resolveAsset(token, `../${name}`)).toBeNull();
1045+
}
1046+
}
1047+
}).pipe(Effect.provide(layerTest)),
1048+
);
1049+
9581050
it.effect("previews workspace files with literal fragment characters in their paths", () =>
9591051
Effect.gen(function* () {
9601052
const fileSystem = yield* FileSystem.FileSystem;
@@ -963,7 +1055,21 @@ describe("AssetAccess", () => {
9631055
const directory = path.join(root, "assets#archive");
9641056
yield* fileSystem.makeDirectory(directory);
9651057

966-
for (const name of ["icon#v2.png", "report#draft.pdf"]) {
1058+
for (const name of [
1059+
"icon#v2.png",
1060+
"report#draft.pdf",
1061+
"report.docx",
1062+
"workbook.xlsx",
1063+
"slides.pptx",
1064+
"specimen.ttf",
1065+
"specimen.otf",
1066+
"specimen.woff",
1067+
"specimen.woff2",
1068+
"image.bmp",
1069+
"image.heic",
1070+
"image.tif",
1071+
"image.tiff",
1072+
]) {
9671073
const filePath = path.join(directory, name);
9681074
yield* fileSystem.writeFileString(filePath, "preview fixture");
9691075
const canonicalFile = yield* fileSystem.realPath(filePath);
@@ -1049,6 +1155,12 @@ describe("AssetAccess", () => {
10491155
for (const [extension, mimeType] of [
10501156
["pdf", "application/pdf"],
10511157
["svg", "image/svg+xml"],
1158+
["bmp", "image/bmp"],
1159+
["ttf", "font/ttf"],
1160+
["otf", "font/otf"],
1161+
["woff", "font/woff"],
1162+
["woff2", "font/woff2"],
1163+
["weba", "audio/webm"],
10521164
] as const) {
10531165
const attachmentId = `thread-1-00000000-0000-4000-8000-000000000001-${extension}`;
10541166
const attachmentPath = path.join(config.attachmentsDir, `${attachmentId}.${extension}`);
@@ -1059,7 +1171,7 @@ describe("AssetAccess", () => {
10591171
_tag: "attachment",
10601172
attachmentId,
10611173
fileName: `report.${extension}`,
1062-
mimeType,
1174+
mimeType: "application/octet-stream",
10631175
disposition: "inline",
10641176
},
10651177
});

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import {
1919
TurnItemId,
2020
} from "@t3tools/contracts";
2121
import {
22-
audioMimeTypeFromExtension,
2322
hostPreviewMimeTypeFromExtension,
23+
officePreviewKind,
2424
isWorkspaceAudioPreviewPath,
2525
isWorkspaceImagePreviewPath,
26+
isWorkspaceFontPreviewPath,
2627
isWorkspacePreviewEntryPath,
2728
isWorkspaceVideoPreviewPath,
2829
WORKSPACE_BROWSER_PREVIEW_EXTENSIONS,
29-
WORKSPACE_IMAGE_PREVIEW_EXTENSIONS,
3030
} from "@t3tools/shared/filePreview";
3131
import {
3232
IMAGE_DIMENSIONS_HEADER_BYTES,
@@ -88,10 +88,20 @@ const INLINE_PREVIEW_MIME_TYPES: Record<string, string> = {
8888
htm: "text/html",
8989
};
9090
const inlinePreviewMimeTypeForExtension = (extension: string) =>
91-
INLINE_PREVIEW_MIME_TYPES[extension] ?? audioMimeTypeFromExtension(`.${extension}`) ?? undefined;
91+
INLINE_PREVIEW_MIME_TYPES[extension] ??
92+
hostPreviewMimeTypeFromExtension(`.${extension}`) ??
93+
undefined;
9294
const PREVIEW_ASSET_EXTENSIONS = new Set([
9395
...WORKSPACE_BROWSER_PREVIEW_EXTENSIONS,
94-
...WORKSPACE_IMAGE_PREVIEW_EXTENSIONS,
96+
// Keep HTML dependency grants independent of newly supported file viewers.
97+
".avif",
98+
".gif",
99+
".ico",
100+
".jpeg",
101+
".jpg",
102+
".png",
103+
".svg",
104+
".webp",
95105
".css",
96106
".js",
97107
".mjs",
@@ -507,21 +517,24 @@ const finalizeWorkspaceFileAsset = Effect.fn("AssetAccess.finalizeWorkspaceFileA
507517
? yield* readImageDimensionsFromHeader(canonicalFile)
508518
: null;
509519
return {
510-
claims: isWorkspaceImagePreviewPath(resolved.relativePath)
511-
? {
512-
version: 1 as const,
513-
kind: "workspace-file-exact" as const,
514-
workspaceRoot: canonicalWorkspaceRoot,
515-
relativePath: resolved.relativePath,
516-
expiresAt: input.expiresAt,
517-
}
518-
: {
519-
version: 1 as const,
520-
kind: "workspace-file" as const,
521-
workspaceRoot: canonicalWorkspaceRoot,
522-
baseRelativePath: path.dirname(resolved.relativePath),
523-
expiresAt: input.expiresAt,
524-
},
520+
claims:
521+
isWorkspaceImagePreviewPath(resolved.relativePath) ||
522+
isWorkspaceFontPreviewPath(resolved.relativePath) ||
523+
officePreviewKind({ name: resolved.relativePath }) !== null
524+
? {
525+
version: 1 as const,
526+
kind: "workspace-file-exact" as const,
527+
workspaceRoot: canonicalWorkspaceRoot,
528+
relativePath: resolved.relativePath,
529+
expiresAt: input.expiresAt,
530+
}
531+
: {
532+
version: 1 as const,
533+
kind: "workspace-file" as const,
534+
workspaceRoot: canonicalWorkspaceRoot,
535+
baseRelativePath: path.dirname(resolved.relativePath),
536+
expiresAt: input.expiresAt,
537+
},
525538
fileName: path.basename(resolved.relativePath),
526539
imageDimensions,
527540
fileRevision: undefined,

0 commit comments

Comments
 (0)