Stream file to Readable with no temp files
npm install expo-file-streamexpo-file-stream is a small library for creating read streams from files on mobile.
* Supports multiple files in parallel.
* Enables content:// URI on Android to be streamed without copy/open.
* Compatible with Bare runtime
* Powered by streamx
``bash`
npm i expo-file-stream
Below is a minimal example that mirrors the one in the repository’s example/App.tsx.
`tsx /example/App.tsx#L1-70
import { useCallback } from 'react'
import { Button, View } from 'react-native'
import { getDocumentAsync } from 'expo-document-picker'
import getMimeType from 'get-mime-type'
import { streamFile } from 'file-stream'
export default function App() {
const onUpload = useCallback(async () => {
const result = await getDocumentAsync({
multiple: true,
copyToCacheDirectory: false
})
if (result.canceled || !result.assets) return
const assets = await Promise.all(
result.assets.map(async ({ name, size, mimeType, uri }) => {
return {
name,
byteLength: size ?? 0,
type: mimeType || getMimeType(uri),
uri,
isOutsideAppCache: true
}
})
)
const target = assets[0].uri
const stream = streamFile(target)
stream.on('data', chunk => {
console.log(chunk)
})
stream.on('end', () => {
console.log('Upload complete')
})
}, [])
return (
)
}
`
For use with Bare the library is designed to be easily streamed in to P2P friendly storage.
`js
const Hyperblobs = require('hyperblobs')
const Corestore = require('corestore')
const store = new Corestore("./storage")
const core = store.get({ name:"my-blobs" })
const blobs = new Hyperblobs(core)
await blobs.ready()
const readStream = streamFile('content://com.android.providers.media.documents/document/image%3A62')
const writeStream = blobs.createWriteStream()
const pipeStream = readStream.pipe(writeStream)
pipeStream.on('finish',async () => {
const blob = await blobs.get(writeStream.id)
console.log(blob)
})
`
- streamFile(uri: string): ReadableStream – Returns a streamx Readable that emits data chunks and an end event when the file has been fully read.
`ts`
const stream = streamFile('content://com.android.providers.media.documents/document/image%3A62')
stream.on('data', chunk => console.log(chunk))
stream.on('end', () => console.log('done'))
- When picking files from the document picker, set copyToCacheDirectory: false to obtain a raw content://` URI.
- The returned stream can be piped to any writable destination