Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions apps/server/src/assets/AssetAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform";
import * as NodeFSP from "node:fs/promises";
import * as NodeOS from "node:os";
import { AssetAccessError, AssetPreviewTypeValidationError, ThreadId } from "@t3tools/contracts";
import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon";
import { describe, expect, it } from "@effect/vitest";
Expand Down Expand Up @@ -35,6 +36,11 @@ vi.mock("node:fs/promises", async (importOriginal) => {
return { ...actual, open: vi.fn(actual.open), realpath: vi.fn(actual.realpath) };
});

vi.mock("node:os", async (importOriginal) => {
const actual = await importOriginal<typeof NodeOS>();
return { ...actual, homedir: vi.fn(actual.homedir) };
});

const configLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
prefix: "t3-asset-access-test-",
});
Expand Down Expand Up @@ -183,6 +189,55 @@ describe("AssetAccess", () => {
}).pipe(Effect.provide(testLayer)),
);

it.effect("resolves home-relative media paths independently of the workspace", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-media-home-" });
const home = path.join(directory, "var", "home", "alice");
const filePath = path.join(home, "Downloads", "repro.mp4");
yield* fs.makeDirectory(path.dirname(filePath), { recursive: true });
yield* fs.writeFileString(filePath, "recording bytes");
const canonicalFile = yield* fs.realPath(filePath);
const homeSpy = vi.mocked(NodeOS.homedir).mockReturnValue(home);
try {
for (const workspaceRoot of [
path.join(home, "project"),
path.join(directory, "srv", "project"),
undefined,
]) {
if (workspaceRoot) yield* fs.makeDirectory(workspaceRoot, { recursive: true });
for (const requestedPath of ["~/Downloads/repro.mp4", "~\\Downloads/repro.mp4"]) {
const result = yield* issueAssetUrl({
resource: {
_tag: "media-file",
threadId: ThreadId.make("thread-1"),
path: requestedPath,
},
...(workspaceRoot ? { workspaceRoot } : {}),
});
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
const separator = suffix.indexOf("/");
const asset = yield* resolveAsset(
suffix.slice(0, separator),
suffix.slice(separator + 1),
);
expect(asset).toMatchObject({
kind: "file",
path: canonicalFile,
mimeType: "video/mp4",
});
if (asset?.kind !== "file") throw new Error("Expected a resolved home media file");
const response = HttpServerResponse.toWeb(yield* assetFileResponse(asset));
expect(yield* Effect.promise(() => response.text())).toBe("recording bytes");
}
}
} finally {
homeSpy.mockRestore();
}
}).pipe(Effect.provide(testLayer)),
);

it.effect.skipIf(!symlinksSupported)(
"rejects non-previewable files, disguised targets, and directories",
() =>
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/assets/AssetAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts";
import * as ServerConfig from "../config.ts";
import { expandHomePathWith } from "../pathExpansion.ts";
import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
Expand Down Expand Up @@ -433,7 +434,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i

switch (input.resource._tag) {
case "media-file": {
let requestedPath = input.resource.path;
let requestedPath = expandHomePathWith(input.resource.path, path);
if (!path.isAbsolute(requestedPath)) {
if (!input.workspaceRoot) {
return yield* new AssetWorkspaceContextNotFoundError({ resource: input.resource });
Expand Down
Loading