@umituz/react-native-filesystem 1.2.4 → 1.2.6
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/package.json +1 -1
- package/src/infrastructure/services/directory.service.ts +1 -1
- package/src/infrastructure/services/download.service.ts +1 -1
- package/src/infrastructure/services/file-info.service.ts +1 -1
- package/src/infrastructure/services/file-manager.service.ts +1 -1
- package/src/infrastructure/services/file-reader.service.ts +36 -2
- package/src/infrastructure/services/file-writer.service.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-filesystem",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "Domain-Driven Design filesystem utilities for React Native apps with build-time module loading and runtime file operations",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Single Responsibility: Download files from URLs
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import * as FileSystem from "expo-file-system
|
|
6
|
+
import * as FileSystem from "expo-file-system";
|
|
7
7
|
import type { FileOperationResult } from "../../domain/entities/File";
|
|
8
8
|
import { getDirectoryPath } from "./directory.service";
|
|
9
9
|
import { FileUtils } from "../../domain/entities/File";
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Single Responsibility: Read files from device storage
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import * as FileSystem from "expo-file-system
|
|
6
|
+
import * as FileSystem from "expo-file-system";
|
|
7
7
|
import type { FileEncoding } from "../../domain/entities/File";
|
|
8
8
|
import { getEncodingType } from "./encoding.service";
|
|
9
9
|
|
|
@@ -15,9 +15,26 @@ export async function readFile(
|
|
|
15
15
|
encoding: FileEncoding = "utf8",
|
|
16
16
|
): Promise<string | null> {
|
|
17
17
|
try {
|
|
18
|
+
// For file:// URLs, try fetch first (works in React Native)
|
|
19
|
+
if (uri.startsWith("file://")) {
|
|
20
|
+
try {
|
|
21
|
+
const response = await fetch(uri);
|
|
22
|
+
if (response.ok) {
|
|
23
|
+
if (encoding === "base64") {
|
|
24
|
+
const blob = await response.blob();
|
|
25
|
+
return await blobToBase64(blob);
|
|
26
|
+
}
|
|
27
|
+
return await response.text();
|
|
28
|
+
}
|
|
29
|
+
} catch (fetchError) {
|
|
30
|
+
// Fall through to FileSystem API
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Use FileSystem API as fallback
|
|
18
35
|
const encodingType = getEncodingType(encoding);
|
|
19
36
|
const content = await FileSystem.readAsStringAsync(uri, {
|
|
20
|
-
encoding: encodingType as
|
|
37
|
+
encoding: encodingType as any,
|
|
21
38
|
});
|
|
22
39
|
return content;
|
|
23
40
|
} catch (error) {
|
|
@@ -25,6 +42,23 @@ export async function readFile(
|
|
|
25
42
|
}
|
|
26
43
|
}
|
|
27
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Convert blob to base64 string
|
|
47
|
+
*/
|
|
48
|
+
function blobToBase64(blob: Blob): Promise<string> {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
const reader = new FileReader();
|
|
51
|
+
reader.onloadend = () => {
|
|
52
|
+
const result = reader.result as string;
|
|
53
|
+
// Remove data URL prefix if present
|
|
54
|
+
const base64 = result.includes(",") ? result.split(",")[1] : result;
|
|
55
|
+
resolve(base64);
|
|
56
|
+
};
|
|
57
|
+
reader.onerror = reject;
|
|
58
|
+
reader.readAsDataURL(blob);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
28
62
|
/**
|
|
29
63
|
* Read file as base64 string
|
|
30
64
|
*/
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Single Responsibility: Write files to device storage
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import * as FileSystem from "expo-file-system
|
|
6
|
+
import * as FileSystem from "expo-file-system";
|
|
7
7
|
import type { FileEncoding, FileOperationResult } from "../../domain/entities/File";
|
|
8
8
|
import { getEncodingType } from "./encoding.service";
|
|
9
9
|
|
|
@@ -18,7 +18,7 @@ export async function writeFile(
|
|
|
18
18
|
try {
|
|
19
19
|
const encodingType = getEncodingType(encoding);
|
|
20
20
|
await FileSystem.writeAsStringAsync(uri, content, {
|
|
21
|
-
encoding: encodingType as
|
|
21
|
+
encoding: encodingType as any,
|
|
22
22
|
});
|
|
23
23
|
return { success: true, uri };
|
|
24
24
|
} catch (error) {
|