@unvired/react-native-unvired-sdk 0.0.23 → 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 CHANGED
@@ -1 +1 @@
1
- R-0.000.0023
1
+ R-0.000.0025
@@ -1,4 +1,44 @@
1
- import { IFileEntry, FileSystemEncoding } from './FileSystem.types';
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,3 +1,3 @@
1
1
  export { FileSystem } from './FileSystem';
2
- export type { IFileEntry, IDirectoryReader } from './FileSystem.types';
2
+ export type { IFileEntry, IDirectoryReader } from './FileSystem';
3
3
  export { default } from './FileSystem';
@@ -1,5 +1,5 @@
1
1
  import * as RNFS from 'react-native-fs';
2
- import { IFileEntry, IFileSystem, FileSystemEncoding } from '../FileSystem.types';
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
- import { RNFileEntry } from '../RNFileEntry';
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.types';
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@unvired/react-native-unvired-sdk",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "description": "Unvired SDK for React Native with logging, database, notifications, and file system support",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
@@ -1,4 +1,9 @@
1
- import { DeviceInfo, DatabaseManager, LocalStorage, logger, LogLevel, PushNotification, FileSystem } from '@unvired/react-native-unvired-sdk';
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
- return await pushNotification.getToken();
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,3 +0,0 @@
1
- // FileSystem.types.ts
2
- // Type definitions for file system module
3
- export {};
@@ -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
- }