mbkbucket 3.5.1 โ†’ 3.7.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 (42) hide show
  1. package/.env.example +14 -14
  2. package/.mbkbucket.config.example.json +14 -14
  3. package/LICENSE +20 -20
  4. package/README.md +377 -350
  5. package/index.js +30 -30
  6. package/lib/cli.js +5 -4
  7. package/lib/public/bucketadmin.css +1695 -1420
  8. package/lib/public/bucketadmin.js +1229 -1229
  9. package/lib/public/mbkbucket-helper-demo.html +1184 -1184
  10. package/lib/public/mbkbucket-helper.css +1235 -1235
  11. package/lib/public/mbkbucket-helper.js +1557 -1557
  12. package/lib/src/app.js +109 -109
  13. package/lib/src/cli/auth-helper.js +141 -150
  14. package/lib/src/config/index.js +110 -112
  15. package/lib/src/config/validation.js +73 -73
  16. package/lib/src/controllers/api.controller.js +410 -410
  17. package/lib/src/controllers/dashboard.controller.js +49 -48
  18. package/lib/src/controllers/info.controller.js +33 -34
  19. package/lib/src/controllers/view.controller.js +231 -233
  20. package/lib/src/middleware/async-handler.js +19 -23
  21. package/lib/src/middleware/bucket-resolver.js +37 -37
  22. package/lib/src/middleware/error-handler.js +50 -54
  23. package/lib/src/middleware/rate-limiter.js +22 -22
  24. package/lib/src/middleware/security.js +21 -27
  25. package/lib/src/routes/api.routes.js +53 -40
  26. package/lib/src/routes/index.js +60 -21
  27. package/lib/src/routes/info.routes.js +9 -9
  28. package/lib/src/routes/view.routes.js +25 -20
  29. package/lib/src/services/s3.service.js +594 -603
  30. package/lib/src/utils/errors.js +46 -54
  31. package/lib/src/utils/helpers.js +212 -251
  32. package/lib/src/utils/logger.js +17 -17
  33. package/package.json +83 -83
  34. package/views/bucket.handlebars +196 -196
  35. package/views/bucketadmin_alerts.handlebars +15 -15
  36. package/views/bucketadmin_delete_modal.handlebars +36 -36
  37. package/views/bucketadmin_filelist_skeleton.handlebars +32 -32
  38. package/views/bucketadmin_newfolder_modal.handlebars +29 -29
  39. package/views/bucketadmin_preview_modal.handlebars +40 -40
  40. package/views/bucketadmin_upload_modal.handlebars +54 -54
  41. package/views/bucketadmincontent.handlebars +141 -141
  42. package/views/mbkbucket_info.handlebars +232 -232
package/README.md CHANGED
@@ -1,351 +1,378 @@
1
- # mbkbucket
2
-
3
- > Flexible S3/R2 bucket management โ€” library, Express router, and 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%3D14.0.0-brightgreen.svg)](https://nodejs.org/)
9
- [![Publish Status](https://github.com/MIbnEKhalid/mbkbucket/actions/workflows/publish.yml/badge.svg)](https://github.com/MIbnEKhalid/mbkbucket/actions/workflows/publish.yml)
10
-
11
- ---
12
-
13
- ## Table of Contents
14
-
15
- - [Installation](#installation-)
16
- - [Quick Start](#quick-start-)
17
- - [CLI Usage](#cli-usage-)
18
- - [Library API](#library-api-)
19
- - [Express Router](#express-router-)
20
- - [Environment Configuration](#environment-configuration-)
21
- - [Supported Storage Providers](#supported-storage-providers-)
22
- - [Tests](#automated-tests-)
23
- - [Support](#contact--support)
24
-
25
- ---
26
-
27
- ## Installation ๐Ÿ“ฆ
28
-
29
- ```bash
30
- npm install mbkbucket
31
- ```
32
-
33
- ---
34
-
35
- ## Quick Start ๐Ÿš€
36
-
37
- ### As a Library
38
-
39
- ```js
40
- import { uploadFile, downloadFile, listfiles, deleteFile, deleteFolder, getFileMetadata, generateSignedUrl,
41
- bucket, // Express router
42
- packageJson // Package metadata
43
- } from 'mbkbucket';
44
-
45
- // Upload a file
46
- const result = await uploadFile(
47
- 'photos/sunset.jpg',
48
- fileBuffer,
49
- 'image/jpeg',
50
- { bucketName: 'R2_Bucket' }
51
- );
52
-
53
- // List files
54
- const { Contents, hasMore } = await listfiles('photos/', {
55
- delimiter: '/',
56
- maxKeys: 100
57
- });
58
-
59
- // Download a file
60
- const { Body, ContentType } = await downloadFile('photos/sunset.jpg');
61
-
62
- // Generate a pre-signed URL (valid for 1 hour)
63
- const { url } = await generateSignedUrl('photos/sunset.jpg', 'getObject', 3600);
64
- ```
65
-
66
- ### As Express Middleware
67
-
68
- ```js
69
- import mbkbucket from 'mbkbucket';
70
-
71
- // mbkbucket is a fully configured Express app with auth, sessions, and bucket routes
72
- mbkbucket.listen(3004, () => {
73
- console.log('mbkbucket server running on http://localhost:3004');
74
- });
75
- ```
76
-
77
- The Express app includes:
78
- - **`/mbkbucket`** โ€” Admin dashboard (superadmin only)
79
- - **`/mbkbucket/api/*`** โ€” REST API for file operations
80
- - **`/mbkbucket/p_view/:key(*)`** โ€” Public file view
81
- - **`/mbkbucket/info`** โ€” Health & config info
82
-
83
- ---
84
-
85
- ## CLI Usage ๐Ÿ’ป
86
-
87
- > **โš ๏ธ Under active development.** The CLI is functional but some features may change.
88
-
89
- mbkbucket ships with a command-line interface for managing buckets directly from the terminal.
90
-
91
- ### Setup
92
-
93
- ```bash
94
- # Set your mbkauthe server URL (one-time)
95
- mbkbucket config set serverUrl https://your-server.example.com
96
-
97
- # Set your API token profile key (one-time, optional)
98
- mbkbucket config set profileKey your-profile-key
99
-
100
- # Authenticate via device flow
101
- mbkbucket login
102
- ```
103
-
104
- ### Commands
105
-
106
- | Command | Alias | Description |
107
- |---|---|---|
108
- | `mbkbucket login` | โ€” | Authenticate via mbkauthe device flow |
109
- | `mbkbucket logout` | โ€” | Clear stored credentials |
110
- | `mbkbucket whoami` | โ€” | Show login status |
111
- | | | |
112
- | `mbkbucket list [prefix]` | `ls` | List files and folders |
113
- | `mbkbucket upload <file> [key]` | `up` | Upload a file or folder |
114
- | `mbkbucket download <key> [dest]` | `dl` | Download a file or folder |
115
- | `mbkbucket delete <key>` | `rm` | Delete a file |
116
- | `mbkbucket delete-folder <prefix>` | `rmdir` | Recursively delete a folder |
117
- | `mbkbucket info <key>` | `stat` | Show file metadata (size, type, etc.) |
118
- | `mbkbucket signed-url <key>` | `sign` | Generate a pre-signed download URL |
119
- | | | |
120
- | `mbkbucket config` | `cfg` | Show all config values |
121
- | `mbkbucket config get <key>` | โ€” | Get a specific config value |
122
- | `mbkbucket config set <k> <v>` | โ€” | Set a config value |
123
- | `mbkbucket config unset <key>` | โ€” | Remove a config value |
124
- | `mbkbucket config path` | โ€” | Show config file location |
125
- | `mbkbucket config reset` | โ€” | Reset to demo values |
126
- | `mbkbucket config edit` | โ€” | Open config file in editor |
127
-
128
- ### Global Options
129
-
130
- | Flag | Description |
131
- |---|---|
132
- | `--app`, `-a <name>` | Override `APP_NAME` for key prefix isolation |
133
- | `--bucket`, `-b <name>` | Override default bucket name |
134
- | `--help`, `-h` | Show help |
135
-
136
- ### Login Options
137
-
138
- | Flag | Description |
139
- |---|---|
140
- | `--server <url>` | mbkauthe server URL |
141
- | `--profile-key <k>` | API token profile key |
142
-
143
- ### Config File
144
-
145
- CLI configuration is stored at `~/.mbkbucket/config.json`. A demo config is created automatically on first use.
146
-
147
- ---
148
-
149
- ## Library API ๐Ÿ“š
150
-
151
- All functions are exported from the package root. Full TypeScript declarations in [index.d.ts](index.d.ts).
152
-
153
- ### File Operations
154
-
155
- | Function | Description |
156
- |---|---|
157
- | `uploadFile(key, buffer, contentType, options?)` | Upload a file |
158
- | `downloadFile(key, options?)` | Download a file |
159
- | `deleteFile(key, bucketName?)` | Delete a single file |
160
- | `deleteFiles(keys, bucketName?)` | Batch delete up to 1000 files |
161
- | `deleteFolder(prefix, bucketName?)` | Recursively delete all files under a prefix |
162
- | `listfiles(prefix?, options?)` | List files with pagination and filtering |
163
- | `getFileMetadata(key, bucketName?)` | Get file metadata without downloading |
164
- | `fileExists(key, bucketName?)` | Check if a file exists |
165
- | `getFileSize(key, bucketName?)` | Get file size in bytes |
166
- | `generateSignedUrl(key, operation?, expiresIn?, bucketName?)` | Generate a pre-signed URL |
167
-
168
- ### Multipart Upload
169
-
170
- | Function | Description |
171
- |---|---|
172
- | `createMultipartUpload(key, contentType?, metadata?, bucketName?)` | Start a multipart upload |
173
- | `uploadPart(key, uploadId, partNumber, buffer, bucketName?)` | Upload a single part |
174
- | `completeMultipartUpload(key, uploadId, parts, bucketName?)` | Complete a multipart upload |
175
- | `abortMultipartUpload(key, uploadId, bucketName?)` | Abort a multipart upload |
176
- | `listIncompleteMultipartUploads(prefix?, bucketName?)` | List incomplete uploads |
177
- | `cleanupIncompleteMultipartUploads(olderThanDays?, prefix?, bucketName?)` | Clean up stale uploads |
178
-
179
- ### Bucket Management
180
-
181
- | Function | Description |
182
- |---|---|
183
- | `getAvailableBucketNames()` | List configured bucket names |
184
- | `resolveBucketName(bucketName?)` | Resolve and validate a bucket name |
185
- | `getBucketConfig(bucketName?)` | Get bucket credentials/config |
186
- | `getBucketClient(bucketName?)` | Get an S3 client instance |
187
- | `checkHealth()` | Check S3 connectivity |
188
-
189
- ### Key Prefixing
190
-
191
- | Function | Description |
192
- |---|---|
193
- | `ensureKeyHasAppPrefix(key?)` | Prefix a key with the app name |
194
- | `ensurePrefix(prefix?)` | Prefix a folder path with the app name |
195
- | `getAppName()` | Get the current app name |
196
-
197
- ### Config
198
-
199
- | Export | Description |
200
- |---|---|
201
- | `packageJson` | Package metadata (version, etc.) |
202
- | `appVersion` | Parent project version |
203
- | `mbkbucketVar` | Parsed mbkbucketVar config |
204
- | `checkVersion()` | Check for newer package versions |
205
-
206
- ### Express
207
-
208
- | Export | Description |
209
- |---|---|
210
- | `bucket` (named) | Express Router with all bucket routes |
211
- | `default` | Fully configured Express application |
212
-
213
- ---
214
-
215
- ## Express Router ๐Ÿ–ฅ๏ธ
216
-
217
- ### Routes
218
-
219
- | Route | Auth | Description |
220
- |---|---|---|
221
- | `GET /mbkbucket` | superadmin | Admin dashboard |
222
- | `GET /mbkbucket/info` | Any | Health check & config info |
223
- | `GET /mbkbucket/p_view/:key(*)` | Public | Public file viewer |
224
- | `POST /mbkbucket/api/list` | superadmin | List files (JSON) |
225
- | `POST /mbkbucket/api/upload` | superadmin | Upload file(s) |
226
- | `POST /mbkbucket/api/delete` | superadmin | Delete file(s) |
227
- | `POST /mbkbucket/api/delete-folder` | superadmin | Delete folder |
228
- | `POST /mbkbucket/api/create-folder` | superadmin | Create folder marker |
229
- | `POST /mbkbucket/api/rename` | superadmin | Rename/move a file |
230
- | `GET /mbkbucket/api/download/:key(*)` | superadmin | Download a file |
231
- | `GET /mbkbucket/api/metadata/:key(*)` | superadmin | Get file metadata |
232
- | `POST /mbkbucket/api/multipart/init` | superadmin | Init multipart upload |
233
- | `POST /mbkbucket/api/multipart/part` | superadmin | Upload multipart chunk |
234
- | `POST /mbkbucket/api/multipart/complete` | superadmin | Complete multipart |
235
- | `POST /mbkbucket/api/multipart/abort` | superadmin | Abort multipart |
236
- | `GET /mbkbucket/api/multipart/list` | superadmin | List incomplete uploads |
237
- | `POST /mbkbucket/api/multipart/cleanup` | superadmin | Clean up stale uploads |
238
-
239
- ### Views
240
-
241
- | Template | Description |
242
- |---|---|
243
- | `bucketportal.handlebars` | Admin dashboard shell |
244
- | `bucketadmincontent.handlebars` | File listing & management UI |
245
- | `bucketadmin_alerts.handlebars` | Alert/notification partial |
246
- | `bucketadmin_delete_modal.handlebars` | Delete confirmation modal |
247
- | `bucketadmin_preview_modal.handlebars` | File preview modal |
248
- | `bucketadmin_filelist_skeleton.handlebars` | Loading skeleton |
249
- | `bucket.handlebars` | Public file viewer |
250
- | `mbkbucket_info.handlebars` | Info/health page |
251
-
252
- ---
253
-
254
- ## Environment Configuration โš™๏ธ
255
-
256
- ### `BucketConnection` (required)
257
-
258
- JSON mapping of bucket names to S3-compatible credentials:
259
-
260
- ```env
261
- BucketConnection={"R2_Bucket":{"BUCKET_NAME":"my-bucket","ACCESS_KEY_ID":"...","SECRET_ACCESS_KEY":"...","ENDPOINT":"https://<id>.r2.cloudflarestorage.com"}}
262
- ```
263
-
264
- | Field | Required | Description |
265
- |---|---|---|
266
- | `BUCKET_NAME` | Yes | Target bucket name |
267
- | `ACCESS_KEY_ID` | Yes | Access key |
268
- | `SECRET_ACCESS_KEY` | Yes | Secret key |
269
- | `ENDPOINT` | Yes | S3-compatible endpoint URL |
270
- | `region` | No | AWS region (default: `auto`) |
271
-
272
- ### `mbkautheVar` (required)
273
-
274
- Standard mbkauthe configuration. Key fields for mbkbucket:
275
-
276
- | Field | Description |
277
- |---|---|
278
- | `APP_NAME` | App name used for key prefix isolation |
279
- | `bucket` | Default bucket name (falls back to first in `BucketConnection`) |
280
- | `CLI_AUTH_ENABLED` | Enable CLI device-flow login (`"true"` / `"false"`) |
281
-
282
- ### `mbkbucketVar` (optional)
283
-
284
- | Field | Default | Description |
285
- |---|---|---|
286
- | `publiView_enabled` | `"false"` | Enable public file viewer |
287
- | `p_view_inline` | `"false"` | Show files inline vs download |
288
-
289
- ### Common Mistakes
290
-
291
- ```env
292
- # โŒ Wrong โ€” inner object quoted as string
293
- BucketConnection={"R2_Bucket":"{\"BUCKET_NAME\":\"...\"}"}
294
-
295
- # โœ… Correct โ€” proper JSON nesting
296
- BucketConnection={"R2_Bucket":{"BUCKET_NAME":"...","ACCESS_KEY_ID":"...","SECRET_ACCESS_KEY":"...","ENDPOINT":"https://..."}}
297
- ```
298
-
299
- ---
300
-
301
- ## Supported Storage Providers ๐Ÿ—„๏ธ
302
-
303
- Any S3-compatible object storage:
304
-
305
- - **AWS S3**
306
- - **Cloudflare R2**
307
- - **IDrive e2**
308
- - **MinIO**
309
- - **Backblaze B2** (via S3-compatible API)
310
- - **DigitalOcean Spaces**
311
-
312
- ---
313
-
314
- ## Automated Tests ๐Ÿงช
315
-
316
- ```bash
317
- npm test
318
- ```
319
-
320
- Covers:
321
- - `mbkbucketVar` parsing, defaults, and boolean normalization
322
- - `BucketConnection` shape validation and required fields
323
-
324
- ---
325
-
326
- ## Contact & Support
327
-
328
- - **Website**: [mbktech.org/Support](https://mbktech.org/Support/?Project=MIbnEKhalidWeb)
329
- - **Email**: [support@mbktech.org](mailto:support@mbktech.org)
330
- - **GitHub**: [MIbnEKhalid/mbkbucket](https://github.com/MIbnEKhalid/mbkbucket)
331
-
332
- ---
333
-
334
- ## About
335
-
336
- Developed by [Muhammad Bin Khalid](https://github.com/MIbnEKhalid)
337
- Part of [MBK Tech](https://mbktech.org/)
338
-
339
- ---
340
-
341
- ## License
342
-
343
- Licensed under the MIT License. See [LICENSE](LICENSE) for details.
344
-
345
-
346
- <!--
347
- * MBKBucket
348
- * Copyright (c) 2026 Muhammad Bin Khalid, MBKTech.org and contributors
349
- * Licensed under the MIT License.
350
- * Source: https://github.com/MIbnEKhalid/mbkbucket
1
+ # mbkbucket
2
+
3
+ > Flexible S3/R2 bucket management โ€” library, Express router, and 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%3D14.0.0-brightgreen.svg)](https://nodejs.org/)
9
+ [![Check npm version](https://github.com/MIbnEKhalid/mbkbucket/actions/workflows/checkLatestVersion.yml/badge.svg)]
10
+
11
+ ---
12
+
13
+ ## Table of Contents
14
+
15
+ - [Installation](#installation-)
16
+ - [Quick Start](#quick-start-)
17
+ - [CLI Usage](#cli-usage-)
18
+ - [Library API](#library-api-)
19
+ - [Express Router](#express-router-)
20
+ - [Environment Configuration](#environment-configuration-)
21
+ - [Supported Storage Providers](#supported-storage-providers-)
22
+ - [Tests](#automated-tests-)
23
+ - [Support](#contact--support)
24
+
25
+ ---
26
+
27
+ ## Installation ๐Ÿ“ฆ
28
+
29
+ ```bash
30
+ npm install mbkbucket
31
+ ```
32
+
33
+ ---
34
+
35
+ ## Quick Start ๐Ÿš€
36
+
37
+ ### As a Library
38
+
39
+ ```js
40
+ import { uploadFile, downloadFile, listfiles, deleteFile, deleteFolder, getFileMetadata, generateSignedUrl,
41
+ bucket, // Express router
42
+ packageJson // Package metadata
43
+ } from 'mbkbucket';
44
+
45
+ // Upload a file
46
+ const result = await uploadFile(
47
+ 'photos/sunset.jpg',
48
+ fileBuffer,
49
+ 'image/jpeg',
50
+ { bucketName: 'R2_Bucket' }
51
+ );
52
+
53
+ // List files
54
+ const { Contents, hasMore } = await listfiles('photos/', {
55
+ delimiter: '/',
56
+ maxKeys: 100
57
+ });
58
+
59
+ // Download a file
60
+ const { Body, ContentType } = await downloadFile('photos/sunset.jpg');
61
+
62
+ // Generate a pre-signed URL (valid for 1 hour)
63
+ const { url } = await generateSignedUrl('photos/sunset.jpg', 'getObject', 3600);
64
+ ```
65
+
66
+ ### As Express Middleware
67
+
68
+ ```js
69
+ import mbkbucket from 'mbkbucket';
70
+
71
+ // mbkbucket is a fully configured Express app with auth, sessions, and bucket routes
72
+ mbkbucket.listen(3004, () => {
73
+ console.log('mbkbucket server running on http://localhost:3004');
74
+ });
75
+ ```
76
+
77
+ ### Host-controlled permissions
78
+
79
+ The default `bucket` router remains superadmin-only. A host application can
80
+ provide its own mbkauthe permission instead:
81
+
82
+ ```js
83
+ import { createBucketRouter } from 'mbkbucket';
84
+ import { sessPerm } from 'mbkauthe';
85
+ import { Permissions } from './permissions.js';
86
+
87
+ app.use(createBucketRouter({
88
+ authorization: {
89
+ view: sessPerm(Permissions.storage.view),
90
+ upload: sessPerm(Permissions.storage.upload),
91
+ delete: sessPerm(Permissions.storage.delete),
92
+ },
93
+ }));
94
+ ```
95
+
96
+ Define `Permissions.storage.view`, `Permissions.storage.upload`, and
97
+ `Permissions.storage.delete` in the host application's permission manifest and
98
+ sync that manifest with mbkauthe. The supplied middleware controls the
99
+ dashboard, private views, and bucket APIs; public views and info endpoints keep
100
+ their existing behavior. When no middleware is supplied, mbkbucket uses
101
+ `sessRole('superadmin')` for every private operation. A single middleware may
102
+ also be passed as `authorization` to protect all three operations uniformly.
103
+
104
+ The Express app includes:
105
+ - **`/mbkbucket`** โ€” Admin dashboard (superadmin only)
106
+ - **`/mbkbucket/api/*`** โ€” REST API for file operations
107
+ - **`/mbkbucket/p_view/:key(*)`** โ€” Public file view
108
+ - **`/mbkbucket/info`** โ€” Health & config info
109
+
110
+ ---
111
+
112
+ ## CLI Usage ๐Ÿ’ป
113
+
114
+ > **โš ๏ธ Under active development.** The CLI is functional but some features may change.
115
+
116
+ mbkbucket ships with a command-line interface for managing buckets directly from the terminal.
117
+
118
+ ### Setup
119
+
120
+ ```bash
121
+ # Set your mbkauthe server URL (one-time)
122
+ mbkbucket config set serverUrl https://your-server.example.com
123
+
124
+ # Set your API token profile key (one-time, optional)
125
+ mbkbucket config set profileKey your-profile-key
126
+
127
+ # Authenticate via device flow
128
+ mbkbucket login
129
+ ```
130
+
131
+ ### Commands
132
+
133
+ | Command | Alias | Description |
134
+ |---|---|---|
135
+ | `mbkbucket login` | โ€” | Authenticate via mbkauthe device flow |
136
+ | `mbkbucket logout` | โ€” | Clear stored credentials |
137
+ | `mbkbucket whoami` | โ€” | Show login status |
138
+ | | | |
139
+ | `mbkbucket list [prefix]` | `ls` | List files and folders |
140
+ | `mbkbucket upload <file> [key]` | `up` | Upload a file or folder |
141
+ | `mbkbucket download <key> [dest]` | `dl` | Download a file or folder |
142
+ | `mbkbucket delete <key>` | `rm` | Delete a file |
143
+ | `mbkbucket delete-folder <prefix>` | `rmdir` | Recursively delete a folder |
144
+ | `mbkbucket info <key>` | `stat` | Show file metadata (size, type, etc.) |
145
+ | `mbkbucket signed-url <key>` | `sign` | Generate a pre-signed download URL |
146
+ | | | |
147
+ | `mbkbucket config` | `cfg` | Show all config values |
148
+ | `mbkbucket config get <key>` | โ€” | Get a specific config value |
149
+ | `mbkbucket config set <k> <v>` | โ€” | Set a config value |
150
+ | `mbkbucket config unset <key>` | โ€” | Remove a config value |
151
+ | `mbkbucket config path` | โ€” | Show config file location |
152
+ | `mbkbucket config reset` | โ€” | Reset to demo values |
153
+ | `mbkbucket config edit` | โ€” | Open config file in editor |
154
+
155
+ ### Global Options
156
+
157
+ | Flag | Description |
158
+ |---|---|
159
+ | `--app`, `-a <name>` | Override `APP_NAME` for key prefix isolation |
160
+ | `--bucket`, `-b <name>` | Override default bucket name |
161
+ | `--help`, `-h` | Show help |
162
+
163
+ ### Login Options
164
+
165
+ | Flag | Description |
166
+ |---|---|
167
+ | `--server <url>` | mbkauthe server URL |
168
+ | `--profile-key <k>` | API token profile key |
169
+
170
+ ### Config File
171
+
172
+ CLI configuration is stored at `~/.mbkbucket/config.json`. A demo config is created automatically on first use.
173
+
174
+ ---
175
+
176
+ ## Library API ๐Ÿ“š
177
+
178
+ All functions are exported from the package root. Full TypeScript declarations in [index.d.ts](index.d.ts).
179
+
180
+ ### File Operations
181
+
182
+ | Function | Description |
183
+ |---|---|
184
+ | `uploadFile(key, buffer, contentType, options?)` | Upload a file |
185
+ | `downloadFile(key, options?)` | Download a file |
186
+ | `deleteFile(key, bucketName?)` | Delete a single file |
187
+ | `deleteFiles(keys, bucketName?)` | Batch delete up to 1000 files |
188
+ | `deleteFolder(prefix, bucketName?)` | Recursively delete all files under a prefix |
189
+ | `listfiles(prefix?, options?)` | List files with pagination and filtering |
190
+ | `getFileMetadata(key, bucketName?)` | Get file metadata without downloading |
191
+ | `fileExists(key, bucketName?)` | Check if a file exists |
192
+ | `getFileSize(key, bucketName?)` | Get file size in bytes |
193
+ | `generateSignedUrl(key, operation?, expiresIn?, bucketName?)` | Generate a pre-signed URL |
194
+
195
+ ### Multipart Upload
196
+
197
+ | Function | Description |
198
+ |---|---|
199
+ | `createMultipartUpload(key, contentType?, metadata?, bucketName?)` | Start a multipart upload |
200
+ | `uploadPart(key, uploadId, partNumber, buffer, bucketName?)` | Upload a single part |
201
+ | `completeMultipartUpload(key, uploadId, parts, bucketName?)` | Complete a multipart upload |
202
+ | `abortMultipartUpload(key, uploadId, bucketName?)` | Abort a multipart upload |
203
+ | `listIncompleteMultipartUploads(prefix?, bucketName?)` | List incomplete uploads |
204
+ | `cleanupIncompleteMultipartUploads(olderThanDays?, prefix?, bucketName?)` | Clean up stale uploads |
205
+
206
+ ### Bucket Management
207
+
208
+ | Function | Description |
209
+ |---|---|
210
+ | `getAvailableBucketNames()` | List configured bucket names |
211
+ | `resolveBucketName(bucketName?)` | Resolve and validate a bucket name |
212
+ | `getBucketConfig(bucketName?)` | Get bucket credentials/config |
213
+ | `getBucketClient(bucketName?)` | Get an S3 client instance |
214
+ | `checkHealth()` | Check S3 connectivity |
215
+
216
+ ### Key Prefixing
217
+
218
+ | Function | Description |
219
+ |---|---|
220
+ | `ensureKeyHasAppPrefix(key?)` | Prefix a key with the app name |
221
+ | `ensurePrefix(prefix?)` | Prefix a folder path with the app name |
222
+ | `getAppName()` | Get the current app name |
223
+
224
+ ### Config
225
+
226
+ | Export | Description |
227
+ |---|---|
228
+ | `packageJson` | Package metadata (version, etc.) |
229
+ | `appVersion` | Parent project version |
230
+ | `mbkbucketVar` | Parsed mbkbucketVar config |
231
+ | `checkVersion()` | Check for newer package versions |
232
+
233
+ ### Express
234
+
235
+ | Export | Description |
236
+ |---|---|
237
+ | `bucket` (named) | Express Router with all bucket routes |
238
+ | `default` | Fully configured Express application |
239
+
240
+ ---
241
+
242
+ ## Express Router ๐Ÿ–ฅ๏ธ
243
+
244
+ ### Routes
245
+
246
+ | Route | Auth | Description |
247
+ |---|---|---|
248
+ | `GET /mbkbucket` | superadmin | Admin dashboard |
249
+ | `GET /mbkbucket/info` | Any | Health check & config info |
250
+ | `GET /mbkbucket/p_view/:key(*)` | Public | Public file viewer |
251
+ | `POST /mbkbucket/api/list` | superadmin | List files (JSON) |
252
+ | `POST /mbkbucket/api/upload` | superadmin | Upload file(s) |
253
+ | `POST /mbkbucket/api/delete` | superadmin | Delete file(s) |
254
+ | `POST /mbkbucket/api/delete-folder` | superadmin | Delete folder |
255
+ | `POST /mbkbucket/api/create-folder` | superadmin | Create folder marker |
256
+ | `POST /mbkbucket/api/rename` | superadmin | Rename/move a file |
257
+ | `GET /mbkbucket/api/download/:key(*)` | superadmin | Download a file |
258
+ | `GET /mbkbucket/api/metadata/:key(*)` | superadmin | Get file metadata |
259
+ | `POST /mbkbucket/api/multipart/init` | superadmin | Init multipart upload |
260
+ | `POST /mbkbucket/api/multipart/part` | superadmin | Upload multipart chunk |
261
+ | `POST /mbkbucket/api/multipart/complete` | superadmin | Complete multipart |
262
+ | `POST /mbkbucket/api/multipart/abort` | superadmin | Abort multipart |
263
+ | `GET /mbkbucket/api/multipart/list` | superadmin | List incomplete uploads |
264
+ | `POST /mbkbucket/api/multipart/cleanup` | superadmin | Clean up stale uploads |
265
+
266
+ ### Views
267
+
268
+ | Template | Description |
269
+ |---|---|
270
+ | `bucketportal.handlebars` | Admin dashboard shell |
271
+ | `bucketadmincontent.handlebars` | File listing & management UI |
272
+ | `bucketadmin_alerts.handlebars` | Alert/notification partial |
273
+ | `bucketadmin_delete_modal.handlebars` | Delete confirmation modal |
274
+ | `bucketadmin_preview_modal.handlebars` | File preview modal |
275
+ | `bucketadmin_filelist_skeleton.handlebars` | Loading skeleton |
276
+ | `bucket.handlebars` | Public file viewer |
277
+ | `mbkbucket_info.handlebars` | Info/health page |
278
+
279
+ ---
280
+
281
+ ## Environment Configuration โš™๏ธ
282
+
283
+ ### `BucketConnection` (required)
284
+
285
+ JSON mapping of bucket names to S3-compatible credentials:
286
+
287
+ ```env
288
+ BucketConnection={"R2_Bucket":{"BUCKET_NAME":"my-bucket","ACCESS_KEY_ID":"...","SECRET_ACCESS_KEY":"...","ENDPOINT":"https://<id>.r2.cloudflarestorage.com"}}
289
+ ```
290
+
291
+ | Field | Required | Description |
292
+ |---|---|---|
293
+ | `BUCKET_NAME` | Yes | Target bucket name |
294
+ | `ACCESS_KEY_ID` | Yes | Access key |
295
+ | `SECRET_ACCESS_KEY` | Yes | Secret key |
296
+ | `ENDPOINT` | Yes | S3-compatible endpoint URL |
297
+ | `region` | No | AWS region (default: `auto`) |
298
+
299
+ ### `mbkautheVar` (required)
300
+
301
+ Standard mbkauthe configuration. Key fields for mbkbucket:
302
+
303
+ | Field | Description |
304
+ |---|---|
305
+ | `APP_NAME` | App name used for key prefix isolation |
306
+ | `bucket` | Default bucket name (falls back to first in `BucketConnection`) |
307
+ | `CLI_AUTH_ENABLED` | Enable CLI device-flow login (`"true"` / `"false"`) |
308
+
309
+ ### `mbkbucketVar` (optional)
310
+
311
+ | Field | Default | Description |
312
+ |---|---|---|
313
+ | `publiView_enabled` | `"false"` | Enable public file viewer |
314
+ | `p_view_inline` | `"false"` | Show files inline vs download |
315
+
316
+ ### Common Mistakes
317
+
318
+ ```env
319
+ # โŒ Wrong โ€” inner object quoted as string
320
+ BucketConnection={"R2_Bucket":"{\"BUCKET_NAME\":\"...\"}"}
321
+
322
+ # โœ… Correct โ€” proper JSON nesting
323
+ BucketConnection={"R2_Bucket":{"BUCKET_NAME":"...","ACCESS_KEY_ID":"...","SECRET_ACCESS_KEY":"...","ENDPOINT":"https://..."}}
324
+ ```
325
+
326
+ ---
327
+
328
+ ## Supported Storage Providers ๐Ÿ—„๏ธ
329
+
330
+ Any S3-compatible object storage:
331
+
332
+ - **AWS S3**
333
+ - **Cloudflare R2**
334
+ - **IDrive e2**
335
+ - **MinIO**
336
+ - **Backblaze B2** (via S3-compatible API)
337
+ - **DigitalOcean Spaces**
338
+
339
+ ---
340
+
341
+ ## Automated Tests ๐Ÿงช
342
+
343
+ ```bash
344
+ npm test
345
+ ```
346
+
347
+ Covers:
348
+ - `mbkbucketVar` parsing, defaults, and boolean normalization
349
+ - `BucketConnection` shape validation and required fields
350
+
351
+ ---
352
+
353
+ ## Contact & Support
354
+
355
+ - **Website**: [mbktech.org/Support](https://mbktech.org/Support/?Project=MIbnEKhalidWeb)
356
+ - **Email**: [support@mbktech.org](mailto:support@mbktech.org)
357
+ - **GitHub**: [MIbnEKhalid/mbkbucket](https://github.com/MIbnEKhalid/mbkbucket)
358
+
359
+ ---
360
+
361
+ ## About
362
+
363
+ Developed by [Muhammad Bin Khalid](https://github.com/MIbnEKhalid)
364
+ Part of [MBK Tech](https://mbktech.org/)
365
+
366
+ ---
367
+
368
+ ## License
369
+
370
+ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
371
+
372
+
373
+ <!--
374
+ * MBKBucket
375
+ * Copyright (c) 2026 Muhammad Bin Khalid, MBKTech.org and contributors
376
+ * Licensed under the MIT License.
377
+ * Source: https://github.com/MIbnEKhalid/mbkbucket
351
378
  -->