Skip to content

Commit 63ce85f

Browse files
committed
fix(server): read a workspace file's size and revision from one open
The workspace preview path opened the file twice, once for image dimensions and once for the revision, so a file replaced in between could report the two from different versions. Read both from the same handle, as the absolute media path already does.
1 parent 1f44276 commit 63ce85f

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,22 @@ describe("AssetAccess", () => {
627627
}).pipe(Effect.provide(layerTest)),
628628
);
629629

630+
it.effect("reports a workspace image's dimensions and revision together", () =>
631+
Effect.gen(function* () {
632+
const fs = yield* FileSystem.FileSystem;
633+
const path = yield* Path.Path;
634+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-workspace-image-" });
635+
const filePath = path.join(root, "screenshot.png");
636+
yield* fs.writeFile(filePath, screenshotPng);
637+
const result = yield* issueAssetUrl({
638+
resource: { _tag: "workspace-file", threadId: ThreadId.make("thread-1"), path: filePath },
639+
workspaceRoot: root,
640+
});
641+
expect(result.imageDimensions).toEqual({ width: 390, height: 844 });
642+
expect(result.fileRevision).toEqual(expect.any(String));
643+
}).pipe(Effect.provide(layerTest)),
644+
);
645+
630646
it.effect("keeps in-place edits readable but requires a new URL after atomic replacement", () =>
631647
Effect.gen(function* () {
632648
const fs = yield* FileSystem.FileSystem;

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,6 @@ const readImageDimensionsFromOpenFile = (filePath: string, file: OpenMediaFile)
340340
const fileRevisionOf = (file: OpenMediaFile) =>
341341
`${file.info.dev}:${file.info.ino}:${file.info.size}:${file.info.mtimeNs}:${file.info.ctimeNs}`;
342342

343-
/** Opens through `openMediaFile` for the same reason as `readImageDimensionsFromHeader`. */
344-
const readFileRevision = (filePath: string) =>
345-
openMediaFile(filePath).pipe(
346-
Effect.map((file) => (file === null ? undefined : fileRevisionOf(file))),
347-
Effect.scoped,
348-
Effect.orElseSucceed((): string | undefined => undefined),
349-
);
350-
351343
/**
352344
* Opens through `openMediaFile` so a path swapped for a FIFO cannot block the
353345
* request; a regular open would wait for a writer that never comes.
@@ -495,12 +487,24 @@ const finalizeWorkspaceFileAsset = Effect.fn("AssetAccess.finalizeWorkspaceFileA
495487
}),
496488
),
497489
);
498-
const imageDimensions = HEADER_IMAGE_EXTENSIONS.has(
490+
const wantsDimensions = HEADER_IMAGE_EXTENSIONS.has(
499491
path.extname(resolved.relativePath).toLowerCase(),
500-
)
501-
? yield* readImageDimensionsFromHeader(canonicalFile)
502-
: null;
503-
const fileRevision = yield* readFileRevision(canonicalFile);
492+
);
493+
// One open for both, so the dimensions and revision describe the same version of the file.
494+
const opened = yield* openMediaFile(canonicalFile).pipe(
495+
Effect.flatMap((file) =>
496+
file === null
497+
? Effect.succeed(null)
498+
: Effect.map(
499+
wantsDimensions
500+
? readImageDimensionsFromOpenFile(canonicalFile, file)
501+
: Effect.succeed(null),
502+
(dimensions) => ({ dimensions, fileRevision: fileRevisionOf(file) }),
503+
),
504+
),
505+
Effect.scoped,
506+
Effect.orElseSucceed(() => null),
507+
);
504508
return {
505509
claims: isWorkspaceImagePreviewPath(resolved.relativePath)
506510
? {
@@ -518,8 +522,8 @@ const finalizeWorkspaceFileAsset = Effect.fn("AssetAccess.finalizeWorkspaceFileA
518522
expiresAt: input.expiresAt,
519523
},
520524
fileName: path.basename(resolved.relativePath),
521-
imageDimensions,
522-
fileRevision,
525+
imageDimensions: opened?.dimensions ?? null,
526+
fileRevision: opened?.fileRevision,
523527
};
524528
},
525529
);

0 commit comments

Comments
 (0)