Files
orca/mobile/src/files/mobile-diff-image-preview.ts
T
Jinjing 115b87efe4 Fix stale binary image fallback for empty modified diffs and relay reads
- mobileDiffImageDataUri now distinguishes a true deletion (modified
  side absent) from a modify whose binary bytes arrived empty
  (relay/size-cap cases), returning null instead of the stale
  pre-change image
- readWorkingDiffFile passes the file path to bufferToBlob so relay
  working-tree reads can detect previewable image extensions instead
  of always reporting empty binary content
- add mobile-file-tab-doc.test.ts covering diff/image/binary/text
  resolution paths
2026-07-16 16:46:59 -07:00

37 lines
1.5 KiB
TypeScript

import { buildImageDataUri } from '../../../src/shared/image-data-uri'
// A git.diff result for a binary file. The host (git.diff) already sends base64
// image bytes plus isImage/mimeType for previewable formats, so mobile can render
// image diffs instead of falling back to "Binary preview unavailable". The
// originalIsBinary/modifiedIsBinary flags tell a deletion (modified side absent)
// apart from a modify whose bytes the host couldn't inline (relay/size-capped).
export type MobileBinaryDiffResult = {
kind: 'binary'
originalContent?: string
modifiedContent?: string
originalIsBinary?: boolean
modifiedIsBinary?: boolean
isImage?: boolean
mimeType?: string
}
// Builds a data URI for a binary image diff, preferring the modified (post-change)
// bytes. Only falls back to the original for a genuine deletion — when the modified
// side is absent (not binary). Returns null for non-previewable binaries and when
// the modified side IS a binary image but arrives empty (e.g. a relay working-tree
// read or a >size-cap file), since showing the pre-change image there would be
// silently stale rather than the current content.
export function mobileDiffImageDataUri(result: MobileBinaryDiffResult): string | null {
if (result.isImage !== true) {
return null
}
const modified = result.modifiedContent || ''
if (modified) {
return buildImageDataUri(result.mimeType, modified)
}
if (result.modifiedIsBinary === true) {
return null
}
return buildImageDataUri(result.mimeType, result.originalContent || '')
}