@@ -10,7 +10,6 @@ import { createLogger } from '@sim/logger'
1010import { getErrorMessage , getPostgresConstraintName , getPostgresErrorCode } from '@sim/utils/errors'
1111import { generateShortId } from '@sim/utils/id'
1212import { and , eq , isNotNull , isNull , or , sql } from 'drizzle-orm'
13- import { imageSize } from 'image-size'
1413import type { ShareRecord } from '@/lib/api/contracts/public-shares'
1514import {
1615 decrementStorageUsageForBillingContextInTx ,
@@ -863,11 +862,12 @@ async function mapSingleWorkspaceFileRecord(
863862}
864863
865864/**
866- * Backfill an image file's intrinsic pixel dimensions (a pure rendering hint used to reserve layout
867- * space before the image loads). Idempotent by construction: the `width IS NULL` guard makes it a no-op
868- * once populated, so concurrent first-view reporters converge on a single write with no churn. Does NOT
869- * touch `updatedAt` — dimensions are not content and must not cache-bust the served image bytes. Returns
870- * whether a row was actually written (false when already populated, deleted, or absent).
865+ * Store an image file's intrinsic pixel dimensions (a pure rendering hint used to reserve layout space
866+ * before the image loads). The client reports the browser's own EXIF-corrected `naturalWidth/Height`, and
867+ * only when it differs from what's stored, so this overwrites rather than backfilling once — a stale
868+ * value (e.g. left over after the file's content was replaced) self-corrects on the next view instead of
869+ * sticking behind a `width IS NULL` guard. Does NOT touch `updatedAt` — dimensions are not content and
870+ * must not cache-bust the served image bytes. Returns whether a live row was written.
871871 */
872872export async function updateWorkspaceFileDimensions (
873873 workspaceId : string ,
@@ -881,31 +881,13 @@ export async function updateWorkspaceFileDimensions(
881881 and (
882882 eq ( workspaceFiles . id , fileId ) ,
883883 eq ( workspaceFiles . workspaceId , workspaceId ) ,
884- isNull ( workspaceFiles . deletedAt ) ,
885- isNull ( workspaceFiles . width )
884+ isNull ( workspaceFiles . deletedAt )
886885 )
887886 )
888887 . returning ( { id : workspaceFiles . id } )
889888 return updated . length > 0
890889}
891890
892- /**
893- * Best-effort intrinsic dimensions of an image buffer (reads headers only, no full decode). Returns null
894- * for non-images and unparseable bytes — callers then store null and let the lazy client backfill fill it.
895- */
896- function measureImageDimensions (
897- content : Buffer ,
898- contentType : string
899- ) : { width : number ; height : number } | null {
900- if ( ! contentType . startsWith ( 'image/' ) ) return null
901- try {
902- const { width, height } = imageSize ( content )
903- return width && height ? { width, height } : null
904- } catch {
905- return null
906- }
907- }
908-
909891/**
910892 * Look up a single active workspace file by its original name.
911893 * Returns the record if found, or null if no matching file exists.
@@ -1252,10 +1234,6 @@ export async function updateWorkspaceFileContent(
12521234 const storageBillingContext = await resolveStorageBillingContext ( workspaceId )
12531235 const nextContentType = contentType || fileRecord . type
12541236 const nextStorageKey = generateWorkspaceFileKey ( workspaceId , fileRecord . name )
1255- // Re-derive intrinsic dimensions from the NEW bytes so the row never carries the previous image's size
1256- // — and so a late fire-and-forget backfill PATCH for the OLD image (guarded on `width IS NULL`) can't
1257- // resurrect stale dimensions, since these stay non-null for an image.
1258- const nextDimensions = measureImageDimensions ( content , nextContentType )
12591237
12601238 try {
12611239 const metadata : Record < string , string > = {
@@ -1334,10 +1312,6 @@ export async function updateWorkspaceFileContent(
13341312 key : uploadResult . key ,
13351313 size : content . length ,
13361314 contentType : nextContentType ,
1337- // Replaced content gets its OWN intrinsic dimensions (or null for a non-image), so the editor
1338- // never reserves the previous image's aspect ratio and a late stale backfill can't apply.
1339- width : nextDimensions ?. width ?? null ,
1340- height : nextDimensions ?. height ?? null ,
13411315 updatedAt : now ,
13421316 contentUpdatedAt,
13431317 } )
0 commit comments