@afriapps/fs-client
Version:
React Native FileServer Client SDK for file synchronization and data management. Requires React Native environment with SQLite and File System support.
107 lines (92 loc) • 3.84 kB
text/typescript
import RNFS from 'react-native-fs';
import {Buffer} from 'buffer';
import {IFileSystem} from './IFileSystem';
export class ReactNativeFileSystem implements IFileSystem {
basePath: string = RNFS.DocumentDirectoryPath;
async readFile(path: string): Promise<string> {
return RNFS.readFile(path, 'utf8');
}
public async writeFile(path: string, data: object | string | Uint8Array | ArrayBuffer): Promise<void> {
let stringData: string;
if (typeof data === 'string') {
// Si data est une chaîne, l'utiliser directement
stringData = data;
} else if (typeof data === 'object' && !(data instanceof ArrayBuffer) && !(data instanceof Uint8Array)) {
// Si data est un objet (et pas un ArrayBuffer ou Uint8Array), le convertir en JSON
stringData = JSON.stringify(data);
} else if (data instanceof ArrayBuffer) {
// Si data est un ArrayBuffer, le convertir en base64
const uint8Array = new Uint8Array(data);
stringData = Buffer.from(String.fromCharCode.apply(null, uint8Array as any)).toString('base64');
} else if (data instanceof Uint8Array) {
// Traiter spécifiquement si besoin, sinon convertir Uint8Array en base64 ou autre format
// Pour cet exemple, on va convertir en base64 aussi, mais vous pouvez choisir de traiter différemment
stringData = Buffer.from(String.fromCharCode.apply(null, data as any)).toString('base64');
} else {
throw new Error("Type de données non pris en charge pour l'écriture du fichier.");
}
try {
if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
// Pour ArrayBuffer et Uint8Array convertis en base64, écrire avec 'base64' encoding
await RNFS.writeFile(path, stringData, 'base64');
} else {
// Pour les autres types, écrire comme du texte
await RNFS.writeFile(path, stringData, 'utf8');
}
} catch (error) {
throw new Error(`Erreur lors de l'écriture du fichier : ${error}`);
}
}
public async writeFileOldOk(path: string, data: object | string): Promise<void> {
let stringData: string;
if (typeof data === 'object') {
// Convertit l'objet en chaîne JSON
stringData = JSON.stringify(data);
} else if (typeof data === 'string') {
// Utilise directement la chaîne
stringData = data;
} else {
throw new Error("Type de données non pris en charge pour l'écriture du fichier.");
}
try {
await RNFS.writeFile(path, stringData, 'utf8');
} catch (error) {
throw new Error(`Erreur lors de l'écriture du fichier : ${error}`);
}
}
async deleteFile(path: string): Promise<void> {
await RNFS.unlink(path);
}
async fileExists(path: string): Promise<boolean> {
try {
const exists = await RNFS.exists(path);
return exists;
} catch (error) {
return false;
}
}
public async directoryExists(path: string): Promise<boolean> {
try {
const stats = await RNFS.stat(path);
return stats.isDirectory();
} catch (error) {
return false;
}
}
async createDirectory(path: string): Promise<void> {
// Utilisez mkdirOptions pour créer le répertoire avec les options appropriées
await RNFS.mkdir(path);
}
public async getFullPathAndEnsureDirectoryExists(relativePath: string): Promise<string> {
const fullPath = `${this.basePath}/${relativePath}`;
const exists = await this.directoryExists(fullPath);
if (!exists) {
try {
await RNFS.mkdir(fullPath, {NSURLIsExcludedFromBackupKey: true});
} catch (error) {
throw new Error(`Erreur lors de la création du répertoire : ${error}`);
}
}
return fullPath;
}
}