Skip to content

Commit f008404

Browse files
committed
fix(shared): classify workspace previews by literal filenames
1 parent 0c200c5 commit f008404

4 files changed

Lines changed: 65 additions & 10 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,49 @@ describe("AssetAccess", () => {
573573
}).pipe(Effect.provide(testLayer)),
574574
);
575575

576+
it.effect("previews workspace files with literal fragment characters in their paths", () =>
577+
Effect.gen(function* () {
578+
const fileSystem = yield* FileSystem.FileSystem;
579+
const path = yield* Path.Path;
580+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-preview-literal-" });
581+
const directory = path.join(root, "assets#archive");
582+
yield* fileSystem.makeDirectory(directory);
583+
584+
for (const name of ["icon#v2.png", "report#draft.pdf"]) {
585+
const filePath = path.join(directory, name);
586+
yield* fileSystem.writeFileString(filePath, "preview fixture");
587+
const canonicalFile = yield* fileSystem.realPath(filePath);
588+
const result = yield* issueAssetUrl({
589+
resource: {
590+
_tag: "workspace-file",
591+
threadId: ThreadId.make("thread-1"),
592+
path: filePath,
593+
},
594+
workspaceRoot: root,
595+
});
596+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
597+
const token = suffix.slice(0, suffix.indexOf("/"));
598+
599+
expect(yield* resolveAsset(token, name)).toEqual({ kind: "file", path: canonicalFile });
600+
if (name.endsWith(".png")) {
601+
expect(yield* resolveAsset(token, "other.png")).toBeNull();
602+
}
603+
}
604+
605+
const disguisedPath = path.join(root, "image.png#notes.txt");
606+
yield* fileSystem.writeFileString(disguisedPath, "not an image");
607+
const error = yield* issueAssetUrl({
608+
resource: {
609+
_tag: "workspace-file",
610+
threadId: ThreadId.make("thread-1"),
611+
path: disguisedPath,
612+
},
613+
workspaceRoot: root,
614+
}).pipe(Effect.flip);
615+
expect(error).toBeInstanceOf(AssetPreviewTypeValidationError);
616+
}).pipe(Effect.provide(testLayer)),
617+
);
618+
576619
it.effect("issues exact attachment capabilities by attachment id", () =>
577620
Effect.gen(function* () {
578621
const config = yield* ServerConfig.ServerConfig;

‎docs/user/composer.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,8 @@ On web and desktop, HTML and PDF files open as rendered pages. Switch an HTML
134134
file to source view to read its markup; a link to a specific line opens source
135135
automatically. HTML previews cannot access your T3 Code session.
136136

137+
The file viewer recognizes images, HTML, and PDF files by their filename extension,
138+
including filenames or folders containing `#` or `?`.
139+
137140
On mobile, select a PDF attachment or link to open it. iOS uses the native viewer;
138141
Android opens the system chooser.

‎packages/shared/src/filePreview.test.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "./filePreview.ts";
1010

1111
describe("workspace file previews", () => {
12-
it.each(["report.html", "report.HTM", "document.pdf?download=1"])(
12+
it.each(["report.html", "report.HTM", "document#draft.pdf", "reports?old/document.pdf"])(
1313
"recognizes browser preview path %s",
1414
(path) => {
1515
expect(isWorkspaceBrowserPreviewPath(path)).toBe(true);
@@ -21,20 +21,29 @@ describe("workspace file previews", () => {
2121
"icon.png",
2222
"photo.JPEG",
2323
"animation.gif",
24-
"vector.svg#mark",
24+
"vector#mark.svg",
25+
"photo?edited.JPEG",
26+
"images#archive/icon.png",
2527
"texture.webp",
2628
"image.avif",
2729
])("recognizes image preview path %s", (path) => {
2830
expect(isWorkspaceImagePreviewPath(path)).toBe(true);
2931
expect(isWorkspacePreviewEntryPath(path)).toBe(true);
3032
});
3133

32-
it.each(["README.md", "src/index.ts", "image.png.ts", "png"])(
33-
"rejects non-preview path %s",
34-
(path) => {
35-
expect(isWorkspacePreviewEntryPath(path)).toBe(false);
36-
},
37-
);
34+
it.each([
35+
"README.md",
36+
"src/index.ts",
37+
"image.png.ts",
38+
"png",
39+
"image.png#notes.txt",
40+
"image.svg?notes.txt",
41+
"document.pdf?download=1",
42+
"report.html#notes.txt",
43+
"image%2Epng",
44+
])("rejects non-preview path %s", (path) => {
45+
expect(isWorkspacePreviewEntryPath(path)).toBe(false);
46+
});
3847
});
3948

4049
describe("media path parsing", () => {

‎packages/shared/src/filePreview.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ export function mediaKindFromPath(path: string): "image" | "video" | null {
8181
}
8282

8383
function hasPreviewExtension(path: string, extensions: ReadonlyArray<string>): boolean {
84-
const pathWithoutQuery = path.split(/[?#]/, 1)[0]?.toLowerCase() ?? "";
85-
return extensions.some((extension) => pathWithoutQuery.endsWith(extension));
84+
const literalPath = path.toLowerCase();
85+
return extensions.some((extension) => literalPath.endsWith(extension));
8686
}
8787

8888
export function isWorkspaceBrowserPreviewPath(path: string): boolean {

0 commit comments

Comments
 (0)