rn-file-toolkit 1.0.7 → 1.0.8
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/README.md +218 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,10 +25,14 @@
|
|
|
25
25
|
- [Quick Start: `useDownload`](#-quick-start-usedownload)
|
|
26
26
|
- [Core APIs](#-core-apis)
|
|
27
27
|
- [Background Downloads](#background-downloads)
|
|
28
|
+
- [Download Controls](#download-controls)
|
|
28
29
|
- [Multipart Uploads](#multipart-uploads)
|
|
30
|
+
- [Queue Management](#queue-management)
|
|
29
31
|
- [File System (FS)](#file-system-fs)
|
|
30
32
|
- [Zip & Unzip Archives](#zip--unzip-archives)
|
|
33
|
+
- [Cache Management](#cache-management)
|
|
31
34
|
- [Media & Utilities](#media--utilities)
|
|
35
|
+
- [Event Listeners](#event-listeners)
|
|
32
36
|
- [API Reference](#-api-reference)
|
|
33
37
|
- [Expo Support](#-expo-support)
|
|
34
38
|
- [Contributing](#-contributing)
|
|
@@ -159,19 +163,54 @@ export default function DownloadScreen() {
|
|
|
159
163
|
For programmatic, queue-aware background downloads outside of React components.
|
|
160
164
|
|
|
161
165
|
```typescript
|
|
162
|
-
import { download
|
|
163
|
-
|
|
164
|
-
// Optimize global concurrency
|
|
165
|
-
setQueueOptions({ maxConcurrent: 3 });
|
|
166
|
+
import { download } from 'rn-file-toolkit';
|
|
166
167
|
|
|
167
168
|
const result = await download({
|
|
168
169
|
url: 'https://example.com/file.pdf',
|
|
170
|
+
fileName: 'report.pdf', // Optional custom filename
|
|
169
171
|
destination: 'documents', // 'downloads' | 'cache' | 'documents'
|
|
172
|
+
background: true, // Survive app suspension
|
|
173
|
+
headers: { Authorization: 'Bearer token' },
|
|
170
174
|
queue: true, // Join the managed queue
|
|
171
175
|
priority: 'high', // 'high' | 'normal'
|
|
172
|
-
|
|
176
|
+
downloadId: 'my-unique-id', // Optional custom ID for tracking
|
|
177
|
+
notificationTitle: 'Downloading report…', // Android notification
|
|
178
|
+
notificationDescription: 'Please wait',
|
|
179
|
+
checksum: { hash: 'abc123...', algorithm: 'sha256' }, // Verify integrity
|
|
180
|
+
retry: {
|
|
181
|
+
attempts: 3,
|
|
182
|
+
delay: 1000,
|
|
183
|
+
onRetry: (attempt, error) => console.warn(`Retry #${attempt}: ${error}`),
|
|
184
|
+
},
|
|
173
185
|
onProgress: (p) => console.log(`${p.percent.toFixed(1)}% downloaded`),
|
|
174
186
|
});
|
|
187
|
+
|
|
188
|
+
console.log(result.filePath); // Path to the downloaded file
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Download Controls
|
|
192
|
+
|
|
193
|
+
Pause, resume, or cancel any active download by its ID—works both inside and outside React components.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import {
|
|
197
|
+
download,
|
|
198
|
+
pauseDownload,
|
|
199
|
+
resumeDownload,
|
|
200
|
+
cancelDownload,
|
|
201
|
+
} from 'rn-file-toolkit';
|
|
202
|
+
|
|
203
|
+
// Start a download with a known ID
|
|
204
|
+
const result = download({
|
|
205
|
+
url: 'https://example.com/large-video.mp4',
|
|
206
|
+
downloadId: 'video-1',
|
|
207
|
+
destination: 'documents',
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Later… pause, resume, or cancel by ID
|
|
211
|
+
await pauseDownload('video-1');
|
|
212
|
+
await resumeDownload('video-1');
|
|
213
|
+
await cancelDownload('video-1');
|
|
175
214
|
```
|
|
176
215
|
|
|
177
216
|
### Multipart Uploads
|
|
@@ -185,21 +224,52 @@ const result = await upload({
|
|
|
185
224
|
url: 'https://api.example.com/v1/upload',
|
|
186
225
|
filePath: '/path/to/local/image.jpg',
|
|
187
226
|
fieldName: 'file',
|
|
227
|
+
headers: { Authorization: 'Bearer token' },
|
|
188
228
|
parameters: { userId: '123', folder: 'avatars' },
|
|
229
|
+
uploadId: 'upload-1', // Optional custom ID for tracking
|
|
189
230
|
onProgress: (percent) => console.log(`Uploading: ${percent}%`),
|
|
190
231
|
});
|
|
232
|
+
|
|
233
|
+
console.log(result.status); // HTTP status code
|
|
234
|
+
console.log(result.data); // Server response body
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Queue Management
|
|
238
|
+
|
|
239
|
+
Control download concurrency globally and inspect the queue state.
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
import { setQueueOptions, getQueueStatus } from 'rn-file-toolkit';
|
|
243
|
+
|
|
244
|
+
// Set the maximum number of simultaneous downloads
|
|
245
|
+
setQueueOptions({ maxConcurrent: 3 });
|
|
246
|
+
|
|
247
|
+
// Inspect the queue at any time
|
|
248
|
+
const status = getQueueStatus();
|
|
249
|
+
console.log(status.active); // Currently downloading
|
|
250
|
+
console.log(status.pending); // Waiting in queue
|
|
251
|
+
console.log(status.maxConcurrent); // Concurrency cap
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
You can also retrieve all downloads currently running in the background (useful after app re-launch):
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
import { getBackgroundDownloads } from 'rn-file-toolkit';
|
|
258
|
+
|
|
259
|
+
const active = await getBackgroundDownloads();
|
|
260
|
+
console.log(active); // Array of background download descriptors
|
|
191
261
|
```
|
|
192
262
|
|
|
193
263
|
### File System (FS)
|
|
194
264
|
|
|
195
|
-
Perform native filesystem operations securely.
|
|
265
|
+
Perform native filesystem operations securely. All methods are available both as the namespaced `fs` object and as standalone named exports.
|
|
196
266
|
|
|
197
267
|
```typescript
|
|
198
268
|
import { fs } from 'rn-file-toolkit';
|
|
199
269
|
|
|
200
270
|
// Check & Inspect
|
|
201
271
|
const exists = await fs.exists('/path/to/data.json');
|
|
202
|
-
const stats = await fs.stat('/path/to/data.json'); // { size, modified, isDir }
|
|
272
|
+
const stats = await fs.stat('/path/to/data.json'); // { path, name, size, modified, isDir }
|
|
203
273
|
|
|
204
274
|
// Read & Write
|
|
205
275
|
await fs.writeFile('/path/to/data.txt', 'Hello World', 'utf8'); // Also supports 'base64'
|
|
@@ -213,18 +283,42 @@ await fs.moveFile('/path/old.txt', '/path/new.txt');
|
|
|
213
283
|
await fs.deleteFile('/path/unwanted.txt');
|
|
214
284
|
```
|
|
215
285
|
|
|
286
|
+
> **Tip:** You can also import each FS method individually:
|
|
287
|
+
> ```typescript
|
|
288
|
+
> import { exists, stat, readFile, writeFile, copyFile, moveFile, deleteFile, mkdir, ls } from 'rn-file-toolkit';
|
|
289
|
+
> ```
|
|
290
|
+
|
|
216
291
|
### Zip & Unzip Archives
|
|
217
292
|
|
|
218
|
-
Compress and extract archives directly on the device.
|
|
293
|
+
Compress and extract archives directly on the device using native `java.util.zip` (Android) and `zlib` (iOS).
|
|
219
294
|
|
|
220
295
|
```typescript
|
|
221
296
|
import { unzip, zip } from 'rn-file-toolkit';
|
|
222
297
|
|
|
223
298
|
// Extract a downloaded zip
|
|
224
|
-
await unzip('/path/to/bundle.zip', '/path/to/extract-folder');
|
|
299
|
+
const unzipResult = await unzip('/path/to/bundle.zip', '/path/to/extract-folder');
|
|
300
|
+
console.log(unzipResult.files); // List of extracted file paths
|
|
225
301
|
|
|
226
302
|
// Compress user data before uploading
|
|
227
|
-
await zip('/path/to/user-data-folder', '/path/to/backup.zip');
|
|
303
|
+
const zipResult = await zip('/path/to/user-data-folder', '/path/to/backup.zip');
|
|
304
|
+
console.log(zipResult.zipPath); // Path to the created archive
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Cache Management
|
|
308
|
+
|
|
309
|
+
Inspect and clear files stored in the cache directory.
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
import { getCachedFiles, clearCache } from 'rn-file-toolkit';
|
|
313
|
+
|
|
314
|
+
// List all cached files with metadata
|
|
315
|
+
const cache = await getCachedFiles();
|
|
316
|
+
cache.files?.forEach((f) => {
|
|
317
|
+
console.log(f.fileName, f.filePath, f.size, f.modifiedAt);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// Wipe the entire cache directory
|
|
321
|
+
await clearCache();
|
|
228
322
|
```
|
|
229
323
|
|
|
230
324
|
### Media & Utilities
|
|
@@ -239,18 +333,27 @@ import {
|
|
|
239
333
|
openFile,
|
|
240
334
|
} from 'rn-file-toolkit';
|
|
241
335
|
|
|
242
|
-
// Base64 to File
|
|
336
|
+
// Base64 to File (accepts raw base64 or data URIs)
|
|
243
337
|
await saveBase64AsFile({
|
|
244
338
|
base64Data: 'data:image/png;base64,...',
|
|
245
339
|
destination: 'documents',
|
|
246
340
|
fileName: 'image.png',
|
|
247
341
|
});
|
|
248
342
|
|
|
249
|
-
// URL to Base64 (
|
|
250
|
-
const b64 = await urlToBase64({
|
|
343
|
+
// URL to Base64 (great for caching small images)
|
|
344
|
+
const b64 = await urlToBase64({
|
|
345
|
+
url: 'https://example.com/icon.png',
|
|
346
|
+
headers: { Authorization: 'Bearer token' }, // Optional
|
|
347
|
+
});
|
|
348
|
+
console.log(b64.mimeType); // e.g. 'image/png'
|
|
349
|
+
console.log(b64.dataUri); // Ready-to-use data URI string
|
|
251
350
|
|
|
252
351
|
// Native Share Sheet
|
|
253
|
-
await shareFile({
|
|
352
|
+
await shareFile({
|
|
353
|
+
filePath: '/path/to/report.pdf',
|
|
354
|
+
title: 'Share report', // Optional
|
|
355
|
+
subject: 'Monthly report', // Optional (email subject)
|
|
356
|
+
});
|
|
254
357
|
|
|
255
358
|
// Open with default system app
|
|
256
359
|
await openFile({
|
|
@@ -259,19 +362,111 @@ await openFile({
|
|
|
259
362
|
});
|
|
260
363
|
```
|
|
261
364
|
|
|
365
|
+
### Event Listeners
|
|
366
|
+
|
|
367
|
+
Subscribe to global download and upload lifecycle events. Each listener returns an unsubscribe function.
|
|
368
|
+
|
|
369
|
+
```typescript
|
|
370
|
+
import {
|
|
371
|
+
onDownloadComplete,
|
|
372
|
+
onDownloadError,
|
|
373
|
+
onDownloadRetry,
|
|
374
|
+
onUploadProgress,
|
|
375
|
+
} from 'rn-file-toolkit';
|
|
376
|
+
|
|
377
|
+
// Fires when any download finishes successfully
|
|
378
|
+
const unsub1 = onDownloadComplete((event) => {
|
|
379
|
+
console.log('Download done:', event);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// Fires when any download fails
|
|
383
|
+
const unsub2 = onDownloadError((event) => {
|
|
384
|
+
console.error('Download failed:', event);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// Fires on each retry attempt (when retry is configured)
|
|
388
|
+
const unsub3 = onDownloadRetry((event) => {
|
|
389
|
+
console.warn(`Retry #${event.attempt}:`, event.error);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// Fires on upload progress updates
|
|
393
|
+
const unsub4 = onUploadProgress((event) => {
|
|
394
|
+
console.log(`Upload ${event.uploadId}: ${event.progress}%`);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
// Clean up when done
|
|
398
|
+
unsub1();
|
|
399
|
+
unsub2();
|
|
400
|
+
unsub3();
|
|
401
|
+
unsub4();
|
|
402
|
+
```
|
|
403
|
+
|
|
262
404
|
---
|
|
263
405
|
|
|
264
406
|
## 📚 API Reference
|
|
265
407
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
|
269
|
-
|
|
|
270
|
-
| `
|
|
271
|
-
| `
|
|
272
|
-
| `
|
|
273
|
-
|
|
274
|
-
|
|
408
|
+
### Types & Interfaces
|
|
409
|
+
|
|
410
|
+
| Interface | Key Properties | Description |
|
|
411
|
+
| :--- | :--- | :--- |
|
|
412
|
+
| `DownloadOptions` | `url`, `fileName`, `destination`, `background`, `headers`, `queue`, `priority`, `downloadId`, `checksum`, `retry`, `onProgress`, `notificationTitle`, `notificationDescription` | Full configuration for downloading a file. |
|
|
413
|
+
| `UploadOptions` | `url`, `filePath`, `fieldName`, `headers`, `parameters`, `uploadId`, `onProgress` | Configuration for multipart uploads. |
|
|
414
|
+
| `ProgressInfo` | `percent`, `bytesDownloaded`, `totalBytes`, `speedBps`, `etaSeconds` | Rich real-time download progress payload. |
|
|
415
|
+
| `DownloadResult` | `success`, `filePath`, `downloadId`, `error` | Result returned after a download completes. |
|
|
416
|
+
| `UploadResult` | `success`, `status`, `data`, `uploadId`, `error` | Result returned after an upload completes. |
|
|
417
|
+
| `ActionResult` | `success`, `error` | Generic result for actions like pause/resume/cancel. |
|
|
418
|
+
| `UseDownloadReturn` | `start`, `pause`, `resume`, `cancel`, `status`, `progress`, `result`, `downloadId` | Hook state and control methods. |
|
|
419
|
+
| `FsApi` | `exists`, `stat`, `readFile`, `writeFile`, `copyFile`, `moveFile`, `deleteFile`, `mkdir`, `ls` | Namespaced filesystem API. |
|
|
420
|
+
| `FsStat` | `path`, `name`, `size`, `modified`, `isDir` | Output of the filesystem `stat` method. |
|
|
421
|
+
| `FsEncoding` | `'utf8'` \| `'base64'` | Encoding used for read/write operations. |
|
|
422
|
+
| `QueueOptions` | `maxConcurrent` | Configuration for the download queue. |
|
|
423
|
+
| `QueueStatus` | `active`, `pending`, `maxConcurrent` | Snapshot of the current queue state. |
|
|
424
|
+
| `CachedFile` | `fileName`, `filePath`, `size`, `modifiedAt` | Metadata for a single cached file. |
|
|
425
|
+
| `CacheResult` | `success`, `files`, `error` | Result of `getCachedFiles()`. |
|
|
426
|
+
| `SaveBase64Options` | `base64Data`, `fileName`, `destination` | Options for saving a base64 string to a file. |
|
|
427
|
+
| `SaveBase64Result` | `success`, `filePath`, `error` | Result of `saveBase64AsFile()`. |
|
|
428
|
+
| `UrlToBase64Options` | `url`, `headers` | Options for converting a URL to base64. |
|
|
429
|
+
| `UrlToBase64Result` | `success`, `base64`, `mimeType`, `dataUri`, `error` | Result of `urlToBase64()`. |
|
|
430
|
+
| `ShareFileOptions` | `filePath`, `title`, `subject` | Options for the native share sheet. |
|
|
431
|
+
| `OpenFileOptions` | `filePath`, `mimeType` | Options for opening a file with the system default app. |
|
|
432
|
+
| `UnzipResult` | `success`, `destDir`, `files`, `error` | Result of `unzip()`. |
|
|
433
|
+
| `ZipResult` | `success`, `zipPath`, `error` | Result of `zip()`. |
|
|
434
|
+
|
|
435
|
+
### Exported Functions
|
|
436
|
+
|
|
437
|
+
| Function | Signature | Description |
|
|
438
|
+
| :--- | :--- | :--- |
|
|
439
|
+
| `download` | `(options: DownloadOptions) => Promise<DownloadResult>` | Download a file (supports queue, background, retries). |
|
|
440
|
+
| `upload` | `(options: UploadOptions) => Promise<UploadResult>` | Multipart upload a file. |
|
|
441
|
+
| `pauseDownload` | `(id: string) => Promise<ActionResult>` | Pause an active download by ID. |
|
|
442
|
+
| `resumeDownload` | `(id: string) => Promise<ActionResult>` | Resume a paused download by ID. |
|
|
443
|
+
| `cancelDownload` | `(id: string) => Promise<ActionResult>` | Cancel a download by ID. |
|
|
444
|
+
| `setQueueOptions` | `(options: QueueOptions) => void` | Set global queue concurrency. |
|
|
445
|
+
| `getQueueStatus` | `() => QueueStatus` | Get current queue state (active/pending counts). |
|
|
446
|
+
| `getBackgroundDownloads` | `() => Promise<any>` | Retrieve active background download descriptors. |
|
|
447
|
+
| `getCachedFiles` | `() => Promise<CacheResult>` | List all files in the cache directory. |
|
|
448
|
+
| `clearCache` | `() => Promise<ActionResult>` | Delete all cached files. |
|
|
449
|
+
| `deleteFile` | `(path: string) => Promise<ActionResult>` | Delete a single file by path. |
|
|
450
|
+
| `exists` | `(path: string) => Promise<boolean>` | Check if a file or directory exists. |
|
|
451
|
+
| `stat` | `(path: string) => Promise<FsStat>` | Get metadata for a file or directory. |
|
|
452
|
+
| `readFile` | `(path: string, encoding?: FsEncoding) => Promise<string>` | Read file contents as a string. |
|
|
453
|
+
| `writeFile` | `(path: string, data: string, encoding?: FsEncoding) => Promise<void>` | Write a string to a file. |
|
|
454
|
+
| `copyFile` | `(from: string, to: string) => Promise<void>` | Copy a file. |
|
|
455
|
+
| `moveFile` | `(from: string, to: string) => Promise<void>` | Move or rename a file. |
|
|
456
|
+
| `mkdir` | `(path: string) => Promise<void>` | Create a directory (recursive). |
|
|
457
|
+
| `ls` | `(path: string) => Promise<string[]>` | List directory contents. |
|
|
458
|
+
| `unzip` | `(src: string, dest: string) => Promise<UnzipResult>` | Extract a zip archive. |
|
|
459
|
+
| `zip` | `(src: string, dest: string) => Promise<ZipResult>` | Compress a folder into a zip archive. |
|
|
460
|
+
| `saveBase64AsFile` | `(options: SaveBase64Options) => Promise<SaveBase64Result>` | Save a base64 string as a file. |
|
|
461
|
+
| `urlToBase64` | `(options: UrlToBase64Options) => Promise<UrlToBase64Result>` | Fetch a URL and return its content as base64. |
|
|
462
|
+
| `shareFile` | `(options: ShareFileOptions) => Promise<ShareFileResult>` | Open the native share sheet for a file. |
|
|
463
|
+
| `openFile` | `(options: OpenFileOptions) => Promise<OpenFileResult>` | Open a file with the system default app. |
|
|
464
|
+
| `onDownloadComplete` | `(cb) => () => void` | Subscribe to download completion events. |
|
|
465
|
+
| `onDownloadError` | `(cb) => () => void` | Subscribe to download error events. |
|
|
466
|
+
| `onDownloadRetry` | `(cb) => () => void` | Subscribe to download retry events. |
|
|
467
|
+
| `onUploadProgress` | `(cb) => () => void` | Subscribe to upload progress events. |
|
|
468
|
+
| `useDownload` | `() => UseDownloadReturn` | React hook for managing a download with state. |
|
|
469
|
+
| `fs` | `FsApi` | Namespaced object grouping all filesystem methods. |
|
|
275
470
|
|
|
276
471
|
---
|
|
277
472
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rn-file-toolkit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "The ultimate native file management toolkit for React Native. Features background downloads, resumable uploads, filesystem operations, queues, zip/unzip, and media utilities with zero third-party dependencies.",
|
|
5
5
|
"main": "./lib/commonjs/index.js",
|
|
6
6
|
"module": "./lib/module/index.js",
|