rn-file-toolkit 1.0.8 → 1.0.9
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/FileToolkit.podspec +1 -0
- package/README.md +149 -3
- package/android/src/main/java/com/filetoolkit/FileToolkitModule.kt +273 -0
- package/ios/FileToolkit.mm +220 -0
- package/lib/commonjs/NativeFileToolkit.js.map +1 -1
- package/lib/commonjs/index.js +137 -3
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/NativeFileToolkit.js.map +1 -1
- package/lib/module/index.js +129 -3
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/commonjs/src/NativeFileToolkit.d.ts +6 -0
- package/lib/typescript/commonjs/src/NativeFileToolkit.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/index.d.ts +67 -0
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
- package/lib/typescript/module/src/NativeFileToolkit.d.ts +6 -0
- package/lib/typescript/module/src/NativeFileToolkit.d.ts.map +1 -1
- package/lib/typescript/module/src/index.d.ts +67 -0
- package/lib/typescript/module/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeFileToolkit.ts +6 -0
- package/src/index.tsx +183 -2
package/src/index.tsx
CHANGED
|
@@ -136,6 +136,56 @@ export interface FsStat {
|
|
|
136
136
|
isDir: boolean;
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
+
export interface DiskSpaceResult {
|
|
140
|
+
success: boolean;
|
|
141
|
+
freeBytes?: number;
|
|
142
|
+
totalBytes?: number;
|
|
143
|
+
error?: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type HashAlgorithm = 'md5' | 'sha1' | 'sha256';
|
|
147
|
+
|
|
148
|
+
export interface HashResult {
|
|
149
|
+
success: boolean;
|
|
150
|
+
hash?: string;
|
|
151
|
+
error?: string;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface Cookie {
|
|
155
|
+
name: string;
|
|
156
|
+
value: string;
|
|
157
|
+
domain: string;
|
|
158
|
+
path: string;
|
|
159
|
+
expiresDate?: number;
|
|
160
|
+
isSecure?: boolean;
|
|
161
|
+
isHTTPOnly?: boolean;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface CookiesResult {
|
|
165
|
+
success: boolean;
|
|
166
|
+
cookies?: Cookie[];
|
|
167
|
+
error?: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface MediaStoreOptions {
|
|
171
|
+
filePath: string;
|
|
172
|
+
mediaType?: 'image' | 'video' | 'audio' | 'download';
|
|
173
|
+
album?: string;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface MediaStoreResult {
|
|
177
|
+
success: boolean;
|
|
178
|
+
uri?: string;
|
|
179
|
+
error?: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface SessionApi {
|
|
183
|
+
add: (sessionId: string, filePath: string) => void;
|
|
184
|
+
get: (sessionId: string) => string[];
|
|
185
|
+
clear: (sessionId: string) => Promise<ActionResult>;
|
|
186
|
+
clearAll: () => Promise<ActionResult>;
|
|
187
|
+
}
|
|
188
|
+
|
|
139
189
|
export interface FsApi {
|
|
140
190
|
exists: (filePath: string) => Promise<boolean>;
|
|
141
191
|
stat: (filePath: string) => Promise<FsStat>;
|
|
@@ -145,11 +195,18 @@ export interface FsApi {
|
|
|
145
195
|
data: string,
|
|
146
196
|
encoding?: FsEncoding
|
|
147
197
|
) => Promise<void>;
|
|
198
|
+
appendFile: (
|
|
199
|
+
filePath: string,
|
|
200
|
+
data: string,
|
|
201
|
+
encoding?: FsEncoding
|
|
202
|
+
) => Promise<void>;
|
|
148
203
|
copyFile: (fromPath: string, toPath: string) => Promise<void>;
|
|
149
204
|
moveFile: (fromPath: string, toPath: string) => Promise<void>;
|
|
150
205
|
deleteFile: (filePath: string) => Promise<void>;
|
|
151
206
|
mkdir: (dirPath: string) => Promise<void>;
|
|
152
207
|
ls: (dirPath: string) => Promise<string[]>;
|
|
208
|
+
df: () => Promise<DiskSpaceResult>;
|
|
209
|
+
hash: (filePath: string, algorithm?: HashAlgorithm) => Promise<HashResult>;
|
|
153
210
|
}
|
|
154
211
|
|
|
155
212
|
export interface QueueOptions {
|
|
@@ -226,8 +283,8 @@ const _globalQueue = new DownloadQueue();
|
|
|
226
283
|
|
|
227
284
|
function _generateId(): string {
|
|
228
285
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
229
|
-
const r = (Math.random() * 16)
|
|
230
|
-
const v = c === 'x' ? r : (r
|
|
286
|
+
const r = Math.floor(Math.random() * 16);
|
|
287
|
+
const v = c === 'x' ? r : (r % 4) + 8;
|
|
231
288
|
return v.toString(16);
|
|
232
289
|
});
|
|
233
290
|
}
|
|
@@ -468,11 +525,125 @@ export async function ls(path: string): Promise<string[]> {
|
|
|
468
525
|
return res.entries || [];
|
|
469
526
|
}
|
|
470
527
|
|
|
528
|
+
export async function df(): Promise<DiskSpaceResult> {
|
|
529
|
+
try {
|
|
530
|
+
return await (FileToolkitModule as any).df();
|
|
531
|
+
} catch (err: any) {
|
|
532
|
+
return { success: false, error: err.message || 'DF_ERROR' };
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
export async function appendFile(
|
|
537
|
+
path: string,
|
|
538
|
+
data: string,
|
|
539
|
+
enc: FsEncoding = 'utf8'
|
|
540
|
+
): Promise<void> {
|
|
541
|
+
const res = await (FileToolkitModule as any).appendFile(path, data, enc);
|
|
542
|
+
_ensure(res, 'APPEND_ERROR');
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export async function hash(
|
|
546
|
+
path: string,
|
|
547
|
+
algorithm: HashAlgorithm = 'md5'
|
|
548
|
+
): Promise<HashResult> {
|
|
549
|
+
try {
|
|
550
|
+
return await (FileToolkitModule as any).hash(path, algorithm);
|
|
551
|
+
} catch (err: any) {
|
|
552
|
+
return { success: false, error: err.message || 'HASH_ERROR' };
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export async function getCookies(domain: string): Promise<CookiesResult> {
|
|
557
|
+
try {
|
|
558
|
+
return await (FileToolkitModule as any).getCookies(domain);
|
|
559
|
+
} catch (err: any) {
|
|
560
|
+
return { success: false, error: err.message || 'GET_COOKIES_ERROR' };
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
export async function clearCookies(domain: string = ''): Promise<ActionResult> {
|
|
565
|
+
try {
|
|
566
|
+
return await (FileToolkitModule as any).clearCookies(domain);
|
|
567
|
+
} catch (err: any) {
|
|
568
|
+
return { success: false, error: err.message || 'CLEAR_COOKIES_ERROR' };
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
export async function saveToMediaStore(
|
|
573
|
+
opts: MediaStoreOptions
|
|
574
|
+
): Promise<MediaStoreResult> {
|
|
575
|
+
try {
|
|
576
|
+
return await (FileToolkitModule as any).saveToMediaStore({
|
|
577
|
+
filePath: opts.filePath,
|
|
578
|
+
mediaType: opts.mediaType ?? 'download',
|
|
579
|
+
album: opts.album,
|
|
580
|
+
});
|
|
581
|
+
} catch (err: any) {
|
|
582
|
+
return { success: false, error: err.message || 'MEDIA_STORE_ERROR' };
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// ─── Session Management (JS-only) ──────────────────────────────────────────
|
|
587
|
+
|
|
588
|
+
const _sessionRegistry = new Map<string, Set<string>>();
|
|
589
|
+
|
|
590
|
+
function _sessionAdd(sessionId: string, filePath: string): void {
|
|
591
|
+
let set = _sessionRegistry.get(sessionId);
|
|
592
|
+
if (!set) {
|
|
593
|
+
set = new Set();
|
|
594
|
+
_sessionRegistry.set(sessionId, set);
|
|
595
|
+
}
|
|
596
|
+
set.add(filePath);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function _sessionGet(sessionId: string): string[] {
|
|
600
|
+
const set = _sessionRegistry.get(sessionId);
|
|
601
|
+
return set ? Array.from(set) : [];
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
async function _sessionClear(sessionId: string): Promise<ActionResult> {
|
|
605
|
+
const set = _sessionRegistry.get(sessionId);
|
|
606
|
+
if (!set) return { success: true };
|
|
607
|
+
const errors: string[] = [];
|
|
608
|
+
for (const filePath of set) {
|
|
609
|
+
const res = await deleteFile(filePath);
|
|
610
|
+
if (!res.success && res.error) errors.push(res.error);
|
|
611
|
+
}
|
|
612
|
+
_sessionRegistry.delete(sessionId);
|
|
613
|
+
return errors.length > 0
|
|
614
|
+
? { success: false, error: errors.join('; ') }
|
|
615
|
+
: { success: true };
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
async function _sessionClearAll(): Promise<ActionResult> {
|
|
619
|
+
const errors: string[] = [];
|
|
620
|
+
for (const [sessionId] of _sessionRegistry) {
|
|
621
|
+
const res = await _sessionClear(sessionId);
|
|
622
|
+
if (!res.success && res.error) errors.push(res.error);
|
|
623
|
+
}
|
|
624
|
+
return errors.length > 0
|
|
625
|
+
? { success: false, error: errors.join('; ') }
|
|
626
|
+
: { success: true };
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export const session: SessionApi = {
|
|
630
|
+
add: _sessionAdd,
|
|
631
|
+
get: _sessionGet,
|
|
632
|
+
clear: _sessionClear,
|
|
633
|
+
clearAll: _sessionClearAll,
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
export const cookies = {
|
|
637
|
+
get: getCookies,
|
|
638
|
+
clear: clearCookies,
|
|
639
|
+
};
|
|
640
|
+
|
|
471
641
|
export const fs: FsApi = {
|
|
472
642
|
exists,
|
|
473
643
|
stat,
|
|
474
644
|
readFile,
|
|
475
645
|
writeFile,
|
|
646
|
+
appendFile,
|
|
476
647
|
copyFile,
|
|
477
648
|
moveFile,
|
|
478
649
|
deleteFile: async (p) => {
|
|
@@ -480,6 +651,8 @@ export const fs: FsApi = {
|
|
|
480
651
|
},
|
|
481
652
|
mkdir,
|
|
482
653
|
ls,
|
|
654
|
+
df,
|
|
655
|
+
hash,
|
|
483
656
|
};
|
|
484
657
|
|
|
485
658
|
export function onDownloadComplete(cb: any) {
|
|
@@ -676,11 +849,19 @@ export default {
|
|
|
676
849
|
stat,
|
|
677
850
|
readFile,
|
|
678
851
|
writeFile,
|
|
852
|
+
appendFile,
|
|
679
853
|
copyFile,
|
|
680
854
|
moveFile,
|
|
681
855
|
mkdir,
|
|
682
856
|
ls,
|
|
857
|
+
df,
|
|
858
|
+
hash,
|
|
859
|
+
getCookies,
|
|
860
|
+
clearCookies,
|
|
861
|
+
saveToMediaStore,
|
|
683
862
|
fs,
|
|
863
|
+
cookies,
|
|
864
|
+
session,
|
|
684
865
|
useDownload,
|
|
685
866
|
unzip,
|
|
686
867
|
zip,
|