feat(mobile): view image files from the Files tree (#5472)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -223,6 +223,41 @@ describe('RuntimeFileCommands', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('opens previewable images through the renderer host as an image tab', async () => {
|
||||
const openFile = vi.fn()
|
||||
const { commands } = createRuntimeFileCommands({ openFile })
|
||||
|
||||
const result = await commands.openMobileFile('id:wt-1', 'assets/logo.png')
|
||||
|
||||
expect(openFile).toHaveBeenCalledWith(
|
||||
'wt-1',
|
||||
'/repo/assets/logo.png',
|
||||
'assets/logo.png',
|
||||
undefined
|
||||
)
|
||||
expect(result).toEqual({
|
||||
worktree: 'wt-1',
|
||||
relativePath: 'assets/logo.png',
|
||||
kind: 'image',
|
||||
opened: true
|
||||
})
|
||||
})
|
||||
|
||||
it('leaves non-previewable binaries unavailable on mobile', async () => {
|
||||
const openFile = vi.fn()
|
||||
const { commands } = createRuntimeFileCommands({ openFile })
|
||||
|
||||
const result = await commands.openMobileFile('id:wt-1', 'dist/bundle.zip')
|
||||
|
||||
expect(openFile).not.toHaveBeenCalled()
|
||||
expect(result).toEqual({
|
||||
worktree: 'wt-1',
|
||||
relativePath: 'dist/bundle.zip',
|
||||
kind: 'binary',
|
||||
opened: false
|
||||
})
|
||||
})
|
||||
|
||||
it('does not follow symlinks when reading runtime-local file explorer dirs', async () => {
|
||||
const { commands } = createRuntimeFileCommands()
|
||||
resolveAuthorizedPathMock.mockResolvedValue('/repo')
|
||||
|
||||
@@ -87,6 +87,28 @@ const MOBILE_BINARY_EXTENSIONS = new Set([
|
||||
'.webp',
|
||||
'.zip'
|
||||
])
|
||||
// Raster image extensions the mobile client can render from a base64 data URI
|
||||
// via files.readPreview. Mirrors mobile's classifyMobileArtifact image set;
|
||||
// SVG/PDF are intentionally excluded (RN <Image> can't decode those data URIs).
|
||||
const MOBILE_PREVIEWABLE_IMAGE_EXTENSIONS = new Set([
|
||||
'.png',
|
||||
'.jpg',
|
||||
'.jpeg',
|
||||
'.gif',
|
||||
'.webp',
|
||||
'.bmp',
|
||||
'.ico'
|
||||
])
|
||||
|
||||
function isMobilePreviewableImagePath(relativePath: string): boolean {
|
||||
const basename = basenameFromRelativePath(relativePath)
|
||||
const dotIndex = basename.lastIndexOf('.')
|
||||
if (dotIndex <= 0) {
|
||||
return false
|
||||
}
|
||||
return MOBILE_PREVIEWABLE_IMAGE_EXTENSIONS.has(basename.slice(dotIndex).toLowerCase())
|
||||
}
|
||||
|
||||
const RUNTIME_PREVIEWABLE_BINARY_MIME_TYPES: Record<string, string> = {
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
@@ -182,11 +204,15 @@ export class RuntimeFileCommands {
|
||||
if (!isSafeMobileRelativePath(relativePath)) {
|
||||
throw new Error('invalid_relative_path')
|
||||
}
|
||||
const kind = isMobileBinaryPath(relativePath)
|
||||
? 'binary'
|
||||
: isMobileMarkdownPath(relativePath)
|
||||
? 'markdown'
|
||||
: 'text'
|
||||
// Previewable images open like text (the mobile viewer renders them via
|
||||
// files.readPreview); other binaries stay unavailable on mobile.
|
||||
const kind = isMobilePreviewableImagePath(relativePath)
|
||||
? 'image'
|
||||
: isMobileBinaryPath(relativePath)
|
||||
? 'binary'
|
||||
: isMobileMarkdownPath(relativePath)
|
||||
? 'markdown'
|
||||
: 'text'
|
||||
if (kind === 'binary') {
|
||||
return { worktree: worktree.id, relativePath, kind, opened: false }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user