The Web File implementation for Web3.
The Web File implementation and standard are still on their beginning.
The current implementation has a non standard File.webkitRelativePath read-only
property that we cannot use outside the webkitdirectory
context. As a result, we cannot set a File path programatically in the Browser, nor in Node.js @web-std/file.
Web3File
bridges this gap by adding a path property and other extras, while being a close implementation of Web File. This allows
files to be transformed into their UnixFs representation and back into the original structure, and leverage the IPFS Content Routing primitives.
# install it as a dependency
$ npm i web3-file
To create your Web3File
with an async iterable content, you can simply create an instance of Web3File. Otherwise, you can leverage one of the static functions provided to create a Web3File
from another data type.
Please note that the same options of the constructor can be provided in the static functions.
Name | Type | Description |
---|---|---|
content | AsyncIterable<Uint8Array> |
File content to be read |
filename | string |
filename |
[options] | object |
Web3File options |
[options.path] | string |
File Path |
[options.lastModified] | number |
Last modified timestamp |
import { Web3File } from 'web3-file'
const file = new Web3File(
fs.createReadStream('path/to/file.zip'),
'file.zip',
{
path: 'path/to/file.zip',
lastModified: Date.now()
}
)
- Returns
string
Get file name.
- Returns
string
Get file path.
- Returns
number
Get file last modified timestamp.
- Returns
AsyncIterable<Uint8Array>
Get file content readable source.
- Returns
AsyncIterable<Uint8Array>
Get file content readable source iterator.
- Returns
Promise<Blob>
Get file content as a Blob.
Takes Uint8Array bytes to create a Web3File.
import Web3File from 'web3-file'
const file = Web3File.fromBytes(new Uint8Array([2, 44, 1]), 'file.zip')
Takes a string content to create a Web3File.
import { Web3File } from 'web3-file'
const file = Web3File.fromString('web3file', 'file.txt')
Takes a Readable Stream content to create a Web3File.
import { Web3File } from 'web3-file'
const response = await fetch('https://example.org/image.png')
const file = Web3File.fromReadableStream(response.body, 'image.png')
Takes a Blob content to create a Web3File.
import { Web3File } from 'web3-file'
const response = await fetch('https://example.org/image.png')
const blob = await response.blob()
const file = Web3File.fromBlob(blob, 'image.png')
Takes a File content to create a Web3File.
import { Web3File } from 'web3-file'
const image = new File([bytes], 'image.png')
const file = Web3File.fromFile(webFile)