Skip to content

Commit 3ce5cfe

Browse files
committed
fix(server): expand home-relative media paths
1 parent 3b0093d commit 3ce5cfe

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform";
44
import * as NodeFSP from "node:fs/promises";
5+
import * as NodeOS from "node:os";
56
import { AssetAccessError, AssetPreviewTypeValidationError, ThreadId } from "@t3tools/contracts";
67
import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon";
78
import { describe, expect, it } from "@effect/vitest";
@@ -35,6 +36,11 @@ vi.mock("node:fs/promises", async (importOriginal) => {
3536
return { ...actual, open: vi.fn(actual.open), realpath: vi.fn(actual.realpath) };
3637
});
3738

39+
vi.mock("node:os", async (importOriginal) => {
40+
const actual = await importOriginal<typeof NodeOS>();
41+
return { ...actual, homedir: vi.fn(actual.homedir) };
42+
});
43+
3844
const configLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
3945
prefix: "t3-asset-access-test-",
4046
});
@@ -183,6 +189,48 @@ describe("AssetAccess", () => {
183189
}).pipe(Effect.provide(testLayer)),
184190
);
185191

192+
it.effect("resolves home-relative media paths independently of the workspace", () =>
193+
Effect.gen(function* () {
194+
const fs = yield* FileSystem.FileSystem;
195+
const path = yield* Path.Path;
196+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-media-home-" });
197+
const home = path.join(directory, "var", "home", "alice");
198+
const filePath = path.join(home, "Downloads", "repro.mp4");
199+
yield* fs.makeDirectory(path.dirname(filePath), { recursive: true });
200+
yield* fs.writeFileString(filePath, "recording bytes");
201+
const canonicalFile = yield* fs.realPath(filePath);
202+
const homeSpy = vi.mocked(NodeOS.homedir).mockReturnValue(home);
203+
try {
204+
for (const workspaceRoot of [
205+
path.join(home, "project"),
206+
path.join(directory, "srv", "project"),
207+
undefined,
208+
]) {
209+
if (workspaceRoot) yield* fs.makeDirectory(workspaceRoot, { recursive: true });
210+
for (const requestedPath of ["~/Downloads/repro.mp4", "~\\Downloads/repro.mp4"]) {
211+
const result = yield* issueAssetUrl({
212+
resource: {
213+
_tag: "media-file",
214+
threadId: ThreadId.make("thread-1"),
215+
path: requestedPath,
216+
},
217+
...(workspaceRoot ? { workspaceRoot } : {}),
218+
});
219+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
220+
const separator = suffix.indexOf("/");
221+
const asset = yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1));
222+
expect(asset).toMatchObject({ kind: "file", path: canonicalFile, mimeType: "video/mp4" });
223+
if (asset?.kind !== "file") throw new Error("Expected a resolved home media file");
224+
const response = HttpServerResponse.toWeb(yield* assetFileResponse(asset));
225+
expect(yield* Effect.promise(() => response.text())).toBe("recording bytes");
226+
}
227+
}
228+
} finally {
229+
homeSpy.mockRestore();
230+
}
231+
}).pipe(Effect.provide(testLayer)),
232+
);
233+
186234
it.effect.skipIf(!symlinksSupported)(
187235
"rejects non-previewable files, disguised targets, and directories",
188236
() =>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
5050
import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts";
5151
import * as ServerConfig from "../config.ts";
52+
import { expandHomePathWith } from "../pathExpansion.ts";
5253
import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
5354
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
5455
import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
@@ -433,7 +434,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
433434

434435
switch (input.resource._tag) {
435436
case "media-file": {
436-
let requestedPath = input.resource.path;
437+
let requestedPath = expandHomePathWith(input.resource.path, path);
437438
if (!path.isAbsolute(requestedPath)) {
438439
if (!input.workspaceRoot) {
439440
return yield* new AssetWorkspaceContextNotFoundError({ resource: input.resource });

0 commit comments

Comments
 (0)