rn-file-toolkit 1.0.6 → 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 +219 -23
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
[](https://www.typescriptlang.org/)
|
|
8
8
|
[](https://github.com/chavan-labs/rn-file-toolkit)
|
|
9
9
|
[](https://github.com/chavan-labs/rn-file-toolkit/blob/main/LICENSE)
|
|
10
|
+
[](https://makeapullrequest.com)
|
|
10
11
|
</div>
|
|
11
12
|
|
|
12
13
|
---
|
|
@@ -24,10 +25,14 @@
|
|
|
24
25
|
- [Quick Start: `useDownload`](#-quick-start-usedownload)
|
|
25
26
|
- [Core APIs](#-core-apis)
|
|
26
27
|
- [Background Downloads](#background-downloads)
|
|
28
|
+
- [Download Controls](#download-controls)
|
|
27
29
|
- [Multipart Uploads](#multipart-uploads)
|
|
30
|
+
- [Queue Management](#queue-management)
|
|
28
31
|
- [File System (FS)](#file-system-fs)
|
|
29
32
|
- [Zip & Unzip Archives](#zip--unzip-archives)
|
|
33
|
+
- [Cache Management](#cache-management)
|
|
30
34
|
- [Media & Utilities](#media--utilities)
|
|
35
|
+
- [Event Listeners](#event-listeners)
|
|
31
36
|
- [API Reference](#-api-reference)
|
|
32
37
|
- [Expo Support](#-expo-support)
|
|
33
38
|
- [Contributing](#-contributing)
|
|
@@ -158,19 +163,54 @@ export default function DownloadScreen() {
|
|
|
158
163
|
For programmatic, queue-aware background downloads outside of React components.
|
|
159
164
|
|
|
160
165
|
```typescript
|
|
161
|
-
import { download
|
|
162
|
-
|
|
163
|
-
// Optimize global concurrency
|
|
164
|
-
setQueueOptions({ maxConcurrent: 3 });
|
|
166
|
+
import { download } from 'rn-file-toolkit';
|
|
165
167
|
|
|
166
168
|
const result = await download({
|
|
167
169
|
url: 'https://example.com/file.pdf',
|
|
170
|
+
fileName: 'report.pdf', // Optional custom filename
|
|
168
171
|
destination: 'documents', // 'downloads' | 'cache' | 'documents'
|
|
172
|
+
background: true, // Survive app suspension
|
|
173
|
+
headers: { Authorization: 'Bearer token' },
|
|
169
174
|
queue: true, // Join the managed queue
|
|
170
175
|
priority: 'high', // 'high' | 'normal'
|
|
171
|
-
|
|
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
|
+
},
|
|
172
185
|
onProgress: (p) => console.log(`${p.percent.toFixed(1)}% downloaded`),
|
|
173
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');
|
|
174
214
|
```
|
|
175
215
|
|
|
176
216
|
### Multipart Uploads
|
|
@@ -184,21 +224,52 @@ const result = await upload({
|
|
|
184
224
|
url: 'https://api.example.com/v1/upload',
|
|
185
225
|
filePath: '/path/to/local/image.jpg',
|
|
186
226
|
fieldName: 'file',
|
|
227
|
+
headers: { Authorization: 'Bearer token' },
|
|
187
228
|
parameters: { userId: '123', folder: 'avatars' },
|
|
229
|
+
uploadId: 'upload-1', // Optional custom ID for tracking
|
|
188
230
|
onProgress: (percent) => console.log(`Uploading: ${percent}%`),
|
|
189
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
|
|
190
261
|
```
|
|
191
262
|
|
|
192
263
|
### File System (FS)
|
|
193
264
|
|
|
194
|
-
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.
|
|
195
266
|
|
|
196
267
|
```typescript
|
|
197
268
|
import { fs } from 'rn-file-toolkit';
|
|
198
269
|
|
|
199
270
|
// Check & Inspect
|
|
200
271
|
const exists = await fs.exists('/path/to/data.json');
|
|
201
|
-
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 }
|
|
202
273
|
|
|
203
274
|
// Read & Write
|
|
204
275
|
await fs.writeFile('/path/to/data.txt', 'Hello World', 'utf8'); // Also supports 'base64'
|
|
@@ -212,18 +283,42 @@ await fs.moveFile('/path/old.txt', '/path/new.txt');
|
|
|
212
283
|
await fs.deleteFile('/path/unwanted.txt');
|
|
213
284
|
```
|
|
214
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
|
+
|
|
215
291
|
### Zip & Unzip Archives
|
|
216
292
|
|
|
217
|
-
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).
|
|
218
294
|
|
|
219
295
|
```typescript
|
|
220
296
|
import { unzip, zip } from 'rn-file-toolkit';
|
|
221
297
|
|
|
222
298
|
// Extract a downloaded zip
|
|
223
|
-
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
|
|
224
301
|
|
|
225
302
|
// Compress user data before uploading
|
|
226
|
-
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();
|
|
227
322
|
```
|
|
228
323
|
|
|
229
324
|
### Media & Utilities
|
|
@@ -238,18 +333,27 @@ import {
|
|
|
238
333
|
openFile,
|
|
239
334
|
} from 'rn-file-toolkit';
|
|
240
335
|
|
|
241
|
-
// Base64 to File
|
|
336
|
+
// Base64 to File (accepts raw base64 or data URIs)
|
|
242
337
|
await saveBase64AsFile({
|
|
243
338
|
base64Data: 'data:image/png;base64,...',
|
|
244
339
|
destination: 'documents',
|
|
245
340
|
fileName: 'image.png',
|
|
246
341
|
});
|
|
247
342
|
|
|
248
|
-
// URL to Base64 (
|
|
249
|
-
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
|
|
250
350
|
|
|
251
351
|
// Native Share Sheet
|
|
252
|
-
await shareFile({
|
|
352
|
+
await shareFile({
|
|
353
|
+
filePath: '/path/to/report.pdf',
|
|
354
|
+
title: 'Share report', // Optional
|
|
355
|
+
subject: 'Monthly report', // Optional (email subject)
|
|
356
|
+
});
|
|
253
357
|
|
|
254
358
|
// Open with default system app
|
|
255
359
|
await openFile({
|
|
@@ -258,19 +362,111 @@ await openFile({
|
|
|
258
362
|
});
|
|
259
363
|
```
|
|
260
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
|
+
|
|
261
404
|
---
|
|
262
405
|
|
|
263
406
|
## 📚 API Reference
|
|
264
407
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
|
268
|
-
|
|
|
269
|
-
| `
|
|
270
|
-
| `
|
|
271
|
-
| `
|
|
272
|
-
|
|
273
|
-
|
|
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. |
|
|
274
470
|
|
|
275
471
|
---
|
|
276
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",
|
|
@@ -125,6 +125,7 @@
|
|
|
125
125
|
"url": "https://github.com/chavan-labs/rn-file-toolkit/issues"
|
|
126
126
|
},
|
|
127
127
|
"homepage": "https://chavan-labs.github.io/rn-file-toolkit/",
|
|
128
|
+
"funding": "https://github.com/sponsors/chavan-labs",
|
|
128
129
|
"publishConfig": {
|
|
129
130
|
"registry": "https://registry.npmjs.org/"
|
|
130
131
|
},
|