@umituz/react-native-filesystem 2.1.20 → 2.1.22
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
CHANGED
|
@@ -11,7 +11,7 @@ import { downloadFile } from "./download.service";
|
|
|
11
11
|
import { clearCache, getDirectorySize } from "./cache.service";
|
|
12
12
|
import { generateFilePath } from "./file-path.service";
|
|
13
13
|
import { FileUtils } from "../../domain/entities/File";
|
|
14
|
-
import type {
|
|
14
|
+
import type { FileOperationResult } from "../../domain/entities/File";
|
|
15
15
|
|
|
16
16
|
export class FileSystemService {
|
|
17
17
|
static readFile = readFile;
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { File, Paths, Directory } from "expo-file-system";
|
|
6
6
|
import type { FileOperationResult } from "../../domain/entities/File";
|
|
7
7
|
import { FileUtils } from "../../domain/entities/File";
|
|
8
|
+
import { SUPPORTED_DOWNLOAD_EXTENSIONS, DEFAULT_DOWNLOAD_EXTENSION } from "./download.constants";
|
|
8
9
|
import type { DownloadProgressCallback, DownloadWithProgressResult } from "./download.types";
|
|
9
10
|
|
|
10
11
|
const hashUrl = (url: string) => {
|
|
@@ -14,8 +15,9 @@ const hashUrl = (url: string) => {
|
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
const getExt = (url: string) => {
|
|
17
|
-
const
|
|
18
|
-
|
|
18
|
+
const parts = url.split("?")[0]?.split(".") || [];
|
|
19
|
+
const ext = parts.length > 1 ? parts.pop()?.toLowerCase() || DEFAULT_DOWNLOAD_EXTENSION : DEFAULT_DOWNLOAD_EXTENSION;
|
|
20
|
+
return (SUPPORTED_DOWNLOAD_EXTENSIONS as readonly string[]).includes(ext) ? ext : DEFAULT_DOWNLOAD_EXTENSION;
|
|
19
21
|
};
|
|
20
22
|
|
|
21
23
|
const getCacheUri = (url: string, dir: string) => FileUtils.joinPaths(dir, `cached_${hashUrl(url)}.${getExt(url)}`);
|
|
@@ -25,7 +27,7 @@ export async function downloadFile(url: string, dest?: string): Promise<FileOper
|
|
|
25
27
|
const destination = dest ? new File(dest) : new File(Paths.document, FileUtils.generateUniqueFilename("download"));
|
|
26
28
|
const res = await File.downloadFileAsync(url, destination, { idempotent: true });
|
|
27
29
|
return { success: true, uri: res.uri };
|
|
28
|
-
} catch (e:
|
|
30
|
+
} catch (e: unknown) { return { success: false, error: e instanceof Error ? e.message : String(e) }; }
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
export async function downloadFileWithProgress(url: string, cacheDir: string, onProgress?: DownloadProgressCallback): Promise<DownloadWithProgressResult> {
|
|
@@ -60,7 +62,7 @@ export async function downloadFileWithProgress(url: string, cacheDir: string, on
|
|
|
60
62
|
new File(destUri).write(all);
|
|
61
63
|
|
|
62
64
|
return { success: true, uri: destUri, fromCache: false };
|
|
63
|
-
} catch (e:
|
|
65
|
+
} catch (e: unknown) { return { success: false, error: e instanceof Error ? e.message : String(e) }; }
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
export const isUrlCached = (url: string, dir: string) => new File(getCacheUri(url, dir)).exists;
|
|
@@ -11,8 +11,7 @@ export function blobToBase64(blob: Blob): Promise<string> {
|
|
|
11
11
|
const reader = new FileReader();
|
|
12
12
|
reader.onloadend = () => {
|
|
13
13
|
const result = reader.result as string;
|
|
14
|
-
|
|
15
|
-
const base64 = result.includes(",") ? result.split(",")[1] : result;
|
|
14
|
+
const base64 = result.includes(",") ? result.split(",")[1] ?? result : result;
|
|
16
15
|
resolve(base64);
|
|
17
16
|
};
|
|
18
17
|
reader.onerror = reject;
|