|
| 1 | +import type { Attachment } from "@reprojs/sdk-utils" |
| 2 | +import type { DocumentPickerResult } from "expo-document-picker" |
| 3 | + |
| 4 | +type GetDocumentAsync = (opts?: { |
| 5 | + multiple?: boolean |
| 6 | + copyToCacheDirectory?: boolean |
| 7 | +}) => Promise<DocumentPickerResult> |
| 8 | + |
| 9 | +/** |
| 10 | + * Wraps expo-document-picker.getDocumentAsync. Returns an empty array on |
| 11 | + * cancel or when the picker module is unavailable. Each asset is converted |
| 12 | + * to an Attachment — the blob field is a placeholder Blob; the intake-client |
| 13 | + * uses the previewUrl (file:// uri) at submit time so we don't read every |
| 14 | + * file into memory the moment it's picked. |
| 15 | + */ |
| 16 | +export async function pickFiles({ multiple = true }: { multiple?: boolean } = {}): Promise< |
| 17 | + Attachment[] |
| 18 | +> { |
| 19 | + let getDocumentAsync: GetDocumentAsync | undefined |
| 20 | + try { |
| 21 | + const mod = await import("expo-document-picker") |
| 22 | + getDocumentAsync = mod.getDocumentAsync |
| 23 | + } catch { |
| 24 | + return [] |
| 25 | + } |
| 26 | + if (!getDocumentAsync) return [] |
| 27 | + |
| 28 | + const result = await getDocumentAsync({ |
| 29 | + multiple, |
| 30 | + copyToCacheDirectory: true, |
| 31 | + }) |
| 32 | + |
| 33 | + if (result.canceled || !result.assets) return [] |
| 34 | + |
| 35 | + return result.assets.map((asset, i) => { |
| 36 | + const mime = asset.mimeType ?? "application/octet-stream" |
| 37 | + return { |
| 38 | + id: `picker-${Date.now()}-${i}`, |
| 39 | + blob: new Blob([], { type: mime }), // Placeholder — intake-client uses uri at submit time. |
| 40 | + filename: asset.name, |
| 41 | + mime, |
| 42 | + size: asset.size ?? 0, |
| 43 | + isImage: mime.startsWith("image/"), |
| 44 | + previewUrl: asset.uri, |
| 45 | + } satisfies Attachment |
| 46 | + }) |
| 47 | +} |
0 commit comments