expo-file-stream
v1.1.3
Published
Stream file to Readable with no temp files
Readme
expo-file-stream
expo-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
Install
npm i expo-file-streamUsage
Below is a minimal example that mirrors the one in the repository’s example/App.tsx.
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 (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button title="Upload" onPress={onUpload} />
</View>
)
}Hyperblobs
For use with Bare the library is designed to be easily streamed in to P2P friendly storage.
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)
})API
streamFile(uri: string): ReadableStream– Returns a streamxReadablethat emitsdatachunks and anendevent when the file has been fully read.
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'))Notes
- When picking files from the document picker, set
copyToCacheDirectory: falseto obtain a rawcontent://URI. - The returned stream can be piped to any writable destination
