Files
archived-orca/mobile/src/platform/native-media-device.ts
Kelvin Amoaba c884428609 feat(mobile): copy, save and share an image from the full-screen image viewer (#38303)
* feat(mobile): copy, save and share an image from the chat image viewer

* refactor(mobile): pass image viewer actions in so diagrams skip them

* fix(mobile): name the image verb native.image.act and serialize image actions

* fix(mobile): queue image actions per page and skip SVG links

* refactor(mobile): share the cache-folder helper and table the viewer's image actions

* fix(mobile): keep the toast's live region mounted so screen readers hear it

* test(mobile): re-record agent history goldens for the always-mounted toast region

* test(mobile): import the declared routes in the moved routes test
2026-11-05 22:44:36 +00:00

92 lines
3.6 KiB
TypeScript

import * as Clipboard from 'expo-clipboard'
import * as DocumentPicker from 'expo-document-picker'
import { Directory, File as FsFile, Paths } from 'expo-file-system'
import * as ImagePicker from 'expo-image-picker'
import type { MediaHandleRegistry } from '../mobile-web-shell/media-handle-registry'
import type { NativeMediaDeps } from './native-media'
// Isolate native imports so handlers can be tested with injected device calls.
// Android may return an uncopied content: URI; copy it into cache before minting a handle whose release deletes the file.
export function ownsStagedMediaUri(uri: string): boolean {
return uri.startsWith('file:')
}
function stagedMediaFile(extension: string): FsFile {
return new FsFile(Paths.cache, `orca-media-${Date.now()}-${Math.random()}.${extension}`)
}
// Another app may still read the last file handed to it, so a folder's earlier contents are cleared only when the next one starts.
// Each start gets its own subfolder so an aborted run's cleanup cannot delete its replacement.
export function freshCacheFolder(name: string): Directory {
const root = new Directory(Paths.cache, name)
if (root.exists) {
root.delete()
}
const folder = new Directory(root, `${Date.now()}-${Math.random().toString(36).slice(2)}`)
folder.create({ intermediates: true })
return folder
}
export function discardUnreturnedMediaFile(file: FsFile): void {
try {
file.delete()
} catch {
// Best effort; the cache is the OS's to reclaim.
}
}
// Use bytesSync(): Expo copy/open/stream paths require javaFile and reject content: URIs. The caller must weigh first because this read holds the whole file in memory.
export function copyPickedMediaIntoCache(uri: string): string {
const destination = stagedMediaFile('bin')
destination.create({ overwrite: true })
try {
destination.write(new FsFile(uri).bytesSync())
} catch (error) {
// Remove the failed destination because callers never receive its URI.
discardUnreturnedMediaFile(destination)
throw error
}
return destination.uri
}
export function discardStagedMedia(uri: string): void {
new FsFile(uri).delete()
}
export function nativeMediaDeviceDeps(registry: MediaHandleRegistry): NativeMediaDeps {
return {
registry,
// Use remaining capacity, never zero (unlimited), to bound copies before registry admission.
launchLibrary: ({ multiple, limit }) =>
ImagePicker.launchImageLibraryAsync({
mediaTypes: ['images'],
base64: false,
allowsMultipleSelection: multiple,
...(multiple ? { selectionLimit: limit, orderedSelection: true } : {}),
quality: 1,
// iOS otherwise hands back HEIC as-is, which agents can't read; Compatible gives JPEG.
preferredAssetRepresentationMode:
ImagePicker.UIImagePickerPreferredAssetRepresentationMode.Compatible
}),
// DocumentPicker has no selection cap; enforce remaining room again before staging and minting.
launchFiles: ({ multiple }) =>
DocumentPicker.getDocumentAsync({ type: '*/*', multiple, copyToCacheDirectory: true }),
readClipboardImage: () => Clipboard.getImageAsync({ format: 'png' }),
stageBase64: (base64) => {
const file = stagedMediaFile('png')
file.create({ overwrite: true })
try {
file.write(base64, { encoding: 'base64' })
} catch (error) {
discardUnreturnedMediaFile(file)
throw error
}
return file.uri
},
openFile: (uri) => new FsFile(uri),
ownsStagedUri: ownsStagedMediaUri,
copyIntoCache: copyPickedMediaIntoCache,
discard: discardStagedMedia
}
}