File system adapter for Natify Framework using `react-native-blob-util`.
npm install @natify/file-system-rnFile system adapter for Natify Framework using react-native-blob-util.
``bash`
pnpm add @natify/file-system-rn react-native-blob-util
`bash`
cd ios && pod install && cd ..
No additional configuration required.
`typescript
import { NatifyProvider } from "@natify/core";
import { RnFileSystemAdapter } from "@natify/file-system-rn";
const config = {
filesystem: new RnFileSystemAdapter(),
// ... other adapters
};
function App() {
return (
);
}
`
`typescript
import { useAdapter, FileSystemPort } from "@natify/core";
function MyComponent() {
const fileSystem = useAdapter
const readFile = async () => {
try {
// Read as text
const content = await fileSystem.readFile("/path/to/file.txt", "utf8");
console.log(content);
// Read as base64 (for images, PDFs, etc.)
const imageBase64 = await fileSystem.readFile("/path/to/image.png", "base64");
} catch (error) {
console.error("Error reading file:", error);
}
};
}
`
`typescript
const writeFile = async () => {
// Write text
await fileSystem.writeFile("/path/to/file.txt", "Hello World", "utf8");
// Write base64
await fileSystem.writeFile("/path/to/image.png", base64String, "base64");
};
`
`typescript`
const checkFile = async () => {
const exists = await fileSystem.exists("/path/to/file.txt");
if (exists) {
console.log("File exists!");
}
};
`typescript`
const getInfo = async () => {
const info = await fileSystem.getFileInfo("/path/to/file.pdf");
if (info) {
console.log("Size:", info.size, "bytes");
console.log("Last modified:", info.lastModified);
console.log("MIME type:", info.mimeType);
}
};
`typescript`
const listFiles = async () => {
const files = await fileSystem.listFiles("/path/to/directory");
console.log("Files:", files);
};
`typescript
// Create directory
await fileSystem.mkdir("/path/to/new/directory");
// Delete directory (recursive by default)
await fileSystem.rmdir("/path/to/directory", true);
`
`typescript
// Get system directory paths
const documentsPath = fileSystem.getDocumentsPath();
const cachePath = fileSystem.getCachePath();
const tempPath = fileSystem.getTempPath();
// Save file in documents
const filePath = ${documentsPath}/myfile.txt;`
await fileSystem.writeFile(filePath, "Content", "utf8");
`typescript${documentsPath}/downloaded.pdf
const downloadFile = async () => {
const documentsPath = fileSystem.getDocumentsPath();
const destinationPath = ;
const result = await fileSystem.downloadFile(
"https://example.com/file.pdf",
destinationPath
);
console.log("Downloaded to:", result.path);
console.log("Size:", result.size, "bytes");
};
`
`typescript${fileSystem.getDocumentsPath()}/large-file.zip
const downloadWithProgress = async () => {
const destinationPath = ;
await fileSystem.downloadFile(
"https://example.com/large-file.zip",
destinationPath,
{
onProgress: (downloaded, total) => {
const percent = (downloaded / total) * 100;
console.log(Progress: ${percent.toFixed(2)}%);`
},
headers: {
Authorization: "Bearer token",
},
timeout: 30000, // 30 seconds
}
);
};
`typescript`
await fileSystem.downloadFile(url, destinationPath, {
headers: {
Authorization: "Bearer your-token",
"Custom-Header": "value",
},
});
`typescript${fileSystem.getDocumentsPath()}/document.pdf
const uploadFile = async () => {
const filePath = ;
const result = await fileSystem.uploadFile(
filePath,
"https://api.example.com/upload"
);
console.log("Upload status:", result.status);
console.log("Response:", result.data);
};
`
`typescript${fileSystem.getDocumentsPath()}/large-video.mp4
const uploadWithProgress = async () => {
const filePath = ;
await fileSystem.uploadFile(filePath, "https://api.example.com/upload", {
onProgress: (uploaded, total) => {
const percent = (uploaded / total) * 100;
console.log(Upload progress: ${percent.toFixed(2)}%);`
},
headers: {
Authorization: "Bearer token",
},
fieldName: "video", // Form field name
formData: {
title: "My Video",
description: "Video description",
},
});
};
`typescript`
await fileSystem.uploadFile(filePath, uploadUrl, {
fieldName: "file",
formData: {
userId: "123",
category: "documents",
metadata: JSON.stringify({ key: "value" }),
},
headers: {
Authorization: "Bearer token",
},
});
`typescript${documentsPath}/${filename}
const downloadPDF = async (url: string, filename: string) => {
const documentsPath = fileSystem.getDocumentsPath();
const filePath = ;
await fileSystem.downloadFile(url, filePath, {
onProgress: (downloaded, total) => {
// Update UI with progress
updateProgressBar((downloaded / total) * 100);
},
});
return filePath;
};
`
`typescript
import { useAdapter, FileSystemPort, ImagePickerPort } from "@natify/core";
function ImageUploader() {
const fileSystem = useAdapter
const imagePicker = useAdapter
const uploadImage = async () => {
// 1. Select image
const image = await imagePicker.pickImage({
quality: 0.8,
});
if (!image) return;
// 2. Upload image
const result = await fileSystem.uploadFile(image.uri, "https://api.example.com/upload", {
fieldName: "image",
formData: {
userId: currentUserId,
},
onProgress: (uploaded, total) => {
setUploadProgress((uploaded / total) * 100);
},
});
console.log("Image uploaded:", result);
};
}
`
`typescript${fileSystem.getDocumentsPath()}/config.json
const loadConfig = async () => {
const configPath = ;`
try {
const content = await fileSystem.readFile(configPath, "utf8");
const config = JSON.parse(content);
return config;
} catch (error) {
// If doesn't exist, create default one
const defaultConfig = { theme: "light", language: "en" };
await fileSystem.writeFile(
configPath,
JSON.stringify(defaultConfig, null, 2),
"utf8"
);
return defaultConfig;
}
};
`typescript${cachePath}/${filename}
const getCachedFile = async (url: string, filename: string) => {
const cachePath = fileSystem.getCachePath();
const filePath = ;
// Check if already cached
const exists = await fileSystem.exists(filePath);
if (exists) {
return filePath;
}
// Download and save to cache
await fileSystem.downloadFile(url, filePath);
return filePath;
};
`
| Method | Description |
|--------|-------------|
| readFile(path, encoding?) | Reads file content |writeFile(path, content, encoding?)
| | Writes content to file |deleteFile(path)
| | Deletes file |exists(path)
| | Checks if file exists |getFileInfo(path)
| | Gets file information |listFiles(dirPath)
| | Lists files in directory |mkdir(dirPath)
| | Creates directory |rmdir(dirPath, recursive?)
| | Deletes directory |downloadFile(url, destination, options?)
| | Downloads file from URL |uploadFile(filePath, url, options?)
| | Uploads file to server |getDocumentsPath()
| | Documents directory path |getCachePath()
| | Cache directory path |getTempPath()
| | Temp directory path |
`typescript`
interface DownloadOptions {
headers?: Record
onProgress?: (downloaded: number, total: number) => void;
timeout?: number;
overwrite?: boolean;
}
`typescript`
interface UploadOptions {
headers?: Record
onProgress?: (uploaded: number, total: number) => void;
timeout?: number;
fieldName?: string;
formData?: Record
}
`typescript`
interface FileInfo {
path: string;
size: number;
lastModified: Date;
mimeType?: string;
exists: boolean;
}
- Encoding: Use 'utf8' for text and 'base64' for binaries (images, PDFs, etc.)getDocumentsPath()
- Paths: Use , getCachePath(), or getTempPath() to get system pathsNatifyError` with appropriate codes
- Progress: Progress callbacks run on main thread, ideal for updating UI
- Errors: All errors are converted to