@unvired/react-native-unvired-sdk 0.0.22 → 0.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BuildNo.txt +1 -1
- package/dist/database/Database.d.ts +2 -53
- package/dist/database/Database.js +6 -128
- package/dist/database/services/DatabaseNative.js +83 -22
- package/dist/file-system/FileSystem.d.ts +41 -2
- package/dist/file-system/index.d.ts +1 -1
- package/dist/file-system/services/FileSystemNative.d.ts +1 -1
- package/dist/file-system/services/FileSystemNative.js +178 -1
- package/dist/file-system/services/FileSystemWeb.d.ts +1 -1
- package/package.json +1 -1
- package/ts-core/ReactNativePlatformAdapter.ts +8 -2
- package/dist/file-system/FileSystem.types.d.ts +0 -41
- package/dist/file-system/FileSystem.types.js +0 -3
- package/dist/file-system/RNFileEntry.d.ts +0 -55
- package/dist/file-system/RNFileEntry.js +0 -179
- package/ts-core/ReactNativeFileEntry.ts +0 -208
package/BuildNo.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
R-0.000.
|
|
1
|
+
R-0.000.0025
|
|
@@ -10,60 +10,9 @@ export declare class Database {
|
|
|
10
10
|
*/
|
|
11
11
|
executeSql(query: string, params?: any[]): Promise<any[]>;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Delete a database
|
|
14
14
|
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Drop a table
|
|
18
|
-
*/
|
|
19
|
-
dropTable(tableName: string): Promise<void>;
|
|
20
|
-
/**
|
|
21
|
-
* Insert a record
|
|
22
|
-
*/
|
|
23
|
-
insert(tableName: string, data: Record<string, any>): Promise<number>;
|
|
24
|
-
/**
|
|
25
|
-
* Insert multiple records in a transaction
|
|
26
|
-
*/
|
|
27
|
-
insertBatch(tableName: string, records: Record<string, any>[]): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Update records
|
|
30
|
-
*/
|
|
31
|
-
update(tableName: string, data: Record<string, any>, where: string, whereParams?: any[]): Promise<number>;
|
|
32
|
-
/**
|
|
33
|
-
* Delete records
|
|
34
|
-
*/
|
|
35
|
-
delete(tableName: string, where: string, whereParams?: any[]): Promise<number>;
|
|
36
|
-
/**
|
|
37
|
-
* Select records
|
|
38
|
-
*/
|
|
39
|
-
select(tableName: string, columns?: string[], where?: string, whereParams?: any[], orderBy?: string, limit?: number): Promise<any[]>;
|
|
40
|
-
/**
|
|
41
|
-
* Select a single record
|
|
42
|
-
*/
|
|
43
|
-
selectOne(tableName: string, columns?: string[], where?: string, whereParams?: any[]): Promise<any | null>;
|
|
44
|
-
/**
|
|
45
|
-
* Count records
|
|
46
|
-
*/
|
|
47
|
-
count(tableName: string, where?: string, whereParams?: any[]): Promise<number>;
|
|
48
|
-
/**
|
|
49
|
-
* Check if a table exists
|
|
50
|
-
*/
|
|
51
|
-
tableExists(tableName: string): Promise<boolean>;
|
|
52
|
-
/**
|
|
53
|
-
* Get all table names
|
|
54
|
-
*/
|
|
55
|
-
getAllTables(): Promise<string[]>;
|
|
56
|
-
/**
|
|
57
|
-
* Clear all data from a table
|
|
58
|
-
*/
|
|
59
|
-
truncate(tableName: string): Promise<void>;
|
|
60
|
-
/**
|
|
61
|
-
* Execute multiple SQL statements in a transaction
|
|
62
|
-
*/
|
|
63
|
-
transaction(queries: Array<{
|
|
64
|
-
query: string;
|
|
65
|
-
params?: any[];
|
|
66
|
-
}>): Promise<void>;
|
|
15
|
+
deleteDB(): Promise<void>;
|
|
67
16
|
}
|
|
68
17
|
/**
|
|
69
18
|
* Create a new database instance
|
|
@@ -17,135 +17,13 @@ export class Database {
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* Delete a database
|
|
21
21
|
*/
|
|
22
|
-
async
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
await this.executeSql(query);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Drop a table
|
|
31
|
-
*/
|
|
32
|
-
async dropTable(tableName) {
|
|
33
|
-
const query = `DROP TABLE IF EXISTS ${tableName}`;
|
|
34
|
-
await this.executeSql(query);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Insert a record
|
|
38
|
-
*/
|
|
39
|
-
async insert(tableName, data) {
|
|
40
|
-
var _a;
|
|
41
|
-
const columns = Object.keys(data).join(', ');
|
|
42
|
-
const placeholders = Object.keys(data).map(() => '?').join(', ');
|
|
43
|
-
const values = Object.values(data);
|
|
44
|
-
const query = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
|
|
45
|
-
const result = await this.executeSql(query, values);
|
|
46
|
-
// Return the insert ID if available
|
|
47
|
-
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.insertId) || 0;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Insert multiple records in a transaction
|
|
51
|
-
*/
|
|
52
|
-
async insertBatch(tableName, records) {
|
|
53
|
-
if (records.length === 0)
|
|
54
|
-
return;
|
|
55
|
-
const columns = Object.keys(records[0]).join(', ');
|
|
56
|
-
const placeholders = Object.keys(records[0]).map(() => '?').join(', ');
|
|
57
|
-
const query = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
|
|
58
|
-
for (const record of records) {
|
|
59
|
-
const values = Object.values(record);
|
|
60
|
-
await this.executeSql(query, values);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Update records
|
|
65
|
-
*/
|
|
66
|
-
async update(tableName, data, where, whereParams = []) {
|
|
67
|
-
var _a;
|
|
68
|
-
const setClause = Object.keys(data)
|
|
69
|
-
.map(key => `${key} = ?`)
|
|
70
|
-
.join(', ');
|
|
71
|
-
const values = [...Object.values(data), ...whereParams];
|
|
72
|
-
const query = `UPDATE ${tableName} SET ${setClause} WHERE ${where}`;
|
|
73
|
-
const result = await this.executeSql(query, values);
|
|
74
|
-
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.rowsAffected) || 0;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Delete records
|
|
78
|
-
*/
|
|
79
|
-
async delete(tableName, where, whereParams = []) {
|
|
80
|
-
var _a;
|
|
81
|
-
const query = `DELETE FROM ${tableName} WHERE ${where}`;
|
|
82
|
-
const result = await this.executeSql(query, whereParams);
|
|
83
|
-
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.rowsAffected) || 0;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Select records
|
|
87
|
-
*/
|
|
88
|
-
async select(tableName, columns = ['*'], where, whereParams = [], orderBy, limit) {
|
|
89
|
-
let query = `SELECT ${columns.join(', ')} FROM ${tableName}`;
|
|
90
|
-
if (where) {
|
|
91
|
-
query += ` WHERE ${where}`;
|
|
92
|
-
}
|
|
93
|
-
if (orderBy) {
|
|
94
|
-
query += ` ORDER BY ${orderBy}`;
|
|
95
|
-
}
|
|
96
|
-
if (limit) {
|
|
97
|
-
query += ` LIMIT ${limit}`;
|
|
98
|
-
}
|
|
99
|
-
return this.executeSql(query, whereParams);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Select a single record
|
|
103
|
-
*/
|
|
104
|
-
async selectOne(tableName, columns = ['*'], where, whereParams = []) {
|
|
105
|
-
const results = await this.select(tableName, columns, where, whereParams, undefined, 1);
|
|
106
|
-
return results.length > 0 ? results[0] : null;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Count records
|
|
110
|
-
*/
|
|
111
|
-
async count(tableName, where, whereParams = []) {
|
|
112
|
-
var _a;
|
|
113
|
-
let query = `SELECT COUNT(*) as count FROM ${tableName}`;
|
|
114
|
-
if (where) {
|
|
115
|
-
query += ` WHERE ${where}`;
|
|
116
|
-
}
|
|
117
|
-
const result = await this.executeSql(query, whereParams);
|
|
118
|
-
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.count) || 0;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Check if a table exists
|
|
122
|
-
*/
|
|
123
|
-
async tableExists(tableName) {
|
|
124
|
-
const query = `SELECT name FROM sqlite_master WHERE type='table' AND name=?`;
|
|
125
|
-
const result = await this.executeSql(query, [tableName]);
|
|
126
|
-
return result.length > 0;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Get all table names
|
|
130
|
-
*/
|
|
131
|
-
async getAllTables() {
|
|
132
|
-
const query = `SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`;
|
|
133
|
-
const result = await this.executeSql(query);
|
|
134
|
-
return result.map(row => row.name);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Clear all data from a table
|
|
138
|
-
*/
|
|
139
|
-
async truncate(tableName) {
|
|
140
|
-
await this.executeSql(`DELETE FROM ${tableName}`);
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Execute multiple SQL statements in a transaction
|
|
144
|
-
*/
|
|
145
|
-
async transaction(queries) {
|
|
146
|
-
for (const { query, params } of queries) {
|
|
147
|
-
await this.executeSql(query, params || []);
|
|
148
|
-
}
|
|
22
|
+
async deleteDB() {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const adapter = DatabaseManager.getDatabaseAdapter();
|
|
25
|
+
adapter.deleteUserData({ dbName: this.dbName }, () => resolve(), (error) => reject(new Error(String(error))));
|
|
26
|
+
});
|
|
149
27
|
}
|
|
150
28
|
}
|
|
151
29
|
/**
|
|
@@ -9,10 +9,9 @@ export class DatabaseNative {
|
|
|
9
9
|
if (this.databases.has(dbKey)) {
|
|
10
10
|
return this.databases.get(dbKey);
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// For now, we rely on the name.
|
|
12
|
+
if (!name) {
|
|
13
|
+
throw new Error("Database name is required");
|
|
14
|
+
}
|
|
16
15
|
const db = open({ name });
|
|
17
16
|
this.databases.set(dbKey, db);
|
|
18
17
|
return db;
|
|
@@ -29,43 +28,105 @@ export class DatabaseNative {
|
|
|
29
28
|
}
|
|
30
29
|
},
|
|
31
30
|
execute: async (options, successCallback, errorCallback) => {
|
|
32
|
-
var _a;
|
|
33
31
|
try {
|
|
34
32
|
const db = this.getDatabase(options.dbName);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
const
|
|
40
|
-
|
|
33
|
+
// FIX: Await the result properly
|
|
34
|
+
const res = await db.execute(options.query, options.params);
|
|
35
|
+
let resultToReturn = [];
|
|
36
|
+
// Cast to any to avoid TS error "Property '_array' does not exist on type 'never'"
|
|
37
|
+
const rawRes = res;
|
|
38
|
+
if (rawRes) {
|
|
39
|
+
if (rawRes.rows) {
|
|
40
|
+
const rawRows = rawRes.rows;
|
|
41
|
+
if (Array.isArray(rawRows)) {
|
|
42
|
+
resultToReturn = rawRows;
|
|
43
|
+
}
|
|
44
|
+
else if (rawRows._array && Array.isArray(rawRows._array)) {
|
|
45
|
+
resultToReturn = rawRows._array;
|
|
46
|
+
}
|
|
47
|
+
else if (typeof rawRows.length === 'number') {
|
|
48
|
+
// JSI iterator
|
|
49
|
+
for (let i = 0; i < rawRows.length; i++) {
|
|
50
|
+
// Use item() if available, otherwise index access
|
|
51
|
+
const r = rawRows.item ? rawRows.item(i) : rawRows[i];
|
|
52
|
+
resultToReturn.push(r);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else if (Array.isArray(rawRes)) {
|
|
57
|
+
resultToReturn = rawRes;
|
|
58
|
+
}
|
|
59
|
+
// Attach insertId if present (though interface usually expects strict array,
|
|
60
|
+
// JS runtime allows attaching props to array)
|
|
61
|
+
if (rawRes.insertId !== undefined) {
|
|
62
|
+
resultToReturn.insertId = rawRes.insertId;
|
|
63
|
+
resultToReturn.rowsAffected = rawRes.rowsAffected;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
successCallback(resultToReturn);
|
|
41
67
|
}
|
|
42
68
|
catch (error) {
|
|
43
69
|
errorCallback(error instanceof Error ? error.message : String(error));
|
|
44
70
|
}
|
|
45
71
|
},
|
|
46
72
|
executeStatementOnPath: async (dbPath, sqlQuery, callback) => {
|
|
47
|
-
var _a
|
|
73
|
+
var _a;
|
|
48
74
|
try {
|
|
49
75
|
const dbName = ((_a = dbPath.split("/").pop()) === null || _a === void 0 ? void 0 : _a.replace(".db", "")) || "default";
|
|
50
76
|
const db = this.getDatabase(dbName);
|
|
51
|
-
const res = db.execute(sqlQuery);
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
77
|
+
const res = await db.execute(sqlQuery);
|
|
78
|
+
let resultToReturn = [];
|
|
79
|
+
const rawRes = res;
|
|
80
|
+
if (rawRes && rawRes.rows) {
|
|
81
|
+
const rawRows = rawRes.rows;
|
|
82
|
+
if (typeof rawRows.length === 'number') {
|
|
83
|
+
for (let i = 0; i < rawRows.length; i++) {
|
|
84
|
+
const r = rawRows.item ? rawRows.item(i) : rawRows[i];
|
|
85
|
+
resultToReturn.push(r);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (Array.isArray(rawRows)) {
|
|
89
|
+
resultToReturn = rawRows;
|
|
90
|
+
}
|
|
91
|
+
else if (rawRows._array) {
|
|
92
|
+
resultToReturn = rawRows._array;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Attach insertId if present
|
|
96
|
+
if (rawRes && rawRes.insertId !== undefined) {
|
|
97
|
+
resultToReturn.insertId = rawRes.insertId;
|
|
98
|
+
resultToReturn.rowsAffected = rawRes.rowsAffected;
|
|
99
|
+
}
|
|
100
|
+
callback(resultToReturn);
|
|
55
101
|
}
|
|
56
102
|
catch (error) {
|
|
57
103
|
callback([]);
|
|
58
104
|
}
|
|
59
105
|
},
|
|
60
106
|
selectFromPath: async (dbPath, sqlQuery, callback) => {
|
|
61
|
-
var _a
|
|
107
|
+
var _a;
|
|
62
108
|
try {
|
|
63
109
|
const dbName = ((_a = dbPath.split("/").pop()) === null || _a === void 0 ? void 0 : _a.replace(".db", "")) || "default";
|
|
64
110
|
const db = this.getDatabase(dbName);
|
|
65
|
-
const res = db.execute(sqlQuery);
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
111
|
+
const res = await db.execute(sqlQuery);
|
|
112
|
+
let resultToReturn = [];
|
|
113
|
+
const rawRes = res;
|
|
114
|
+
if (rawRes && rawRes.rows) {
|
|
115
|
+
const rawRows = rawRes.rows;
|
|
116
|
+
if (typeof rawRows.length === 'number') {
|
|
117
|
+
for (let i = 0; i < rawRows.length; i++) {
|
|
118
|
+
const r = rawRows.item ? rawRows.item(i) : rawRows[i];
|
|
119
|
+
resultToReturn.push(r);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if (Array.isArray(rawRows)) {
|
|
123
|
+
resultToReturn = rawRows;
|
|
124
|
+
}
|
|
125
|
+
else if (rawRows._array) {
|
|
126
|
+
resultToReturn = rawRows._array;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
callback(resultToReturn);
|
|
69
130
|
}
|
|
70
131
|
catch (error) {
|
|
71
132
|
callback([]);
|
|
@@ -106,7 +167,7 @@ export class DatabaseNative {
|
|
|
106
167
|
? "DELETE FROM user_data WHERE user_id = ?"
|
|
107
168
|
: "DELETE FROM user_data";
|
|
108
169
|
const params = options.userId ? [options.userId] : [];
|
|
109
|
-
db.execute(query, params);
|
|
170
|
+
await db.execute(query, params);
|
|
110
171
|
callback();
|
|
111
172
|
}
|
|
112
173
|
catch (error) {
|
|
@@ -1,4 +1,44 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* File entry interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IDirectoryReader {
|
|
5
|
+
readEntries(successCallback: (entries: IFileEntry[]) => void, errorCallback: (error: any) => void): void;
|
|
6
|
+
}
|
|
7
|
+
export interface IFileEntry {
|
|
8
|
+
isFile: boolean;
|
|
9
|
+
isDirectory: boolean;
|
|
10
|
+
name: string;
|
|
11
|
+
fullPath: string;
|
|
12
|
+
filesystem?: any;
|
|
13
|
+
nativeURL: string;
|
|
14
|
+
remove(callback: () => void, errorCallback: (error: any) => void): void;
|
|
15
|
+
removeRecursively(callback: () => void, errorCallback: (error: any) => void): void;
|
|
16
|
+
createReader(): IDirectoryReader;
|
|
17
|
+
getDirectory?(path: string, options: {
|
|
18
|
+
create: boolean;
|
|
19
|
+
}, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
20
|
+
getFile?(path: string, options: {
|
|
21
|
+
create: boolean;
|
|
22
|
+
}, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
23
|
+
file?(callback: (file: any) => void, errorCallback: (error: any) => void): void;
|
|
24
|
+
createWriter?(callback: (writer: any) => void, errorCallback: (error: any) => void): void;
|
|
25
|
+
}
|
|
26
|
+
export type FileSystemEncoding = 'utf8' | 'ascii' | 'base64';
|
|
27
|
+
export interface IFileSystem {
|
|
28
|
+
getDocumentDirectory(): string;
|
|
29
|
+
resolveLocalFileSystemURL(url: string): Promise<IFileEntry>;
|
|
30
|
+
getFolderBasedOnUserId(userId: string): Promise<string>;
|
|
31
|
+
deleteUserFolder(userId: string): Promise<void>;
|
|
32
|
+
createDirectory(path: string): Promise<void>;
|
|
33
|
+
readFile(path: string, encoding?: FileSystemEncoding): Promise<string>;
|
|
34
|
+
writeFile(path: string, content: string, encoding?: FileSystemEncoding): Promise<void>;
|
|
35
|
+
deleteFile(path: string): Promise<void>;
|
|
36
|
+
exists(path: string): Promise<boolean>;
|
|
37
|
+
stat(path: string): Promise<any>;
|
|
38
|
+
readDir(path: string): Promise<any[]>;
|
|
39
|
+
copyFile(source: string, destination: string): Promise<void>;
|
|
40
|
+
moveFile(source: string, destination: string): Promise<void>;
|
|
41
|
+
}
|
|
2
42
|
/**
|
|
3
43
|
* FileSystem class
|
|
4
44
|
* Handles file system operations for React Native
|
|
@@ -59,5 +99,4 @@ export declare class FileSystem {
|
|
|
59
99
|
*/
|
|
60
100
|
static moveFile(source: string, destination: string): Promise<void>;
|
|
61
101
|
}
|
|
62
|
-
export type { IFileEntry } from './FileSystem.types';
|
|
63
102
|
export default FileSystem;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as RNFS from 'react-native-fs';
|
|
2
|
-
import { IFileEntry, IFileSystem, FileSystemEncoding } from '../FileSystem
|
|
2
|
+
import type { IFileEntry, IFileSystem, FileSystemEncoding } from '../FileSystem';
|
|
3
3
|
export declare class FileSystemNative implements IFileSystem {
|
|
4
4
|
getDocumentDirectory(): string;
|
|
5
5
|
resolveLocalFileSystemURL(url: string): Promise<IFileEntry>;
|
|
@@ -1,5 +1,182 @@
|
|
|
1
1
|
import * as RNFS from 'react-native-fs';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* DirectoryReader implementation for React Native
|
|
4
|
+
* Wraps RNFS.readDir to match the Cordova DirectoryReader interface
|
|
5
|
+
*/
|
|
6
|
+
class RNDirectoryReader {
|
|
7
|
+
constructor(dirPath) {
|
|
8
|
+
this.dirPath = dirPath;
|
|
9
|
+
}
|
|
10
|
+
readEntries(callback, errorCallback) {
|
|
11
|
+
RNFS.readDir(this.dirPath)
|
|
12
|
+
.then(items => {
|
|
13
|
+
const entries = items.map(item => new RNFileEntry(item.path, item.isDirectory()));
|
|
14
|
+
callback(entries);
|
|
15
|
+
})
|
|
16
|
+
.catch(errorCallback);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* FileEntry implementation for React Native
|
|
21
|
+
* Wraps react-native-fs to match the Cordova FileEntry interface
|
|
22
|
+
*/
|
|
23
|
+
class RNFileEntry {
|
|
24
|
+
constructor(path, isDirectory = false) {
|
|
25
|
+
this.fullPath = path;
|
|
26
|
+
this.nativeURL = `file://${path}`;
|
|
27
|
+
this.isDirectory = isDirectory;
|
|
28
|
+
this.isFile = !isDirectory;
|
|
29
|
+
this.name = path.split('/').pop() || '';
|
|
30
|
+
this.filesystem = null;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Remove a file or empty directory
|
|
34
|
+
*/
|
|
35
|
+
remove(callback, errorCallback) {
|
|
36
|
+
RNFS.unlink(this.fullPath)
|
|
37
|
+
.then(() => callback())
|
|
38
|
+
.catch(errorCallback);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Remove a directory and all its contents recursively
|
|
42
|
+
*/
|
|
43
|
+
removeRecursively(callback, errorCallback) {
|
|
44
|
+
// RNFS.unlink works recursively for directories
|
|
45
|
+
RNFS.unlink(this.fullPath)
|
|
46
|
+
.then(() => callback())
|
|
47
|
+
.catch(errorCallback);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create a directory reader for reading directory contents
|
|
51
|
+
*/
|
|
52
|
+
createReader() {
|
|
53
|
+
return new RNDirectoryReader(this.fullPath);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get or create a subdirectory
|
|
57
|
+
*/
|
|
58
|
+
getDirectory(path, options, callback, errorCallback) {
|
|
59
|
+
const dirPath = `${this.fullPath}/${path}`;
|
|
60
|
+
RNFS.exists(dirPath)
|
|
61
|
+
.then(exists => {
|
|
62
|
+
if (exists) {
|
|
63
|
+
return RNFS.stat(dirPath).then(stats => {
|
|
64
|
+
if (stats.isDirectory()) {
|
|
65
|
+
callback(new RNFileEntry(dirPath, true));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
errorCallback(new Error('Path exists but is not a directory'));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
else if (options.create) {
|
|
73
|
+
return RNFS.mkdir(dirPath).then(() => {
|
|
74
|
+
callback(new RNFileEntry(dirPath, true));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
errorCallback(new Error('Directory does not exist'));
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
.catch(errorCallback);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get or create a file
|
|
85
|
+
*/
|
|
86
|
+
getFile(path, options, callback, errorCallback) {
|
|
87
|
+
const filePath = `${this.fullPath}/${path}`;
|
|
88
|
+
RNFS.exists(filePath)
|
|
89
|
+
.then(exists => {
|
|
90
|
+
if (exists) {
|
|
91
|
+
return RNFS.stat(filePath).then(stats => {
|
|
92
|
+
if (!stats.isDirectory()) {
|
|
93
|
+
callback(new RNFileEntry(filePath, false));
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
errorCallback(new Error('Path exists but is a directory'));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
else if (options.create) {
|
|
101
|
+
// Create empty file
|
|
102
|
+
return RNFS.writeFile(filePath, '', 'utf8').then(() => {
|
|
103
|
+
callback(new RNFileEntry(filePath, false));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
errorCallback(new Error('File does not exist'));
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
.catch(errorCallback);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get file metadata
|
|
114
|
+
*/
|
|
115
|
+
file(callback, errorCallback) {
|
|
116
|
+
RNFS.stat(this.fullPath)
|
|
117
|
+
.then(stats => {
|
|
118
|
+
// Create a File-like object with metadata
|
|
119
|
+
const fileObject = {
|
|
120
|
+
name: this.fullPath.split('/').pop() || '',
|
|
121
|
+
size: stats.size,
|
|
122
|
+
type: '', // RNFS doesn't provide MIME type directly
|
|
123
|
+
lastModified: new Date(stats.mtime).getTime(),
|
|
124
|
+
lastModifiedDate: new Date(stats.mtime),
|
|
125
|
+
path: this.fullPath,
|
|
126
|
+
};
|
|
127
|
+
callback(fileObject);
|
|
128
|
+
})
|
|
129
|
+
.catch(errorCallback);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Create a file writer for writing to the file
|
|
133
|
+
*/
|
|
134
|
+
createWriter(callback, errorCallback) {
|
|
135
|
+
// Create a FileWriter-like object
|
|
136
|
+
const writer = {
|
|
137
|
+
filePath: this.fullPath,
|
|
138
|
+
write: (data) => {
|
|
139
|
+
return RNFS.writeFile(this.fullPath, data, 'utf8')
|
|
140
|
+
.then(() => { if (writer.onwriteend)
|
|
141
|
+
writer.onwriteend(); })
|
|
142
|
+
.catch((err) => { if (writer.onerror)
|
|
143
|
+
writer.onerror(err); });
|
|
144
|
+
},
|
|
145
|
+
append: (data) => {
|
|
146
|
+
return RNFS.appendFile(this.fullPath, data, 'utf8')
|
|
147
|
+
.then(() => { if (writer.onwriteend)
|
|
148
|
+
writer.onwriteend(); })
|
|
149
|
+
.catch((err) => { if (writer.onerror)
|
|
150
|
+
writer.onerror(err); });
|
|
151
|
+
},
|
|
152
|
+
truncate: (size) => {
|
|
153
|
+
// Read file, truncate content, and write back
|
|
154
|
+
return RNFS.readFile(this.fullPath, 'utf8')
|
|
155
|
+
.then(content => {
|
|
156
|
+
const truncated = content.substring(0, size);
|
|
157
|
+
return RNFS.writeFile(this.fullPath, truncated, 'utf8')
|
|
158
|
+
.then(() => { if (writer.onwriteend)
|
|
159
|
+
writer.onwriteend(); });
|
|
160
|
+
})
|
|
161
|
+
.catch((err) => { if (writer.onerror)
|
|
162
|
+
writer.onerror(err); });
|
|
163
|
+
},
|
|
164
|
+
seek: (offset) => {
|
|
165
|
+
// Not directly supported in RNFS
|
|
166
|
+
// This would need to be implemented with read/write operations
|
|
167
|
+
return Promise.resolve();
|
|
168
|
+
},
|
|
169
|
+
onwriteend: null,
|
|
170
|
+
onerror: null
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
callback(writer);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
errorCallback(error);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
3
180
|
export class FileSystemNative {
|
|
4
181
|
getDocumentDirectory() {
|
|
5
182
|
return RNFS.DocumentDirectoryPath;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IFileEntry, IFileSystem, FileSystemEncoding } from '../FileSystem
|
|
1
|
+
import type { IFileEntry, IFileSystem, FileSystemEncoding } from '../FileSystem';
|
|
2
2
|
export declare class FileSystemWeb implements IFileSystem {
|
|
3
3
|
getDocumentDirectory(): string;
|
|
4
4
|
resolveLocalFileSystemURL(url: string): Promise<IFileEntry>;
|
package/package.json
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import { DeviceInfo
|
|
1
|
+
import { DeviceInfo } from '../src/device-info';
|
|
2
|
+
import { DatabaseManager } from '../src/database';
|
|
3
|
+
import { LocalStorage } from '../src/local-storage';
|
|
4
|
+
import { logger, LogLevel } from '../src/logger';
|
|
5
|
+
import { PushNotification } from '../src/push-notification';
|
|
6
|
+
import { FileSystem } from '../src/file-system';
|
|
2
7
|
import { PlatformInterface, IDeviceInfo, IFileEntry, IDatabaseAdapter, IPushNotificationAdapter, ILoggerAdapter, IStorageAdapter } from './PlatformInterface';
|
|
3
8
|
|
|
4
9
|
export class ReactNativePlatformAdapter implements PlatformInterface {
|
|
@@ -74,7 +79,8 @@ export class ReactNativePlatformAdapter implements PlatformInterface {
|
|
|
74
79
|
await pushNotification.requestPermission(options);
|
|
75
80
|
},
|
|
76
81
|
getToken: async () => {
|
|
77
|
-
|
|
82
|
+
const token = await pushNotification.getToken();
|
|
83
|
+
return token || '';
|
|
78
84
|
},
|
|
79
85
|
onTokenRefresh: (callback) => {
|
|
80
86
|
pushNotification.onTokenRefresh(callback);
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* File entry interface
|
|
3
|
-
*/
|
|
4
|
-
export interface IDirectoryReader {
|
|
5
|
-
readEntries(successCallback: (entries: IFileEntry[]) => void, errorCallback: (error: any) => void): void;
|
|
6
|
-
}
|
|
7
|
-
export interface IFileEntry {
|
|
8
|
-
isFile: boolean;
|
|
9
|
-
isDirectory: boolean;
|
|
10
|
-
name: string;
|
|
11
|
-
fullPath: string;
|
|
12
|
-
filesystem?: any;
|
|
13
|
-
nativeURL: string;
|
|
14
|
-
remove(callback: () => void, errorCallback: (error: any) => void): void;
|
|
15
|
-
removeRecursively(callback: () => void, errorCallback: (error: any) => void): void;
|
|
16
|
-
createReader(): IDirectoryReader;
|
|
17
|
-
getDirectory?(path: string, options: {
|
|
18
|
-
create: boolean;
|
|
19
|
-
}, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
20
|
-
getFile?(path: string, options: {
|
|
21
|
-
create: boolean;
|
|
22
|
-
}, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
23
|
-
file?(callback: (file: any) => void, errorCallback: (error: any) => void): void;
|
|
24
|
-
createWriter?(callback: (writer: any) => void, errorCallback: (error: any) => void): void;
|
|
25
|
-
}
|
|
26
|
-
export type FileSystemEncoding = 'utf8' | 'ascii' | 'base64';
|
|
27
|
-
export interface IFileSystem {
|
|
28
|
-
getDocumentDirectory(): string;
|
|
29
|
-
resolveLocalFileSystemURL(url: string): Promise<IFileEntry>;
|
|
30
|
-
getFolderBasedOnUserId(userId: string): Promise<string>;
|
|
31
|
-
deleteUserFolder(userId: string): Promise<void>;
|
|
32
|
-
createDirectory(path: string): Promise<void>;
|
|
33
|
-
readFile(path: string, encoding?: FileSystemEncoding): Promise<string>;
|
|
34
|
-
writeFile(path: string, content: string, encoding?: FileSystemEncoding): Promise<void>;
|
|
35
|
-
deleteFile(path: string): Promise<void>;
|
|
36
|
-
exists(path: string): Promise<boolean>;
|
|
37
|
-
stat(path: string): Promise<any>;
|
|
38
|
-
readDir(path: string): Promise<any[]>;
|
|
39
|
-
copyFile(source: string, destination: string): Promise<void>;
|
|
40
|
-
moveFile(source: string, destination: string): Promise<void>;
|
|
41
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { IFileEntry, IDirectoryReader } from './FileSystem.types';
|
|
2
|
-
/**
|
|
3
|
-
* DirectoryReader implementation for React Native
|
|
4
|
-
* Wraps RNFS.readDir to match the Cordova DirectoryReader interface
|
|
5
|
-
*/
|
|
6
|
-
export declare class RNDirectoryReader implements IDirectoryReader {
|
|
7
|
-
private dirPath;
|
|
8
|
-
constructor(dirPath: string);
|
|
9
|
-
readEntries(callback: (entries: IFileEntry[]) => void, errorCallback: (error: any) => void): void;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* FileEntry implementation for React Native
|
|
13
|
-
* Wraps react-native-fs to match the Cordova FileEntry interface
|
|
14
|
-
*/
|
|
15
|
-
export declare class RNFileEntry implements IFileEntry {
|
|
16
|
-
isFile: boolean;
|
|
17
|
-
isDirectory: boolean;
|
|
18
|
-
name: string;
|
|
19
|
-
fullPath: string;
|
|
20
|
-
nativeURL: string;
|
|
21
|
-
filesystem: any;
|
|
22
|
-
constructor(path: string, isDirectory?: boolean);
|
|
23
|
-
/**
|
|
24
|
-
* Remove a file or empty directory
|
|
25
|
-
*/
|
|
26
|
-
remove(callback: () => void, errorCallback: (error: any) => void): void;
|
|
27
|
-
/**
|
|
28
|
-
* Remove a directory and all its contents recursively
|
|
29
|
-
*/
|
|
30
|
-
removeRecursively(callback: () => void, errorCallback: (error: any) => void): void;
|
|
31
|
-
/**
|
|
32
|
-
* Create a directory reader for reading directory contents
|
|
33
|
-
*/
|
|
34
|
-
createReader(): IDirectoryReader;
|
|
35
|
-
/**
|
|
36
|
-
* Get or create a subdirectory
|
|
37
|
-
*/
|
|
38
|
-
getDirectory(path: string, options: {
|
|
39
|
-
create: boolean;
|
|
40
|
-
}, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
41
|
-
/**
|
|
42
|
-
* Get or create a file
|
|
43
|
-
*/
|
|
44
|
-
getFile(path: string, options: {
|
|
45
|
-
create: boolean;
|
|
46
|
-
}, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
47
|
-
/**
|
|
48
|
-
* Get file metadata
|
|
49
|
-
*/
|
|
50
|
-
file(callback: (file: any) => void, errorCallback: (error: any) => void): void;
|
|
51
|
-
/**
|
|
52
|
-
* Create a file writer for writing to the file
|
|
53
|
-
*/
|
|
54
|
-
createWriter(callback: (writer: any) => void, errorCallback: (error: any) => void): void;
|
|
55
|
-
}
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import * as RNFS from 'react-native-fs';
|
|
2
|
-
/**
|
|
3
|
-
* DirectoryReader implementation for React Native
|
|
4
|
-
* Wraps RNFS.readDir to match the Cordova DirectoryReader interface
|
|
5
|
-
*/
|
|
6
|
-
export class RNDirectoryReader {
|
|
7
|
-
constructor(dirPath) {
|
|
8
|
-
this.dirPath = dirPath;
|
|
9
|
-
}
|
|
10
|
-
readEntries(callback, errorCallback) {
|
|
11
|
-
RNFS.readDir(this.dirPath)
|
|
12
|
-
.then(items => {
|
|
13
|
-
const entries = items.map(item => new RNFileEntry(item.path, item.isDirectory()));
|
|
14
|
-
callback(entries);
|
|
15
|
-
})
|
|
16
|
-
.catch(errorCallback);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* FileEntry implementation for React Native
|
|
21
|
-
* Wraps react-native-fs to match the Cordova FileEntry interface
|
|
22
|
-
*/
|
|
23
|
-
export class RNFileEntry {
|
|
24
|
-
constructor(path, isDirectory = false) {
|
|
25
|
-
this.fullPath = path;
|
|
26
|
-
this.nativeURL = `file://${path}`;
|
|
27
|
-
this.isDirectory = isDirectory;
|
|
28
|
-
this.isFile = !isDirectory;
|
|
29
|
-
this.name = path.split('/').pop() || '';
|
|
30
|
-
this.filesystem = null;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Remove a file or empty directory
|
|
34
|
-
*/
|
|
35
|
-
remove(callback, errorCallback) {
|
|
36
|
-
RNFS.unlink(this.fullPath)
|
|
37
|
-
.then(() => callback())
|
|
38
|
-
.catch(errorCallback);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Remove a directory and all its contents recursively
|
|
42
|
-
*/
|
|
43
|
-
removeRecursively(callback, errorCallback) {
|
|
44
|
-
// RNFS.unlink works recursively for directories
|
|
45
|
-
RNFS.unlink(this.fullPath)
|
|
46
|
-
.then(() => callback())
|
|
47
|
-
.catch(errorCallback);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Create a directory reader for reading directory contents
|
|
51
|
-
*/
|
|
52
|
-
createReader() {
|
|
53
|
-
return new RNDirectoryReader(this.fullPath);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Get or create a subdirectory
|
|
57
|
-
*/
|
|
58
|
-
getDirectory(path, options, callback, errorCallback) {
|
|
59
|
-
const dirPath = `${this.fullPath}/${path}`;
|
|
60
|
-
RNFS.exists(dirPath)
|
|
61
|
-
.then(exists => {
|
|
62
|
-
if (exists) {
|
|
63
|
-
return RNFS.stat(dirPath).then(stats => {
|
|
64
|
-
if (stats.isDirectory()) {
|
|
65
|
-
callback(new RNFileEntry(dirPath, true));
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
errorCallback(new Error('Path exists but is not a directory'));
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
else if (options.create) {
|
|
73
|
-
return RNFS.mkdir(dirPath).then(() => {
|
|
74
|
-
callback(new RNFileEntry(dirPath, true));
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
errorCallback(new Error('Directory does not exist'));
|
|
79
|
-
}
|
|
80
|
-
})
|
|
81
|
-
.catch(errorCallback);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Get or create a file
|
|
85
|
-
*/
|
|
86
|
-
getFile(path, options, callback, errorCallback) {
|
|
87
|
-
const filePath = `${this.fullPath}/${path}`;
|
|
88
|
-
RNFS.exists(filePath)
|
|
89
|
-
.then(exists => {
|
|
90
|
-
if (exists) {
|
|
91
|
-
return RNFS.stat(filePath).then(stats => {
|
|
92
|
-
if (!stats.isDirectory()) {
|
|
93
|
-
callback(new RNFileEntry(filePath, false));
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
errorCallback(new Error('Path exists but is a directory'));
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
else if (options.create) {
|
|
101
|
-
// Create empty file
|
|
102
|
-
return RNFS.writeFile(filePath, '', 'utf8').then(() => {
|
|
103
|
-
callback(new RNFileEntry(filePath, false));
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
errorCallback(new Error('File does not exist'));
|
|
108
|
-
}
|
|
109
|
-
})
|
|
110
|
-
.catch(errorCallback);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Get file metadata
|
|
114
|
-
*/
|
|
115
|
-
file(callback, errorCallback) {
|
|
116
|
-
RNFS.stat(this.fullPath)
|
|
117
|
-
.then(stats => {
|
|
118
|
-
// Create a File-like object with metadata
|
|
119
|
-
const fileObject = {
|
|
120
|
-
name: this.fullPath.split('/').pop() || '',
|
|
121
|
-
size: stats.size,
|
|
122
|
-
type: '', // RNFS doesn't provide MIME type directly
|
|
123
|
-
lastModified: new Date(stats.mtime).getTime(),
|
|
124
|
-
lastModifiedDate: new Date(stats.mtime),
|
|
125
|
-
path: this.fullPath,
|
|
126
|
-
};
|
|
127
|
-
callback(fileObject);
|
|
128
|
-
})
|
|
129
|
-
.catch(errorCallback);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Create a file writer for writing to the file
|
|
133
|
-
*/
|
|
134
|
-
createWriter(callback, errorCallback) {
|
|
135
|
-
// Create a FileWriter-like object
|
|
136
|
-
const writer = {
|
|
137
|
-
filePath: this.fullPath,
|
|
138
|
-
write: (data) => {
|
|
139
|
-
return RNFS.writeFile(this.fullPath, data, 'utf8')
|
|
140
|
-
.then(() => { if (writer.onwriteend)
|
|
141
|
-
writer.onwriteend(); })
|
|
142
|
-
.catch((err) => { if (writer.onerror)
|
|
143
|
-
writer.onerror(err); });
|
|
144
|
-
},
|
|
145
|
-
append: (data) => {
|
|
146
|
-
return RNFS.appendFile(this.fullPath, data, 'utf8')
|
|
147
|
-
.then(() => { if (writer.onwriteend)
|
|
148
|
-
writer.onwriteend(); })
|
|
149
|
-
.catch((err) => { if (writer.onerror)
|
|
150
|
-
writer.onerror(err); });
|
|
151
|
-
},
|
|
152
|
-
truncate: (size) => {
|
|
153
|
-
// Read file, truncate content, and write back
|
|
154
|
-
return RNFS.readFile(this.fullPath, 'utf8')
|
|
155
|
-
.then(content => {
|
|
156
|
-
const truncated = content.substring(0, size);
|
|
157
|
-
return RNFS.writeFile(this.fullPath, truncated, 'utf8')
|
|
158
|
-
.then(() => { if (writer.onwriteend)
|
|
159
|
-
writer.onwriteend(); });
|
|
160
|
-
})
|
|
161
|
-
.catch((err) => { if (writer.onerror)
|
|
162
|
-
writer.onerror(err); });
|
|
163
|
-
},
|
|
164
|
-
seek: (offset) => {
|
|
165
|
-
// Not directly supported in RNFS
|
|
166
|
-
// This would need to be implemented with read/write operations
|
|
167
|
-
return Promise.resolve();
|
|
168
|
-
},
|
|
169
|
-
onwriteend: null,
|
|
170
|
-
onerror: null
|
|
171
|
-
};
|
|
172
|
-
try {
|
|
173
|
-
callback(writer);
|
|
174
|
-
}
|
|
175
|
-
catch (error) {
|
|
176
|
-
errorCallback(error);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import RNFS from 'react-native-fs';
|
|
2
|
-
export interface IDirectoryReader {
|
|
3
|
-
readEntries(callback: (entries: IFileEntry[]) => void, errorCallback: (error: any) => void): void;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export interface IFileEntry {
|
|
7
|
-
fullPath: string;
|
|
8
|
-
nativeURL: string;
|
|
9
|
-
isDirectory?: boolean;
|
|
10
|
-
remove(callback: () => void, errorCallback: (error: any) => void): void;
|
|
11
|
-
removeRecursively(callback: () => void, errorCallback: (error: any) => void): void;
|
|
12
|
-
createReader(): IDirectoryReader;
|
|
13
|
-
getDirectory?(path: string, options: { create: boolean }, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
14
|
-
getFile?(path: string, options: { create: boolean }, callback: (entry: IFileEntry) => void, errorCallback: (error: any) => void): void;
|
|
15
|
-
file?(callback: (file: File) => void, errorCallback: (error: any) => void): void;
|
|
16
|
-
createWriter?(callback: (writer: any) => void, errorCallback: (error: any) => void): void;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* DirectoryReader implementation for React Native
|
|
20
|
-
* Wraps RNFS.readDir to match the Cordova DirectoryReader interface
|
|
21
|
-
*/
|
|
22
|
-
export class ReactNativeDirectoryReader implements IDirectoryReader {
|
|
23
|
-
private dirPath: string;
|
|
24
|
-
|
|
25
|
-
constructor(dirPath: string) {
|
|
26
|
-
this.dirPath = dirPath;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
readEntries(
|
|
30
|
-
callback: (entries: IFileEntry[]) => void,
|
|
31
|
-
errorCallback: (error: any) => void
|
|
32
|
-
): void {
|
|
33
|
-
RNFS.readDir(this.dirPath)
|
|
34
|
-
.then(items => {
|
|
35
|
-
const entries = items.map(item =>
|
|
36
|
-
new ReactNativeFileEntry(item.path, item.isDirectory())
|
|
37
|
-
);
|
|
38
|
-
callback(entries);
|
|
39
|
-
})
|
|
40
|
-
.catch(errorCallback);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* FileEntry implementation for React Native
|
|
46
|
-
* Wraps react-native-fs to match the Cordova FileEntry interface
|
|
47
|
-
*/
|
|
48
|
-
export class ReactNativeFileEntry implements IFileEntry {
|
|
49
|
-
fullPath: string;
|
|
50
|
-
nativeURL: string;
|
|
51
|
-
isDirectory?: boolean;
|
|
52
|
-
|
|
53
|
-
constructor(path: string, isDirectory: boolean = false) {
|
|
54
|
-
this.fullPath = path;
|
|
55
|
-
this.nativeURL = `file://${path}`;
|
|
56
|
-
this.isDirectory = isDirectory;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Remove a file or empty directory
|
|
61
|
-
*/
|
|
62
|
-
remove(callback: () => void, errorCallback: (error: any) => void): void {
|
|
63
|
-
RNFS.unlink(this.fullPath)
|
|
64
|
-
.then(() => callback())
|
|
65
|
-
.catch(errorCallback);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Remove a directory and all its contents recursively
|
|
70
|
-
*/
|
|
71
|
-
removeRecursively(callback: () => void, errorCallback: (error: any) => void): void {
|
|
72
|
-
// RNFS.unlink works recursively for directories
|
|
73
|
-
RNFS.unlink(this.fullPath)
|
|
74
|
-
.then(() => callback())
|
|
75
|
-
.catch(errorCallback);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Create a directory reader for reading directory contents
|
|
80
|
-
*/
|
|
81
|
-
createReader(): IDirectoryReader {
|
|
82
|
-
return new ReactNativeDirectoryReader(this.fullPath);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Get or create a subdirectory
|
|
87
|
-
*/
|
|
88
|
-
getDirectory(
|
|
89
|
-
path: string,
|
|
90
|
-
options: { create: boolean },
|
|
91
|
-
callback: (entry: IFileEntry) => void,
|
|
92
|
-
errorCallback: (error: any) => void
|
|
93
|
-
): void {
|
|
94
|
-
const dirPath = `${this.fullPath}/${path}`;
|
|
95
|
-
|
|
96
|
-
RNFS.exists(dirPath)
|
|
97
|
-
.then(exists => {
|
|
98
|
-
if (exists) {
|
|
99
|
-
return RNFS.stat(dirPath).then(stats => {
|
|
100
|
-
if (stats.isDirectory()) {
|
|
101
|
-
callback(new ReactNativeFileEntry(dirPath, true));
|
|
102
|
-
} else {
|
|
103
|
-
errorCallback(new Error('Path exists but is not a directory'));
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
} else if (options.create) {
|
|
107
|
-
return RNFS.mkdir(dirPath).then(() => {
|
|
108
|
-
callback(new ReactNativeFileEntry(dirPath, true));
|
|
109
|
-
});
|
|
110
|
-
} else {
|
|
111
|
-
errorCallback(new Error('Directory does not exist'));
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
.catch(errorCallback);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Get or create a file
|
|
119
|
-
*/
|
|
120
|
-
getFile(
|
|
121
|
-
path: string,
|
|
122
|
-
options: { create: boolean },
|
|
123
|
-
callback: (entry: IFileEntry) => void,
|
|
124
|
-
errorCallback: (error: any) => void
|
|
125
|
-
): void {
|
|
126
|
-
const filePath = `${this.fullPath}/${path}`;
|
|
127
|
-
|
|
128
|
-
RNFS.exists(filePath)
|
|
129
|
-
.then(exists => {
|
|
130
|
-
if (exists) {
|
|
131
|
-
return RNFS.stat(filePath).then(stats => {
|
|
132
|
-
if (!stats.isDirectory()) {
|
|
133
|
-
callback(new ReactNativeFileEntry(filePath, false));
|
|
134
|
-
} else {
|
|
135
|
-
errorCallback(new Error('Path exists but is a directory'));
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
} else if (options.create) {
|
|
139
|
-
// Create empty file
|
|
140
|
-
return RNFS.writeFile(filePath, '', 'utf8').then(() => {
|
|
141
|
-
callback(new ReactNativeFileEntry(filePath, false));
|
|
142
|
-
});
|
|
143
|
-
} else {
|
|
144
|
-
errorCallback(new Error('File does not exist'));
|
|
145
|
-
}
|
|
146
|
-
})
|
|
147
|
-
.catch(errorCallback);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Get file metadata
|
|
152
|
-
*/
|
|
153
|
-
file(callback: (file: File) => void, errorCallback: (error: any) => void): void {
|
|
154
|
-
RNFS.stat(this.fullPath)
|
|
155
|
-
.then(stats => {
|
|
156
|
-
// Create a File-like object with metadata
|
|
157
|
-
const fileObject: any = {
|
|
158
|
-
name: this.fullPath.split('/').pop() || '',
|
|
159
|
-
size: stats.size,
|
|
160
|
-
type: '', // RNFS doesn't provide MIME type directly
|
|
161
|
-
lastModified: new Date(stats.mtime).getTime(),
|
|
162
|
-
lastModifiedDate: new Date(stats.mtime),
|
|
163
|
-
path: this.fullPath,
|
|
164
|
-
};
|
|
165
|
-
callback(fileObject as File);
|
|
166
|
-
})
|
|
167
|
-
.catch(errorCallback);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Create a file writer for writing to the file
|
|
172
|
-
*/
|
|
173
|
-
createWriter(callback: (writer: any) => void, errorCallback: (error: any) => void): void {
|
|
174
|
-
// Create a FileWriter-like object
|
|
175
|
-
const writer = {
|
|
176
|
-
filePath: this.fullPath,
|
|
177
|
-
|
|
178
|
-
write: (data: string) => {
|
|
179
|
-
return RNFS.writeFile(this.fullPath, data, 'utf8');
|
|
180
|
-
},
|
|
181
|
-
|
|
182
|
-
append: (data: string) => {
|
|
183
|
-
return RNFS.appendFile(this.fullPath, data, 'utf8');
|
|
184
|
-
},
|
|
185
|
-
|
|
186
|
-
truncate: (size: number) => {
|
|
187
|
-
// Read file, truncate content, and write back
|
|
188
|
-
return RNFS.readFile(this.fullPath, 'utf8')
|
|
189
|
-
.then(content => {
|
|
190
|
-
const truncated = content.substring(0, size);
|
|
191
|
-
return RNFS.writeFile(this.fullPath, truncated, 'utf8');
|
|
192
|
-
});
|
|
193
|
-
},
|
|
194
|
-
|
|
195
|
-
seek: (offset: number) => {
|
|
196
|
-
// Not directly supported in RNFS
|
|
197
|
-
// This would need to be implemented with read/write operations
|
|
198
|
-
return Promise.resolve();
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
try {
|
|
203
|
-
callback(writer);
|
|
204
|
-
} catch (error) {
|
|
205
|
-
errorCallback(error);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|