mbkbucket 4.0.0 โ†’ 4.1.0

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.
Files changed (40) hide show
  1. package/.env.example +16 -15
  2. package/README.md +613 -561
  3. package/index.js +36 -33
  4. package/lib/cli.js +239 -239
  5. package/lib/public/bucketadmin.css +2042 -2042
  6. package/lib/public/bucketadmin.js +1844 -1622
  7. package/lib/public/mbkbucket-helper-demo.html +0 -58
  8. package/lib/public/mbkbucket-helper.js +1 -9
  9. package/lib/src/config/validation.js +91 -86
  10. package/lib/src/controllers/api.controller.js +486 -459
  11. package/lib/src/controllers/dashboard.controller.js +69 -69
  12. package/lib/src/controllers/info.controller.js +13 -13
  13. package/lib/src/controllers/view.controller.js +234 -233
  14. package/lib/src/core/metrics.js +134 -0
  15. package/lib/src/core/storage/capabilities.js +58 -0
  16. package/lib/src/core/storage/errors.js +0 -5
  17. package/lib/src/core/storage/gdrive-auth.js +0 -36
  18. package/lib/src/core/storage/gdrive-provider.js +101 -43
  19. package/lib/src/core/storage/index.js +4 -1
  20. package/lib/src/core/storage/local-provider.js +507 -0
  21. package/lib/src/core/storage/models.js +90 -50
  22. package/lib/src/core/storage/path-strategy.js +14 -1
  23. package/lib/src/core/storage/s3-provider.js +17 -37
  24. package/lib/src/core/storage/storage-manager.js +70 -50
  25. package/lib/src/core/storage/storage-provider.js +49 -120
  26. package/lib/src/middleware/bucket-resolver.js +37 -37
  27. package/lib/src/routes/api.routes.js +4 -1
  28. package/lib/src/routes/index.js +60 -60
  29. package/lib/src/routes/view.routes.js +28 -28
  30. package/lib/src/services/s3.service.js +42 -42
  31. package/lib/src/services/storage.service.js +311 -213
  32. package/lib/src/utils/cleanup-scheduler.js +91 -0
  33. package/lib/src/utils/errors.js +56 -56
  34. package/lib/src/utils/helpers.js +6 -6
  35. package/package.json +3 -5
  36. package/views/bucket.hbs +0 -5
  37. package/views/bucketadmin_move_modal.hbs +35 -35
  38. package/views/bucketadmin_preview_modal.hbs +108 -108
  39. package/views/bucketadmincontent.hbs +193 -193
  40. package/views/mbkbucket_info.hbs +134 -134
package/README.md CHANGED
@@ -1,562 +1,614 @@
1
- # mbkbucket
2
-
3
- > Flexible S3, Cloudflare R2, and Google Drive cloud storage management โ€” library, Express router, dashboard, and standalone server CLI for mbktech.org applications.
4
-
5
- [![Version](https://img.shields.io/npm/v/mbkbucket.svg)](https://www.npmjs.com/package/mbkbucket)
6
- [![Downloads](https://img.shields.io/npm/dm/mbkbucket.svg)](https://www.npmjs.com/package/mbkbucket)
7
- [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
8
- [![Node.js](https://img.shields.io/badge/node-%3E%3D18.0.0-brightgreen.svg)](https://nodejs.org/)
9
- [![Tests](https://img.shields.io/badge/tests-passing-brightgreen.svg)](tests)
10
-
11
- ---
12
-
13
- ## Table of Contents
14
-
15
- - [What's New in v4.0.0](#whats-new-in-v400-)
16
- - [Installation](#installation-)
17
- - [Quick Start](#quick-start-)
18
- - [Supported Storage Providers](#supported-storage-providers-)
19
- - [CLI Usage](#cli-usage-)
20
- - [Library API](#library-api-)
21
- - [File Operations](#file-operations)
22
- - [Folder, Copy & Move Operations](#folder-copy--move-operations)
23
- - [Multipart Upload](#multipart-upload)
24
- - [Storage & Connection Management](#storage--connection-management)
25
- - [Storage Core & Custom Drivers](#storage-core--custom-drivers)
26
- - [Google Drive Authentication Helpers](#google-drive-authentication-helpers)
27
- - [Key Prefixing & App Isolation](#key-prefixing--app-isolation)
28
- - [Config & Diagnostics](#config--diagnostics)
29
- - [Express Integration](#express-integration)
30
- - [Express Router & HTTP API](#express-router--http-api-)
31
- - [Endpoints](#endpoints)
32
- - [Host-Controlled Permissions](#host-controlled-permissions)
33
- - [Templates & Views](#templates--views)
34
- - [Frontend Helper Client](#frontend-helper-client-)
35
- - [Environment Configuration](#environment-configuration-)
36
- - [BucketConnection](#bucketconnection-required)
37
- - [mbkautheVar](#mbkauthevar-required)
38
- - [mbkbucketVar](#mbkbucketvar-optional)
39
- - [Automated Tests](#automated-tests-)
40
- - [Contact & Support](#contact--support)
41
- - [License](#license)
42
-
43
- ---
44
-
45
- ## What's New in v4.0.0 ๐ŸŒŸ
46
-
47
- - **Provider-Independent Storage Architecture**: Unified `StorageProvider` base class and dynamic `StorageManager` registry. Seamlessly switch between or combine multiple storage backends.
48
- - **Native Google Drive Support**: First-class Google Drive provider supporting both OAuth 2.0 (user authorization code / refresh token flow) and Google Cloud Service Account credentials with folder-level targeting (`folder_id`).
49
- - **File Move & Copy Operations**: Built-in `moveFile` and `copyFile` operations across both library functions, REST APIs, and UI modals.
50
- - **Native Folder Creation**: Create directory markers and structure via `createFolder` with automatic folder marker resolution.
51
- - **Redesigned Admin Dashboard**: Clean modern UI with connection health badges, provider tags, storage connection switcher dropdown, dedicated Move/Rename modal, and enhanced media previews (video range streaming, PDF, audio, and code view).
52
- - **Frontend Drop-in Client Library (`mbkbucket-helper.js`)**: Bundled client helper with a modal gallery browser, input file picker, drag-and-drop chunked uploader, URL builders, and toast notifications. Demo page included at `/mbkbucket/helper-demo`.
53
- - **Custom Storage Drivers**: Register custom storage engines via `storageManager.registerDriver('custom', factory)`.
54
- - **Full Backward Compatibility**: All legacy S3 helper methods remain available and map cleanly to the underlying storage provider.
55
-
56
- ---
57
-
58
- ## Installation ๐Ÿ“ฆ
59
-
60
- ```bash
61
- npm install mbkbucket
62
- ```
63
-
64
- ---
65
-
66
- ## Quick Start ๐Ÿš€
67
-
68
- ### As a Library
69
-
70
- ```js
71
- import { uploadFile, downloadFile, listfiles, copyFile, moveFile, createFolder, deleteFile, generateSignedUrl, storageManager } from 'mbkbucket';
72
-
73
- // Upload a file to default storage (or specify connectionName / bucketName)
74
- const upload = await uploadFile(
75
- 'documents/invoice.pdf',
76
- fileBuffer,
77
- 'application/pdf',
78
- { connectionName: 'R2_Bucket' }
79
- );
80
-
81
- // List files and directories
82
- const { Contents, CommonPrefixes } = await listfiles('documents/', {
83
- delimiter: '/',
84
- maxKeys: 100
85
- });
86
-
87
- // Copy or move files
88
- await copyFile('documents/invoice.pdf', 'backup/invoice.pdf');
89
- await moveFile('documents/invoice.pdf', 'archive/invoice-2026.pdf');
90
-
91
- // Create a folder marker
92
- await createFolder('projects/2026');
93
-
94
- // Download a file stream
95
- const { Body, ContentType, ContentLength } = await downloadFile('archive/invoice-2026.pdf');
96
-
97
- // Generate pre-signed URL (for S3-compatible providers)
98
- const { url } = await generateSignedUrl('archive/invoice-2026.pdf', 'getObject', 3600);
99
-
100
- // Check health across active connections
101
- const health = await storageManager.checkHealth('Google_Drive');
102
- console.log('Google Drive status:', health.status);
103
- ```
104
-
105
- ### As Express Middleware
106
-
107
- ```js
108
- import express from 'express';
109
- import { createBucketRouter } from 'mbkbucket';
110
- import { sessRole, sessPerm } from 'mbkauthe';
111
-
112
- const app = express();
113
-
114
- // Mount the bucket dashboard and API under /mbkbucket
115
- app.use(createBucketRouter({
116
- authorization: {
117
- view: sessRole('admin'),
118
- upload: sessPerm('storage.upload'),
119
- delete: sessRole('superadmin'),
120
- },
121
- publiViewEnabled: true,
122
- }));
123
-
124
- app.listen(3004, () => {
125
- console.log('Server running on http://localhost:3004');
126
- });
127
- ```
128
-
129
- ### As a Standalone Application
130
-
131
- ```js
132
- import mbkbucket from 'mbkbucket';
133
-
134
- // mbkbucket is a pre-configured Express app with mbkauthe sessions, views, and routes
135
- mbkbucket.listen(3004, () => {
136
- console.log('mbkbucket running on http://localhost:3004/mbkbucket');
137
- });
138
- ```
139
-
140
- ---
141
-
142
- ## Supported Storage Providers ๐Ÿ—„๏ธ
143
-
144
- mbkbucket v4.0.0 supports multiple object and file storage providers simultaneously:
145
-
146
- | Provider | Type Identifier | Features / Capabilities |
147
- |---|---|---|
148
- | **AWS S3** | `s3` | Multipart upload, signed URLs, range streaming, server-side copy/move |
149
- | **Cloudflare R2** | `s3` / `r2` | S3-compatible, zero egress fees, signed URLs, multipart upload |
150
- | **Google Drive** | `gdrive` / `google-drive` | OAuth 2.0 & Service Account auth, native folder hierarchy, direct download URLs, range requests |
151
- | **MinIO** | `s3` / `minio` | Self-hosted S3-compatible storage, multipart, signed URLs |
152
- | **IDrive e2** | `s3` | S3-compatible high-performance object storage |
153
- | **Backblaze B2** | `s3` | S3-compatible API endpoints |
154
- | **DigitalOcean Spaces** | `s3` | S3-compatible object storage |
155
- | **Custom Drivers** | Any custom name | Register via `storageManager.registerDriver(type, factory)` |
156
-
157
- ---
158
-
159
- ## CLI Usage ๐Ÿ’ป
160
-
161
- mbkbucket includes a standalone CLI server runner with built-in configuration verification, port binding, and browser launching.
162
-
163
- ### Run Standalone Server
164
-
165
- ```bash
166
- # Run with npx without installation
167
- npx mbkbucket
168
-
169
- # Or after installing globally or in your repository
170
- mbkbucket
171
- ```
172
-
173
- ### CLI Options
174
-
175
- | Option | Shorthand | Description |
176
- |---|---|---|
177
- | `--port <number>` | `-p` | Port to listen on (default: `3004` or `$PORT`) |
178
- | `--host <host>` | `-H` | Host address to bind to (default: `0.0.0.0` or `$HOST`) |
179
- | `--app <name>` | `-a` | Override `APP_NAME` for bucket key prefix isolation |
180
- | `--bucket <name>` | `-b` | Override default bucket / storage connection name |
181
- | `--env <path>` | `-e` | Load environment variables from a custom `.env` file |
182
- | `--dev` | `-d` | Run in development mode (`NODE_ENV=dev`) |
183
- | `--open` | `-o` | Automatically open dashboard in default browser |
184
- | `--version` | `-v` | Show version number |
185
- | `--help` | `-h` | Show help text |
186
-
187
- ### CLI Examples
188
-
189
- ```bash
190
- # Start server on custom port and open browser
191
- mbkbucket -p 8080 --open
192
-
193
- # Start with a specific production .env file and application prefix
194
- mbkbucket --env ./configs/.env.prod -a portalapp
195
- ```
196
-
197
- ---
198
-
199
- ## Library API ๐Ÿ“š
200
-
201
- Full TypeScript declarations are available in [index.d.ts](index.d.ts).
202
-
203
- ### File Operations
204
-
205
- | Function | Description |
206
- |---|---|
207
- | `uploadFile(key, buffer, contentType, options?)` | Upload a file buffer/stream with options (`preventOverwrite`, `metadata`, `bucketName`/`connectionName`) |
208
- | `downloadFile(key, options?)` | Download a file as a readable stream with metadata and headers |
209
- | `deleteFile(key, connectionName?)` | Delete a single file |
210
- | `deleteFiles(keys, connectionName?)` | Batch delete an array of files |
211
- | `listfiles(prefix?, options?)` | List files and folders with pagination, search, or delimiter filtering |
212
- | `getFileMetadata(key, connectionName?)` | Retrieve file metadata without downloading content |
213
- | `fileExists(key, connectionName?)` | Check if a file exists |
214
- | `getFileSize(key, connectionName?)` | Get file size in bytes (returns `number` or `null`) |
215
- | `generateSignedUrl(key, operation?, expiresIn?, connectionName?)` | Generate a pre-signed URL (`getObject` or `putObject`) |
216
-
217
- ### Folder, Copy & Move Operations
218
-
219
- | Function | Description |
220
- |---|---|
221
- | `createFolder(prefix, connectionName?, options?)` | Create a folder / directory marker |
222
- | `copyFile(sourceKey, destKey, options?, connectionName?)` | Copy a file within storage |
223
- | `moveFile(sourceKey, destKey, options?, connectionName?)` | Move or rename a file within storage |
224
- | `deleteFolder(prefix, connectionName?)` | Recursively delete all files under a folder prefix |
225
-
226
- ### Multipart Upload
227
-
228
- *(Supported on S3-compatible providers)*
229
-
230
- | Function | Description |
231
- |---|---|
232
- | `createMultipartUpload(key, contentType?, metadata?, connectionName?)` | Initialize a multipart upload session |
233
- | `uploadPart(key, uploadId, partNumber, buffer, connectionName?)` | Upload an individual chunk part |
234
- | `completeMultipartUpload(key, uploadId, parts, connectionName?)` | Finalize and assemble multipart upload |
235
- | `abortMultipartUpload(key, uploadId, connectionName?)` | Abort and cancel a multipart upload |
236
- | `listIncompleteMultipartUploads(prefix?, connectionName?)` | List pending / incomplete multipart uploads |
237
- | `cleanupIncompleteMultipartUploads(olderThanDays?, prefix?, connectionName?)` | Clean up abandoned multipart chunks |
238
-
239
- ### Storage & Connection Management
240
-
241
- | Function / Export | Description |
242
- |---|---|
243
- | `getAvailableStorageNames()` / `getAvailableBucketNames()` | List all configured storage connection identifiers |
244
- | `resolveStorageName(name?)` / `resolveBucketName(name?)` | Resolve and validate target connection name (falls back to default) |
245
- | `getStorageProvider(name?)` | Get the initialized `StorageProvider` instance for a connection |
246
- | `getStorageConfig(name?)` / `getBucketConfig(name?)` | Retrieve connection credentials and configuration object |
247
- | `getBucketClient(bucketName?)` | Get the underlying AWS SDK S3Client instance (for S3 connections) |
248
- | `checkHealth(name?)` | Execute health and connectivity check on a storage connection |
249
- | `runHealthCheck(name?)` | Run diagnostic health check with console / debug output |
250
-
251
- ### Storage Core & Custom Drivers
252
-
253
- mbkbucket exposes its core abstraction classes so you can extend or inspect providers programmatically:
254
-
255
- ```js
256
- import { StorageProvider, StorageManager, storageManager } from 'mbkbucket';
257
-
258
- // Register a custom storage driver
259
- storageManager.registerDriver('local-disk', (name, config) => {
260
- return new MyCustomLocalStorageProvider(name, config);
261
- });
262
-
263
- // Register a runtime connection using the driver
264
- storageManager.registerConnection('local_backup', {
265
- type: 'local-disk',
266
- basePath: '/var/storage'
267
- });
268
- ```
269
-
270
- Exported classes:
271
- - `StorageProvider`: Abstract base class with required contracts (`uploadFile`, `downloadFile`, `listFiles`, `deleteFile`, `copyFile`, `moveFile`, `createFolder`, etc.) and `capabilities` flags.
272
- - `S3StorageProvider`: S3, Cloudflare R2, MinIO implementation.
273
- - `GoogleDriveStorageProvider`: Google Drive implementation.
274
- - `StorageManager` / `storageManager`: Connection registry, driver repository, and health orchestrator.
275
- - `StorageFile`, `StorageFolder`, `StorageListResult`: Standardized metadata models.
276
- - Custom Errors: `StorageError`, `StorageNotFoundError`, `StorageAccessDeniedError`, `StorageConflictError`, `StorageValidationError`, `StorageConfigError`, `StorageQuotaExceededError`, `StorageUnsupportedOperationError`.
277
-
278
- ### Google Drive Authentication Helpers
279
-
280
- mbkbucket exports helper functions for handling Google Drive OAuth 2.0 flows:
281
-
282
- | Export | Description |
283
- |---|---|
284
- | `getGoogleAuthUrl(options)` | Generate Google OAuth 2.0 consent URL (`clientId`, `redirectUri`, `state`) |
285
- | `exchangeCodeForTokens(options)` | Exchange authorization code for access and refresh tokens |
286
- | `refreshGoogleAccessToken(options)` | Refresh an expired access token using a refresh token |
287
- | `getServiceAccountAccessToken(options)` | Generate access token using Google Cloud Service Account credentials |
288
- | `GOOGLE_DRIVE_SCOPES` | Standard OAuth scope strings for Google Drive (`drive.file`, `drive`, etc.) |
289
-
290
- ### Key Prefixing & App Isolation
291
-
292
- | Function | Description |
293
- |---|---|
294
- | `getAppName()` | Retrieve the active application namespace from `mbkautheVar.APP_NAME` |
295
- | `ensureKeyHasAppPrefix(key?)` | Prefix a file key with the app prefix to enforce tenant isolation |
296
- | `ensurePrefix(prefix?)` | Prefix a folder path with the app prefix |
297
- | `validateKeySafety(str, isKey?)` | Guard against path traversal attacks (`..`, null bytes, illegal characters) |
298
-
299
- ### Config & Diagnostics
300
-
301
- | Export | Description |
302
- |---|---|
303
- | `packageJson` | mbkbucket package metadata (version, license, etc.) |
304
- | `appVersion` | Detected version of the host application |
305
- | `mbkbucketVar` | Parsed and validated `mbkbucketVar` configuration |
306
- | `checkVersion()` | Check GitHub releases for newer versions of mbkbucket |
307
- | `validateConfiguration()` | Validate `mbkbucketVar` settings |
308
- | `validateBucketConnection()` | Validate `BucketConnection` syntax and provider schemas |
309
- | `validateAllConfiguration()` | Run all configuration validations |
310
-
311
- ### Express Integration
312
-
313
- | Export | Description |
314
- |---|---|
315
- | `bucket` (named) | Pre-configured Express router with superadmin protection |
316
- | `createBucketRouter(options)` | Configurable router factory with custom permission middleware |
317
- | `default` | Standalone Express server application ready for `.listen()` |
318
-
319
- ---
320
-
321
- ## Express Router & HTTP API ๐Ÿ–ฅ๏ธ
322
-
323
- ### Endpoints
324
-
325
- All routes are mounted relative to the router path (typically `/mbkbucket`):
326
-
327
- | Endpoint | Method | Default Access | Description |
328
- |---|---|---|---|
329
- | `/mbkbucket` | `GET` | `view` auth | Admin file manager dashboard UI |
330
- | `/mbkbucket/info` | `GET` | Public / Any | Health check, provider capabilities, and runtime config info |
331
- | `/mbkbucket/view/*key` | `GET` | `view` auth | Inline viewer with byte-range streaming for video, audio, PDF, images, text |
332
- | `/mbkbucket/player/*key` | `GET` | `view` auth | Dedicated media player page for video and audio playback |
333
- | `/mbkbucket/p_view/*key` | `GET` | Public | Public file viewer (enabled when `publiView_enabled` is `"true"`) |
334
- | `/mbkbucket/download/*key` | `GET` | `view` auth | Download file with `Content-Disposition: attachment` headers |
335
- | `/mbkbucket/api/files` | `GET` | `view` auth | List files and folders with pagination, search, and continuation tokens |
336
- | `/mbkbucket/api/file-info/*key`| `GET` | `view` auth | Get metadata (Content-Type, size, ETag, last modified) for a key |
337
- | `/mbkbucket/api/health` | `GET` | `view` auth | Check connectivity for active storage connection |
338
- | `/mbkbucket/api/incomplete-uploads` | `GET` | `view` auth | List incomplete multipart uploads |
339
- | `/mbkbucket/api/cleanup-uploads` | `POST` | `delete` auth | Clean up incomplete multipart uploads older than specified days |
340
- | `/mbkbucket/upload` | `POST` | `upload` auth | Upload a single file via `multipart/form-data` with conflict detection |
341
- | `/mbkbucket/upload-init` | `POST` | `upload` auth | Initiate multipart upload session |
342
- | `/mbkbucket/upload-chunk` | `POST` | `upload` auth | Upload individual multipart chunk (up to 50MB) |
343
- | `/mbkbucket/upload-complete` | `POST` | `upload` auth | Assemble and complete multipart upload |
344
- | `/mbkbucket/upload-abort` | `POST` | `delete` auth | Abort pending multipart upload session |
345
- | `/mbkbucket/create-folder` | `POST` | `upload` auth | Create a new directory folder marker |
346
- | `/mbkbucket/delete` | `POST` | `delete` auth | Delete a single file, batch array of keys, or folder |
347
- | `/mbkbucket/move` | `POST` | `upload` auth | Move or rename a file (`sourceKey` to `destKey`) |
348
- | `/mbkbucket/copy` | `POST` | `upload` auth | Copy or duplicate a file (`sourceKey` to `destKey`) |
349
-
350
- ### Host-Controlled Permissions
351
-
352
- You can configure granular authorization per operation using `createBucketRouter`:
353
-
354
- ```js
355
- import { createBucketRouter } from 'mbkbucket';
356
- import { sessPerm } from 'mbkauthe';
357
- import { Permissions } from './permissions.js';
358
-
359
- app.use(createBucketRouter({
360
- authorization: {
361
- view: sessPerm(Permissions.storage.view),
362
- upload: sessPerm(Permissions.storage.upload),
363
- delete: sessPerm(Permissions.storage.delete),
364
- },
365
- publiViewEnabled: false,
366
- }));
367
- ```
368
-
369
- - When no custom authorization is passed, `sessRole('superadmin')` protects all private endpoints by default.
370
- - Passing a single middleware function to `authorization` applies that guard across all three operations.
371
-
372
- ### Templates & Views
373
-
374
- The Express router renders Handlebars (`.hbs`) views located in `views/`:
375
-
376
- | Template / Partial | Description |
377
- |---|---|
378
- | `bucketportal.hbs` | Dashboard shell layout |
379
- | `bucketadmincontent.hbs` | File manager interface with tree sidebar and storage switcher |
380
- | `bucketadmin_alerts.hbs` | UI alerts and toast partial |
381
- | `bucketadmin_delete_modal.hbs` | Item and folder deletion confirmation modal |
382
- | `bucketadmin_move_modal.hbs` | File move and rename modal with destination selection |
383
- | `bucketadmin_newfolder_modal.hbs` | New folder creation modal |
384
- | `bucketadmin_preview_modal.hbs` | Full-screen media preview modal (images, video, audio, code, PDF) |
385
- | `bucketadmin_upload_modal.hbs` | Chunked file upload modal with progress bars |
386
- | `bucketadmin_filelist_skeleton.hbs`| Loading state placeholder skeleton |
387
- | `bucket.hbs` | Public file viewer page |
388
- | `mbkbucket_info.hbs` | Diagnostics, active connections, and environment info page |
389
-
390
- ---
391
-
392
- ## Frontend Helper Client ๐ŸŽจ
393
-
394
- mbkbucket bundles a lightweight, zero-dependency client library served directly from your app:
395
-
396
- - Script: `/mbkbucket/mbkbucket-helper.js`
397
- - Styles: `/mbkbucket/mbkbucket-helper.css`
398
- - Live interactive demo: `/mbkbucket/helper-demo`
399
-
400
- ### Including the Helper
401
-
402
- ```html
403
- <link rel="stylesheet" href="/mbkbucket/mbkbucket-helper.css">
404
- <script src="/mbkbucket/mbkbucket-helper.js"></script>
405
- ```
406
-
407
- ### Features
408
-
409
- #### 1. Gallery Browser Modal
410
- Open a rich modal browser to allow users to select one or multiple files directly from storage:
411
-
412
- ```js
413
- MBKBucket.openGallery({
414
- title: 'Choose Profile Banner',
415
- filter: 'image', // 'image' | 'video' | 'audio' | 'pdf' | 'all'
416
- multiple: false,
417
- onSelect: (selectedFiles) => {
418
- console.log('Selected:', selectedFiles[0].url, selectedFiles[0].key);
419
- }
420
- });
421
- ```
422
-
423
- #### 2. File Picker Inputs
424
- Bind an existing `<input>` element with auto-attached browse buttons and live thumbnail previews:
425
-
426
- ```html
427
- <input type="text" data-mbk-picker data-filter="image" data-preview="#avatar-preview">
428
- <img id="avatar-preview" src="/placeholder.png" alt="Preview">
429
- ```
430
-
431
- Or programmatically:
432
- ```js
433
- MBKBucket.createPicker(document.getElementById('my-input'), {
434
- filter: 'image',
435
- onSelect: (file) => console.log('Picked:', file)
436
- });
437
- ```
438
-
439
- #### 3. Upload Widget
440
- Add a drag-and-drop uploader with automatic chunking for large files:
441
-
442
- ```html
443
- <div data-mbk-uploader data-prefix="uploads/users" data-allowed="image/*,video/*"></div>
444
- ```
445
-
446
- #### 4. Client URL Builders
447
- ```js
448
- MBKBucket.url.view('photos/cover.jpg'); // -> /mbkbucket/view/photos/cover.jpg
449
- MBKBucket.url.download('reports/data.xlsx'); // -> /mbkbucket/download/reports/data.xlsx
450
- MBKBucket.url.publicView('public/logo.png'); // -> /mbkbucket/p_view/public/logo.png
451
- MBKBucket.url.player('media/intro.mp4'); // -> /mbkbucket/player/media/intro.mp4
452
- ```
453
-
454
- ---
455
-
456
- ## Environment Configuration โš™๏ธ
457
-
458
- ### `BucketConnection` (required)
459
-
460
- A JSON string defining one or more storage connections. You can configure **S3/R2** connections and **Google Drive** connections together:
461
-
462
- ```env
463
- BucketConnection={"R2_Bucket":{"BUCKET_NAME":"my-bucket","ACCESS_KEY_ID":"your-key","SECRET_ACCESS_KEY":"your-secret","ENDPOINT":"https://<account-id>.r2.cloudflarestorage.com"},"Google_Drive":{"type":"gdrive","client_id":"your-client-id.apps.googleusercontent.com","client_secret":"your-client-secret","refresh_token":"your-refresh-token","folder_id":"root"}}
464
- ```
465
-
466
- #### S3 / Cloudflare R2 Connection Fields
467
-
468
- | Field | Required | Description |
469
- |---|---|---|
470
- | `BUCKET_NAME` | Yes | Target bucket name |
471
- | `ACCESS_KEY_ID` | Yes | S3 Access Key ID |
472
- | `SECRET_ACCESS_KEY` | Yes | S3 Secret Access Key |
473
- | `ENDPOINT` | Yes | S3 API endpoint URL (e.g. `https://<id>.r2.cloudflarestorage.com` or `https://s3.us-east-1.amazonaws.com`) |
474
- | `region` | No | Region (defaults to `'auto'` for Cloudflare R2 / custom endpoints) |
475
-
476
- #### Google Drive Connection Fields
477
-
478
- | Field | Required | Description |
479
- |---|---|---|
480
- | `type` | Yes | Provider tag: `"gdrive"` or `"google-drive"` |
481
- | `client_id` | OAuth | Google OAuth 2.0 Client ID |
482
- | `client_secret` | OAuth | Google OAuth 2.0 Client Secret |
483
- | `refresh_token` | OAuth | Long-lived Google OAuth 2.0 refresh token |
484
- | `folder_id` | No | Target root folder ID in Google Drive (defaults to `"root"`) |
485
- | `client_email` | Service Account | Service Account client email (alternative to OAuth) |
486
- | `private_key` | Service Account | Service Account PEM private key (alternative to OAuth) |
487
-
488
- #### Google Drive via Service Account Example
489
-
490
- ```env
491
- BucketConnection={"Google_Drive_SA":{"type":"gdrive","client_email":"storage-sa@project.iam.gserviceaccount.com","private_key":"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----","folder_id":"1A2B3C4D5E6F"}}
492
- ```
493
-
494
- ### `mbkautheVar` (required)
495
-
496
- Standard mbkauthe configuration. Key fields relevant to storage:
497
-
498
- | Field | Required | Description |
499
- |---|---|---|
500
- | `APP_NAME` | Yes | Namespace used for automatic key prefix isolation |
501
- | `bucket` | No | Default active connection name (falls back to first key in `BucketConnection`) |
502
- | `loginRedirectURL` | No | URL to redirect upon authentication |
503
-
504
- ### `mbkbucketVar` (optional)
505
-
506
- Optional behavioral toggles for viewers and streaming:
507
-
508
- ```env
509
- mbkbucketVar={"publiView_enabled":"true","p_view_inline":"true"}
510
- ```
511
-
512
- | Field | Default | Description |
513
- |---|---|---|
514
- | `publiView_enabled` | `false` | Enable or disable unauthenticated public file access under `/mbkbucket/p_view/*` |
515
- | `p_view_inline` | `true` | Stream files inline in browser response (vs redirecting to external signed URLs) |
516
-
517
- ---
518
-
519
- ## Automated Tests ๐Ÿงช
520
-
521
- mbkbucket uses [Vitest](https://vitest.dev/) for unit and integration testing:
522
-
523
- ```bash
524
- npm test
525
- ```
526
-
527
- ### Test Suites Covered
528
-
529
- - **Storage Provider & Core Abstractions**: `StorageProvider` interface contracts, capability inspection, `PathStrategy` traversal security, `StorageError` hierarchy, Google Drive consent URL generation.
530
- - **S3 & Google Drive Drivers**: Health checks, mock provider operations, connection switching, custom driver registration.
531
- - **API Routes Logic**: File listing, single and chunked multipart uploads, conflict detection (409), folder creation, single/batch deletion, move and copy operations, health endpoint, metadata inspection.
532
- - **View Controller & Streaming**: Inline viewer, video byte-range streaming (`206 Partial Content`), unsupported MIME rejection (`415`), public file viewing.
533
- - **Configuration Validation**: S3 and Google Drive connection validation, malformed JSON recovery, boolean normalization, and error messaging.
534
- - **Public API Exports**: Verification of all exported functions, classes, and proxy objects from package root.
535
-
536
- ---
537
-
538
- ## Contact & Support
539
-
540
- - **Website**: [mbktech.org/Support](https://mbktech.org/Support/?Project=MIbnEKhalidWeb)
541
- - **Email**: [support@mbktech.org](mailto:support@mbktech.org)
542
- - **GitHub**: [MIbnEKhalid/mbkbucket](https://github.com/MIbnEKhalid/mbkbucket)
543
-
544
- ---
545
-
546
- ## About
547
-
548
- Developed by [Muhammad Bin Khalid](https://github.com/MIbnEKhalid)
549
- Part of [MBK Tech](https://mbktech.org/)
550
-
551
- ---
552
-
553
- ## License
554
-
555
- Licensed under the MIT License. See [LICENSE](LICENSE) for details.
556
-
557
- <!--
558
- * MBKBucket
559
- * Copyright (c) 2026 Muhammad Bin Khalid, MBKTech.org and contributors
560
- * Licensed under the MIT License.
561
- * Source: https://github.com/MIbnEKhalid/mbkbucket
1
+ # mbkbucket
2
+
3
+ > Flexible S3, Cloudflare R2, Google Drive, and Local Disk storage management โ€” library, Express router, dashboard, and standalone server CLI for mbktech.org applications.
4
+
5
+ [![Version](https://img.shields.io/npm/v/mbkbucket.svg)](https://www.npmjs.com/package/mbkbucket)
6
+ [![Downloads](https://img.shields.io/npm/dm/mbkbucket.svg)](https://www.npmjs.com/package/mbkbucket)
7
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
8
+ [![Node.js](https://img.shields.io/badge/node-%3E%3D18.0.0-brightgreen.svg)](https://nodejs.org/)
9
+ [![Tests](https://img.shields.io/badge/tests-passing-brightgreen.svg)](tests)
10
+
11
+ ---
12
+
13
+ ## Table of Contents
14
+
15
+ - [What's New in v4.1.0](#whats-new-in-v410-)
16
+ - [Installation](#installation-)
17
+ - [Quick Start](#quick-start-)
18
+ - [Supported Storage Providers](#supported-storage-providers-)
19
+ - [CLI Usage](#cli-usage-)
20
+ - [Library API](#library-api-)
21
+ - [File Operations](#file-operations)
22
+ - [Folder, Copy & Move Operations](#folder-copy--move-operations)
23
+ - [Multipart Upload & Cleanup Scheduler](#multipart-upload--cleanup-scheduler)
24
+ - [Storage & Connection Management](#storage--connection-management)
25
+ - [Storage Core & Custom Drivers](#storage-core--custom-drivers)
26
+ - [Metrics & Observability](#metrics--observability)
27
+ - [Google Drive Authentication Helpers](#google-drive-authentication-helpers)
28
+ - [Key Prefixing & App Isolation](#key-prefixing--app-isolation)
29
+ - [Config & Diagnostics](#config--diagnostics)
30
+ - [Express Integration](#express-integration)
31
+ - [Express Router & HTTP API](#express-router--http-api-)
32
+ - [Endpoints](#endpoints)
33
+ - [Host-Controlled Permissions](#host-controlled-permissions)
34
+ - [Templates & Views](#templates--views)
35
+ - [Frontend Helper Client](#frontend-helper-client-)
36
+ - [Environment Configuration](#environment-configuration-)
37
+ - [BucketConnection](#bucketconnection-required)
38
+ - [mbkautheVar](#mbkauthevar-required)
39
+ - [mbkbucketVar](#mbkbucketvar-optional)
40
+ - [Automated Tests](#automated-tests-)
41
+ - [Contact & Support](#contact--support)
42
+ - [License](#license)
43
+
44
+ ---
45
+
46
+ ## What's New in v4.1.0 ๐ŸŒŸ
47
+
48
+ - **Cross-Platform Local Disk Provider (`LocalStorageProvider`)**: First-class local filesystem storage provider with full Windows, Linux, and macOS compatibility, path sanitization against directory traversal, quota enforcement (`maxBytes`), recursive directory scanning, and byte-range streaming.
49
+ - **Metrics & Observability Registry (`MetricsRegistry`)**: Built-in Prometheus-compatible metrics registry (`metricsRegistry`) and endpoints (`/mbkbucket/api/metrics` and `/mbkbucket/metrics`) tracking operation counts, duration summaries, transferred byte throughput, error counts, and active multipart sessions.
50
+ - **Event-Driven Lifecycle & Storage Orchestration**: `StorageManager` now extends `EventEmitter`, broadcasting real-time lifecycle events (`file:uploaded`, `file:deleted`, `files:deleted`, `folder:created`, `folder:deleted`, `connection:registered`, `driver:registered`) with full lifecycle methods (`initialize()`, `dispose()`, `ping()`).
51
+ - **Automated Incomplete Multipart Cleanup Scheduler (`CleanupScheduler`)**: Background cron-like scheduler (`cleanupScheduler`) to automatically identify and abort abandoned multipart upload chunks older than a configurable threshold.
52
+ - **RFC 7233 Open-Ended Byte-Range Streaming**: High-performance range streaming for PDF documents (Chrome PDF viewer, Firefox PDF.js), HTML5 video, and audio players (`206 Partial Content`) with proper inline content disposition.
53
+ - **Native Google Drive & S3/R2 Enhancements**: Unified error handling, exponential backoff retries on rate limits (429/503), and automatic driver inference for local paths.
54
+
55
+ ---
56
+
57
+ ## Installation ๐Ÿ“ฆ
58
+
59
+ ```bash
60
+ npm install mbkbucket
61
+ ```
62
+
63
+ ---
64
+
65
+ ## Quick Start ๐Ÿš€
66
+
67
+ ### As a Library
68
+
69
+ ```js
70
+ import { uploadFile, downloadFile, listfiles, copyFile, moveFile, createFolder, deleteFile, storageManager, metricsRegistry } from 'mbkbucket';
71
+
72
+ // Upload a file to default storage (or specify connectionName / bucketName)
73
+ const upload = await uploadFile(
74
+ 'documents/invoice.pdf',
75
+ fileBuffer,
76
+ 'application/pdf',
77
+ { connectionName: 'Local_Disk' } // or 'R2_Bucket', 'Google_Drive'
78
+ );
79
+
80
+ // List files and directories
81
+ const { Contents, CommonPrefixes } = await listfiles('documents/', {
82
+ delimiter: '/',
83
+ maxKeys: 100
84
+ });
85
+
86
+ // Copy or move files
87
+ await copyFile('documents/invoice.pdf', 'backup/invoice.pdf');
88
+ await moveFile('documents/invoice.pdf', 'archive/invoice-2026.pdf');
89
+
90
+ // Create a folder marker
91
+ await createFolder('projects/2026');
92
+
93
+ // Download a file stream (with optional byte ranges for streaming)
94
+ const { Body, ContentType, ContentLength } = await downloadFile('archive/invoice-2026.pdf', {
95
+ range: 'bytes=0-1048575'
96
+ });
97
+
98
+ // Listen to storage lifecycle events
99
+ storageManager.on('file:uploaded', ({ key, provider, size }) => {
100
+ console.log(`[Event] File uploaded: ${key} (${size} bytes) to ${provider}`);
101
+ });
102
+
103
+ // Check health across active connections
104
+ const health = await storageManager.checkHealth('Local_Disk');
105
+ console.log('Storage status:', health.status);
106
+ ```
107
+
108
+ ### As Express Middleware
109
+
110
+ ```js
111
+ import express from 'express';
112
+ import { createBucketRouter } from 'mbkbucket';
113
+ import { sessRole, sessPerm } from 'mbkauthe';
114
+
115
+ const app = express();
116
+
117
+ // Mount the bucket dashboard and API under /mbkbucket
118
+ app.use(createBucketRouter({
119
+ authorization: {
120
+ view: sessRole('admin'),
121
+ upload: sessPerm('storage.upload'),
122
+ delete: sessRole('superadmin'),
123
+ },
124
+ publiViewEnabled: true,
125
+ }));
126
+
127
+ app.listen(3004, () => {
128
+ console.log('Server running on http://localhost:3004');
129
+ });
130
+ ```
131
+
132
+ ### As a Standalone Application
133
+
134
+ ```js
135
+ import mbkbucket from 'mbkbucket';
136
+
137
+ // mbkbucket is a pre-configured Express app with mbkauthe sessions, views, and routes
138
+ mbkbucket.listen(3004, () => {
139
+ console.log('mbkbucket running on http://localhost:3004/mbkbucket');
140
+ });
141
+ ```
142
+
143
+ ---
144
+
145
+ ## Supported Storage Providers ๐Ÿ—„๏ธ
146
+
147
+ mbkbucket v4.1.0 supports multiple object and file storage providers simultaneously:
148
+
149
+ | Provider | Type Identifier | Features / Capabilities |
150
+ |---|---|---|
151
+ | **Local Filesystem** | `local` / `local-disk` / `disk` | Cross-platform (Win/Linux/macOS), local directory storage, quota limits, range streaming, zero cloud latency |
152
+ | **AWS S3** | `s3` | Multipart upload, range streaming, server-side copy/move |
153
+ | **Cloudflare R2** | `s3` / `r2` | S3-compatible, zero egress fees, multipart upload, range streaming |
154
+ | **Google Drive** | `gdrive` / `google-drive` | OAuth 2.0 & Service Account auth, native folder hierarchy, direct download URLs, range requests |
155
+ | **MinIO** | `s3` / `minio` | Self-hosted S3-compatible storage, multipart upload |
156
+ | **IDrive e2** | `s3` | S3-compatible high-performance object storage |
157
+ | **Backblaze B2** | `s3` | S3-compatible API endpoints |
158
+ | **DigitalOcean Spaces** | `s3` | S3-compatible object storage |
159
+ | **Custom Drivers** | Any custom name | Register via `storageManager.registerDriver(type, factory)` |
160
+
161
+ ---
162
+
163
+ ## CLI Usage ๐Ÿ’ป
164
+
165
+ mbkbucket includes a standalone CLI server runner with built-in configuration verification, port binding, and browser launching.
166
+
167
+ ### Run Standalone Server
168
+
169
+ ```bash
170
+ # Run with npx without installation
171
+ npx mbkbucket
172
+
173
+ # Or after installing globally or in your repository
174
+ mbkbucket
175
+ ```
176
+
177
+ ### CLI Options
178
+
179
+ | Option | Shorthand | Description |
180
+ |---|---|---|
181
+ | `--port <number>` | `-p` | Port to listen on (default: `3004` or `$PORT`) |
182
+ | `--host <host>` | `-H` | Host address to bind to (default: `0.0.0.0` or `$HOST`) |
183
+ | `--app <name>` | `-a` | Override `APP_NAME` for bucket key prefix isolation |
184
+ | `--bucket <name>` | `-b` | Override default bucket / storage connection name |
185
+ | `--env <path>` | `-e` | Load environment variables from a custom `.env` file |
186
+ | `--dev` | `-d` | Run in development mode (`NODE_ENV=dev`) |
187
+ | `--open` | `-o` | Automatically open dashboard in default browser |
188
+ | `--version` | `-v` | Show version number |
189
+ | `--help` | `-h` | Show help text |
190
+
191
+ ### CLI Examples
192
+
193
+ ```bash
194
+ # Start server on custom port and open browser
195
+ mbkbucket -p 8080 --open
196
+
197
+ # Start with a specific production .env file and application prefix
198
+ mbkbucket --env ./configs/.env.prod -a portalapp
199
+ ```
200
+
201
+ ---
202
+
203
+ ## Library API ๐Ÿ“š
204
+
205
+ Full TypeScript declarations are available in [index.d.ts](index.d.ts).
206
+
207
+ ### File Operations
208
+
209
+ | Function | Description |
210
+ |---|---|
211
+ | `uploadFile(key, buffer, contentType, options?)` | Upload a file buffer/stream with options (`preventOverwrite`, `metadata`, `bucketName`/`connectionName`) |
212
+ | `downloadFile(key, options?)` | Download a file as a readable stream with metadata and headers |
213
+ | `deleteFile(key, connectionName?)` | Delete a single file |
214
+ | `deleteFiles(keys, connectionName?)` | Batch delete an array of files |
215
+ | `listfiles(prefix?, options?)` | List files and folders with pagination, search, or delimiter filtering |
216
+ | `getFileMetadata(key, connectionName?)` | Retrieve file metadata without downloading content |
217
+ | `fileExists(key, connectionName?)` | Check if a file exists |
218
+ | `getFileSize(key, connectionName?)` | Get file size in bytes (returns `number` or `null`) |
219
+
220
+ ### Folder, Copy & Move Operations
221
+
222
+ | Function | Description |
223
+ |---|---|
224
+ | `createFolder(prefix, connectionName?, options?)` | Create a folder / directory marker |
225
+ | `copyFile(sourceKey, destKey, options?, connectionName?)` | Copy a file within storage |
226
+ | `moveFile(sourceKey, destKey, options?, connectionName?)` | Move or rename a file within storage |
227
+ | `deleteFolder(prefix, connectionName?)` | Recursively delete all files under a folder prefix |
228
+
229
+ ### Multipart Upload & Cleanup Scheduler
230
+
231
+ *(Supported on S3-compatible providers)*
232
+
233
+ | Function / Export | Description |
234
+ |---|---|
235
+ | `createMultipartUpload(key, contentType?, metadata?, connectionName?)` | Initialize a multipart upload session |
236
+ | `uploadPart(key, uploadId, partNumber, buffer, connectionName?)` | Upload an individual chunk part |
237
+ | `completeMultipartUpload(key, uploadId, parts, connectionName?)` | Finalize and assemble multipart upload |
238
+ | `abortMultipartUpload(key, uploadId, connectionName?)` | Abort and cancel a multipart upload |
239
+ | `listIncompleteMultipartUploads(prefix?, connectionName?)` | List pending / incomplete multipart uploads |
240
+ | `cleanupIncompleteMultipartUploads(olderThanDays?, prefix?, connectionName?)` | Clean up abandoned multipart chunks |
241
+ | `CleanupScheduler` / `cleanupScheduler` | Automated background scheduler to purge incomplete uploads on a recurring interval |
242
+
243
+ ```js
244
+ import { cleanupScheduler } from 'mbkbucket';
245
+
246
+ // Start automated daily background cleanup for uploads older than 7 days
247
+ cleanupScheduler.start({
248
+ intervalHours: 24,
249
+ olderThanDays: 7
250
+ });
251
+ ```
252
+
253
+ ### Storage & Connection Management
254
+
255
+ | Function / Export | Description |
256
+ |---|---|
257
+ | `getAvailableStorageNames()` / `getAvailableBucketNames()` | List all configured storage connection identifiers |
258
+ | `resolveStorageName(name?)` / `resolveBucketName(name?)` | Resolve and validate target connection name (falls back to default) |
259
+ | `getStorageProvider(name?)` | Get the initialized `StorageProvider` instance for a connection |
260
+ | `getStorageConfig(name?)` / `getBucketConfig(name?)` | Retrieve connection credentials and configuration object |
261
+ | `getBucketClient(bucketName?)` | Get the underlying AWS SDK S3Client instance (for S3 connections) |
262
+ | `checkHealth(name?)` | Execute health and connectivity check on a storage connection |
263
+ | `runHealthCheck(name?)` | Run diagnostic health check with console / debug output |
264
+
265
+ ### Storage Core & Custom Drivers
266
+
267
+ mbkbucket exposes its core abstraction classes so you can extend, inspect, or listen to storage events programmatically:
268
+
269
+ ```js
270
+ import { StorageProvider, LocalStorageProvider, StorageManager, storageManager } from 'mbkbucket';
271
+
272
+ // Listen to lifecycle events
273
+ storageManager.on('file:uploaded', (e) => console.log('Uploaded:', e.key, e.provider));
274
+ storageManager.on('file:deleted', (e) => console.log('Deleted:', e.key));
275
+ storageManager.on('folder:created', (e) => console.log('Folder created:', e.prefix));
276
+
277
+ // Register a custom storage driver
278
+ storageManager.registerDriver('custom-ftp', (name, config) => {
279
+ return new MyCustomFtpProvider(name, config);
280
+ });
281
+
282
+ // Register a runtime connection using the driver
283
+ storageManager.registerConnection('backup_ftp', {
284
+ type: 'custom-ftp',
285
+ host: 'ftp.example.com'
286
+ });
287
+ ```
288
+
289
+ Exported classes:
290
+ - `StorageProvider`: Abstract base class with required contracts (`uploadFile`, `downloadFile`, `listFiles`, `deleteFile`, `copyFile`, `moveFile`, `createFolder`, `checkHealth`, etc.) and capability assertions.
291
+ - `LocalStorageProvider`: Cross-platform local disk provider (Windows, Linux, macOS) with quota calculation and path sanitization.
292
+ - `S3StorageProvider`: AWS S3, Cloudflare R2, MinIO, IDrive e2 implementation.
293
+ - `GoogleDriveStorageProvider`: Google Drive OAuth 2.0 and Service Account implementation.
294
+ - `StorageManager` / `storageManager`: Connection registry, driver repository, `EventEmitter` lifecycle orchestrator, and health monitor.
295
+ - `StorageItem`, `StorageFile`, `StorageFolder`, `StorageListResult`: Standardized metadata models.
296
+ - Custom Errors: `StorageError`, `StorageNotFoundError`, `StorageAccessDeniedError`, `StorageConflictError`, `StorageValidationError`, `StorageConfigError`, `StorageQuotaExceededError`, `StorageUnsupportedOperationError`.
297
+
298
+ ### Metrics & Observability
299
+
300
+ mbkbucket includes a Prometheus-compatible metrics registry tracking storage operations, throughput, durations, errors, and active sessions:
301
+
302
+ ```js
303
+ import { metricsRegistry } from 'mbkbucket';
304
+
305
+ // Retrieve snapshot statistics
306
+ const stats = metricsRegistry.getStats();
307
+ console.log('Total ops:', stats.operations);
308
+ console.log('Bytes transferred:', stats.bytesTransferred);
309
+
310
+ // Export Prometheus exposition format
311
+ const prometheusText = metricsRegistry.toPrometheusText();
312
+ ```
313
+
314
+ Endpoints `/mbkbucket/api/metrics` (JSON) and `/mbkbucket/metrics` (Prometheus text) are automatically exposed when mounting the router.
315
+
316
+ ### Google Drive Authentication Helpers
317
+
318
+ mbkbucket exports helper functions for handling Google Drive OAuth 2.0 flows:
319
+
320
+ | Export | Description |
321
+ |---|---|
322
+ | `getGoogleAuthUrl(options)` | Generate Google OAuth 2.0 consent URL (`clientId`, `redirectUri`, `state`) |
323
+ | `exchangeCodeForTokens(options)` | Exchange authorization code for access and refresh tokens |
324
+ | `refreshGoogleAccessToken(options)` | Refresh an expired access token using a refresh token |
325
+ | `getServiceAccountAccessToken(options)` | Generate access token using Google Cloud Service Account credentials |
326
+ | `GOOGLE_DRIVE_SCOPES` | Standard OAuth scope strings for Google Drive (`drive.file`, `drive`, etc.) |
327
+
328
+ ### Key Prefixing & App Isolation
329
+
330
+ | Function | Description |
331
+ |---|---|
332
+ | `getAppName()` | Retrieve the active application namespace from `mbkautheVar.APP_NAME` |
333
+ | `ensureKeyHasAppPrefix(key?)` | Prefix a file key with the app prefix to enforce tenant isolation |
334
+ | `ensurePrefix(prefix?)` | Prefix a folder path with the app prefix |
335
+ | `validateKeySafety(str, isKey?)` | Guard against path traversal attacks (`..`, null bytes, illegal characters) |
336
+
337
+ ### Config & Diagnostics
338
+
339
+ | Export | Description |
340
+ |---|---|
341
+ | `packageJson` | mbkbucket package metadata (version, license, etc.) |
342
+ | `appVersion` | Detected version of the host application |
343
+ | `mbkbucketVar` | Parsed and validated `mbkbucketVar` configuration |
344
+ | `checkVersion()` | Check GitHub releases for newer versions of mbkbucket |
345
+ | `validateConfiguration()` | Validate `mbkbucketVar` settings |
346
+ | `validateBucketConnection()` | Validate `BucketConnection` syntax and provider schemas |
347
+ | `validateAllConfiguration()` | Run all configuration validations |
348
+
349
+ ### Express Integration
350
+
351
+ | Export | Description |
352
+ |---|---|
353
+ | `bucket` (named) | Pre-configured Express router with superadmin protection |
354
+ | `createBucketRouter(options)` | Configurable router factory with custom permission middleware |
355
+ | `default` | Standalone Express server application ready for `.listen()` |
356
+
357
+ ---
358
+
359
+ ## Express Router & HTTP API ๐Ÿ–ฅ๏ธ
360
+
361
+ ### Endpoints
362
+
363
+ All routes are mounted relative to the router path (typically `/mbkbucket`):
364
+
365
+ | Endpoint | Method | Default Access | Description |
366
+ |---|---|---|---|
367
+ | `/mbkbucket` | `GET` | `view` auth | Admin file manager dashboard UI |
368
+ | `/mbkbucket/info` | `GET` | Public / Any | Health check, provider capabilities, and runtime config info |
369
+ | `/mbkbucket/view/*key` | `GET` | `view` auth | Inline viewer with byte-range streaming for video, audio, PDF, images, text |
370
+ | `/mbkbucket/player/*key` | `GET` | `view` auth | Dedicated media player page for video and audio playback |
371
+ | `/mbkbucket/p_view/*key` | `GET` | Public | Public file viewer (enabled when `publiView_enabled` is `"true"`) |
372
+ | `/mbkbucket/download/*key` | `GET` | `view` auth | Download file with `Content-Disposition: attachment` headers |
373
+ | `/mbkbucket/api/files` | `GET` | `view` auth | List files and folders with pagination, search, and continuation tokens |
374
+ | `/mbkbucket/api/file-info/*key`| `GET` | `view` auth | Get metadata (Content-Type, size, ETag, last modified) for a key |
375
+ | `/mbkbucket/api/health` | `GET` | `view` auth | Check connectivity for active storage connection |
376
+ | `/mbkbucket/api/metrics` | `GET` | `view` auth | Prometheus metrics serialized as JSON |
377
+ | `/mbkbucket/metrics` | `GET` | `view` auth | Prometheus exposition format text output |
378
+ | `/mbkbucket/api/signed-url` | `GET` | `upload` auth | Generate pre-signed URL for direct client uploads/downloads |
379
+ | `/mbkbucket/api/incomplete-uploads` | `GET` | `view` auth | List incomplete multipart uploads |
380
+ | `/mbkbucket/api/cleanup-uploads` | `POST` | `delete` auth | Clean up incomplete multipart uploads older than specified days |
381
+ | `/mbkbucket/upload` | `POST` | `upload` auth | Upload a single file via `multipart/form-data` with conflict detection |
382
+ | `/mbkbucket/upload-init` | `POST` | `upload` auth | Initiate multipart upload session |
383
+ | `/mbkbucket/upload-chunk` | `POST` | `upload` auth | Upload individual multipart chunk (up to 50MB) |
384
+ | `/mbkbucket/upload-complete` | `POST` | `upload` auth | Assemble and complete multipart upload |
385
+ | `/mbkbucket/upload-abort` | `POST` | `delete` auth | Abort pending multipart upload session |
386
+ | `/mbkbucket/create-folder` | `POST` | `upload` auth | Create a new directory folder marker |
387
+ | `/mbkbucket/delete` | `POST` | `delete` auth | Delete a single file, batch array of keys, or folder |
388
+ | `/mbkbucket/move` | `POST` | `upload` auth | Move or rename a file (`sourceKey` to `destKey`) |
389
+ | `/mbkbucket/copy` | `POST` | `upload` auth | Copy or duplicate a file (`sourceKey` to `destKey`) |
390
+
391
+ ### Host-Controlled Permissions
392
+
393
+ You can configure granular authorization per operation using `createBucketRouter`:
394
+
395
+ ```js
396
+ import { createBucketRouter } from 'mbkbucket';
397
+ import { sessPerm } from 'mbkauthe';
398
+ import { Permissions } from './permissions.js';
399
+
400
+ app.use(createBucketRouter({
401
+ authorization: {
402
+ view: sessPerm(Permissions.storage.view),
403
+ upload: sessPerm(Permissions.storage.upload),
404
+ delete: sessPerm(Permissions.storage.delete),
405
+ },
406
+ publiViewEnabled: false,
407
+ }));
408
+ ```
409
+
410
+ - When no custom authorization is passed, `sessRole('superadmin')` protects all private endpoints by default.
411
+ - Passing a single middleware function to `authorization` applies that guard across all three operations.
412
+
413
+ ### Templates & Views
414
+
415
+ The Express router renders Handlebars (`.hbs`) views located in `views/`:
416
+
417
+ | Template / Partial | Description |
418
+ |---|---|
419
+ | `bucketportal.hbs` | Dashboard shell layout |
420
+ | `bucketadmincontent.hbs` | File manager interface with tree sidebar and storage switcher |
421
+ | `bucketadmin_alerts.hbs` | UI alerts and toast partial |
422
+ | `bucketadmin_delete_modal.hbs` | Item and folder deletion confirmation modal |
423
+ | `bucketadmin_move_modal.hbs` | File move and rename modal with destination selection |
424
+ | `bucketadmin_newfolder_modal.hbs` | New folder creation modal |
425
+ | `bucketadmin_preview_modal.hbs` | Full-screen media preview modal (images, video, audio, code, PDF) |
426
+ | `bucketadmin_upload_modal.hbs` | Chunked file upload modal with progress bars |
427
+ | `bucketadmin_filelist_skeleton.hbs`| Loading state placeholder skeleton |
428
+ | `bucket.hbs` | Public file viewer page |
429
+ | `mbkbucket_info.hbs` | Diagnostics, active connections, and environment info page |
430
+
431
+ ---
432
+
433
+ ## Frontend Helper Client ๐ŸŽจ
434
+
435
+ mbkbucket bundles a lightweight, zero-dependency client library served directly from your app:
436
+
437
+ - Script: `/mbkbucket/mbkbucket-helper.js`
438
+ - Styles: `/mbkbucket/mbkbucket-helper.css`
439
+ - Live interactive demo: `/mbkbucket/helper-demo`
440
+
441
+ ### Including the Helper
442
+
443
+ ```html
444
+ <link rel="stylesheet" href="/mbkbucket/mbkbucket-helper.css">
445
+ <script src="/mbkbucket/mbkbucket-helper.js"></script>
446
+ ```
447
+
448
+ ### Features
449
+
450
+ #### 1. Gallery Browser Modal
451
+ Open a rich modal browser to allow users to select one or multiple files directly from storage:
452
+
453
+ ```js
454
+ MBKBucket.openGallery({
455
+ title: 'Choose Profile Banner',
456
+ filter: 'image', // 'image' | 'video' | 'audio' | 'pdf' | 'all'
457
+ multiple: false,
458
+ onSelect: (selectedFiles) => {
459
+ console.log('Selected:', selectedFiles[0].url, selectedFiles[0].key);
460
+ }
461
+ });
462
+ ```
463
+
464
+ #### 2. File Picker Inputs
465
+ Bind an existing `<input>` element with auto-attached browse buttons and live thumbnail previews:
466
+
467
+ ```html
468
+ <input type="text" data-mbk-picker data-filter="image" data-preview="#avatar-preview">
469
+ <img id="avatar-preview" src="/placeholder.png" alt="Preview">
470
+ ```
471
+
472
+ Or programmatically:
473
+ ```js
474
+ MBKBucket.createPicker(document.getElementById('my-input'), {
475
+ filter: 'image',
476
+ onSelect: (file) => console.log('Picked:', file)
477
+ });
478
+ ```
479
+
480
+ #### 3. Upload Widget
481
+ Add a drag-and-drop uploader with automatic chunking for large files:
482
+
483
+ ```html
484
+ <div data-mbk-uploader data-prefix="uploads/users" data-allowed="image/*,video/*"></div>
485
+ ```
486
+
487
+ #### 4. Client URL Builders
488
+ ```js
489
+ MBKBucket.url.view('photos/cover.jpg'); // -> /mbkbucket/view/photos/cover.jpg
490
+ MBKBucket.url.download('reports/data.xlsx'); // -> /mbkbucket/download/reports/data.xlsx
491
+ MBKBucket.url.publicView('public/logo.png'); // -> /mbkbucket/p_view/public/logo.png
492
+ MBKBucket.url.player('media/intro.mp4'); // -> /mbkbucket/player/media/intro.mp4
493
+ ```
494
+
495
+ ---
496
+
497
+ ## Environment Configuration โš™๏ธ
498
+
499
+ ### `BucketConnection` (required)
500
+
501
+ A JSON string defining one or more storage connections. You can configure **Local Disk**, **S3/R2**, and **Google Drive** connections together:
502
+
503
+ ```env
504
+ BucketConnection={"Local_Disk":{"type":"local","basePath":"./mbkbucket_storage","maxBytes":10737418240},"R2_Bucket":{"BUCKET_NAME":"my-bucket","ACCESS_KEY_ID":"your-key","SECRET_ACCESS_KEY":"your-secret","ENDPOINT":"https://<account-id>.r2.cloudflarestorage.com"},"Google_Drive":{"type":"gdrive","client_id":"your-client-id.apps.googleusercontent.com","client_secret":"your-client-secret","refresh_token":"your-refresh-token","folder_id":"root"}}
505
+ ```
506
+
507
+ #### Local Disk Connection Fields
508
+
509
+ | Field | Required | Description |
510
+ |---|---|---|
511
+ | `type` | Yes | Provider tag: `"local"`, `"local-disk"`, or `"disk"` |
512
+ | `basePath` | No | Base directory path on local disk (supports relative `./storage` or absolute `D:\storage` / `/var/storage`). Defaults to `./mbkbucket_storage`. |
513
+ | `maxBytes` | No | Storage quota limit in bytes (e.g. `10737418240` for 10 GB) |
514
+
515
+ #### S3 / Cloudflare R2 Connection Fields
516
+
517
+ | Field | Required | Description |
518
+ |---|---|---|
519
+ | `BUCKET_NAME` | Yes | Target bucket name |
520
+ | `ACCESS_KEY_ID` | Yes | S3 Access Key ID |
521
+ | `SECRET_ACCESS_KEY` | Yes | S3 Secret Access Key |
522
+ | `ENDPOINT` | Yes | S3 API endpoint URL (e.g. `https://<id>.r2.cloudflarestorage.com` or `https://s3.us-east-1.amazonaws.com`) |
523
+ | `region` | No | Region (defaults to `'auto'` for Cloudflare R2 / custom endpoints) |
524
+
525
+ #### Google Drive Connection Fields
526
+
527
+ | Field | Required | Description |
528
+ |---|---|---|
529
+ | `type` | Yes | Provider tag: `"gdrive"` or `"google-drive"` |
530
+ | `client_id` | OAuth | Google OAuth 2.0 Client ID |
531
+ | `client_secret` | OAuth | Google OAuth 2.0 Client Secret |
532
+ | `refresh_token` | OAuth | Long-lived Google OAuth 2.0 refresh token |
533
+ | `folder_id` | No | Target root folder ID in Google Drive (defaults to `"root"`) |
534
+ | `client_email` | Service Account | Service Account client email (alternative to OAuth) |
535
+ | `private_key` | Service Account | Service Account PEM private key (alternative to OAuth) |
536
+
537
+ #### Google Drive via Service Account Example
538
+
539
+ ```env
540
+ BucketConnection={"Google_Drive_SA":{"type":"gdrive","client_email":"storage-sa@project.iam.gserviceaccount.com","private_key":"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----","folder_id":"1A2B3C4D5E6F"}}
541
+ ```
542
+
543
+ ### `mbkautheVar` (required)
544
+
545
+ Standard mbkauthe configuration. Key fields relevant to storage:
546
+
547
+ | Field | Required | Description |
548
+ |---|---|---|
549
+ | `APP_NAME` | Yes | Namespace used for automatic key prefix isolation |
550
+ | `bucket` | No | Default active connection name (falls back to first key in `BucketConnection`) |
551
+ | `loginRedirectURL` | No | URL to redirect upon authentication |
552
+
553
+ ### `mbkbucketVar` (optional)
554
+
555
+ Optional behavioral toggles for viewers and streaming:
556
+
557
+ ```env
558
+ mbkbucketVar={"publiView_enabled":"true","p_view_inline":"true"}
559
+ ```
560
+
561
+ | Field | Default | Description |
562
+ |---|---|---|
563
+ | `publiView_enabled` | `false` | Enable or disable unauthenticated public file access under `/mbkbucket/p_view/*` |
564
+ | `p_view_inline` | `true` | Stream files inline in browser response (vs redirecting to external signed URLs) |
565
+
566
+ ---
567
+
568
+ ## Automated Tests ๐Ÿงช
569
+
570
+ mbkbucket uses [Vitest](https://vitest.dev/) for unit and integration testing:
571
+
572
+ ```bash
573
+ npm test
574
+ ```
575
+
576
+ ### Test Suites Covered
577
+
578
+ - **Storage Provider & Core Abstractions**: `StorageProvider` interface contracts, capability inspection, `PathStrategy` traversal security, `StorageError` hierarchy, Google Drive consent URL generation.
579
+ - **Local Storage Provider & Features**: Cross-platform path resolution, directory isolation, quota tracking (`maxBytes`), recursive directory scanning, and byte-range streaming.
580
+ - **Architecture Hardening & Reliability**: Exponential backoff on rate-limits (429/503), `StorageManager` `EventEmitter` lifecycle events, `MetricsRegistry` tracking, and `CleanupScheduler`.
581
+ - **API Routes Logic**: File listing, single and chunked multipart uploads, conflict detection (409), folder creation, single/batch deletion, move and copy operations, health endpoint, metadata inspection.
582
+ - **View Controller & Streaming**: Inline viewer, open-ended RFC 7233 byte-range streaming for PDF documents and media (`206 Partial Content`), unsupported MIME rejection (`415`), public file viewing.
583
+ - **Helpers & Error Hierarchy**: Utility helpers, range header parsing, stream buffers, cache control builders, and domain-specific error classes.
584
+ - **Configuration Validation**: S3, Local Disk, and Google Drive connection validation, malformed JSON recovery, boolean normalization, and error messaging.
585
+ - **S3 Prefix & Name Resolution**: Application namespace prefixing, delimiter handling, and bucket resolution.
586
+ - **Public API Exports**: Verification of all exported functions, classes, and proxy objects from package root.
587
+
588
+ ---
589
+
590
+ ## Contact & Support
591
+
592
+ - **Website**: [mbktech.org/Support](https://mbktech.org/Support/?Project=MIbnEKhalidWeb)
593
+ - **Email**: [support@mbktech.org](mailto:support@mbktech.org)
594
+ - **GitHub**: [MIbnEKhalid/mbkbucket](https://github.com/MIbnEKhalid/mbkbucket)
595
+
596
+ ---
597
+
598
+ ## About
599
+
600
+ Developed by [Muhammad Bin Khalid](https://github.com/MIbnEKhalid)
601
+ Part of [MBK Tech](https://mbktech.org/)
602
+
603
+ ---
604
+
605
+ ## License
606
+
607
+ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
608
+
609
+ <!--
610
+ * MBKBucket
611
+ * Copyright (c) 2026 Muhammad Bin Khalid, MBKTech.org and contributors
612
+ * Licensed under the MIT License.
613
+ * Source: https://github.com/MIbnEKhalid/mbkbucket
562
614
  -->