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.
- package/.env.example +14 -14
- package/.mbkbucket.config.example.json +14 -14
- package/LICENSE +20 -20
- package/README.md +377 -350
- package/index.js +30 -30
- package/lib/cli.js +5 -4
- package/lib/public/bucketadmin.css +1695 -1420
- package/lib/public/bucketadmin.js +1229 -1229
- package/lib/public/mbkbucket-helper-demo.html +1184 -1184
- package/lib/public/mbkbucket-helper.css +1235 -1235
- package/lib/public/mbkbucket-helper.js +1557 -1557
- package/lib/src/app.js +109 -109
- package/lib/src/cli/auth-helper.js +141 -150
- package/lib/src/config/index.js +110 -112
- package/lib/src/config/validation.js +73 -73
- package/lib/src/controllers/api.controller.js +410 -410
- package/lib/src/controllers/dashboard.controller.js +49 -48
- package/lib/src/controllers/info.controller.js +33 -34
- package/lib/src/controllers/view.controller.js +231 -233
- package/lib/src/middleware/async-handler.js +19 -23
- package/lib/src/middleware/bucket-resolver.js +37 -37
- package/lib/src/middleware/error-handler.js +50 -54
- package/lib/src/middleware/rate-limiter.js +22 -22
- package/lib/src/middleware/security.js +21 -27
- package/lib/src/routes/api.routes.js +53 -40
- package/lib/src/routes/index.js +60 -21
- package/lib/src/routes/info.routes.js +9 -9
- package/lib/src/routes/view.routes.js +25 -20
- package/lib/src/services/s3.service.js +594 -603
- package/lib/src/utils/errors.js +46 -54
- package/lib/src/utils/helpers.js +212 -251
- package/lib/src/utils/logger.js +17 -17
- package/package.json +83 -83
- package/views/bucket.handlebars +196 -196
- package/views/bucketadmin_alerts.handlebars +15 -15
- package/views/bucketadmin_delete_modal.handlebars +36 -36
- package/views/bucketadmin_filelist_skeleton.handlebars +32 -32
- package/views/bucketadmin_newfolder_modal.handlebars +29 -29
- package/views/bucketadmin_preview_modal.handlebars +40 -40
- package/views/bucketadmin_upload_modal.handlebars +54 -54
- package/views/bucketadmincontent.handlebars +141 -141
- package/views/mbkbucket_info.handlebars +232 -232
package/lib/src/utils/errors.js
CHANGED
|
@@ -1,54 +1,46 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Centralized error classification for API and View routes.
|
|
3
|
-
* Maps error messages to HTTP status codes and machine-readable error codes.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const API_ERROR_RULES = [
|
|
7
|
-
{ match: 'bucketconnection environment variable is not set', status: 503, code: 'BUCKET_CONFIG_ERROR' },
|
|
8
|
-
{ match: ['no bucket selected', 'not found in bucketconnection'], status: 400, code: 'INVALID_BUCKET' },
|
|
9
|
-
{ match: 'already exists', status: 409, code: 'CONFLICT' },
|
|
10
|
-
{ match: 'not found', status: 404, code: 'NOT_FOUND' },
|
|
11
|
-
{ match: 'access denied', status: 403, code: 'ACCESS_DENIED' },
|
|
12
|
-
{ match: ['required', 'invalid', 'must be'], status: 400, code: 'VALIDATION_ERROR' },
|
|
13
|
-
];
|
|
14
|
-
|
|
15
|
-
const VIEW_ERROR_RULES = [
|
|
16
|
-
{ match: ['no bucket selected', 'not found in bucketconnection'], status: 400, code: 'INVALID_BUCKET' },
|
|
17
|
-
{ match: 'access denied', status: 403, code: 'ACCESS_DENIED' },
|
|
18
|
-
{ match: 'not found', status: 404, code: 'NOT_FOUND' },
|
|
19
|
-
{ match: ['invalid', 'required'], status: 400, code: 'VALIDATION_ERROR' },
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
function matchError(rules, error, fallbackMessage) {
|
|
23
|
-
const message = String(error?.message || fallbackMessage);
|
|
24
|
-
const lower = message.toLowerCase();
|
|
25
|
-
const found = rules.find(r => Array.isArray(r.match) ? r.match.some(m => lower.includes(m)) : lower.includes(r.match));
|
|
26
|
-
return { status: found?.status ?? 500, code: found?.code ?? 'INTERNAL_ERROR', message };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
timestamp: new Date().toISOString(),
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function sendViewError(res, error, fallbackMessage = 'Request failed') {
|
|
52
|
-
const { status, code, message } = classifyViewError(error, fallbackMessage);
|
|
53
|
-
return res.status(status).json({ code, message });
|
|
54
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Centralized error classification for API and View routes.
|
|
3
|
+
* Maps error messages to HTTP status codes and machine-readable error codes.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const API_ERROR_RULES = [
|
|
7
|
+
{ match: 'bucketconnection environment variable is not set', status: 503, code: 'BUCKET_CONFIG_ERROR' },
|
|
8
|
+
{ match: ['no bucket selected', 'not found in bucketconnection'], status: 400, code: 'INVALID_BUCKET' },
|
|
9
|
+
{ match: 'already exists', status: 409, code: 'CONFLICT' },
|
|
10
|
+
{ match: 'not found', status: 404, code: 'NOT_FOUND' },
|
|
11
|
+
{ match: 'access denied', status: 403, code: 'ACCESS_DENIED' },
|
|
12
|
+
{ match: ['required', 'invalid', 'must be'], status: 400, code: 'VALIDATION_ERROR' },
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const VIEW_ERROR_RULES = [
|
|
16
|
+
{ match: ['no bucket selected', 'not found in bucketconnection'], status: 400, code: 'INVALID_BUCKET' },
|
|
17
|
+
{ match: 'access denied', status: 403, code: 'ACCESS_DENIED' },
|
|
18
|
+
{ match: 'not found', status: 404, code: 'NOT_FOUND' },
|
|
19
|
+
{ match: ['invalid', 'required'], status: 400, code: 'VALIDATION_ERROR' },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
function matchError(rules, error, fallbackMessage) {
|
|
23
|
+
const message = String(error?.message || fallbackMessage);
|
|
24
|
+
const lower = message.toLowerCase();
|
|
25
|
+
const found = rules.find(r => Array.isArray(r.match) ? r.match.some(m => lower.includes(m)) : lower.includes(r.match));
|
|
26
|
+
return { status: found?.status ?? 500, code: found?.code ?? 'INTERNAL_ERROR', message };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const classifyApiError = (error, fallbackMessage = 'Request failed') => matchError(API_ERROR_RULES, error, fallbackMessage);
|
|
30
|
+
export const classifyViewError = (error, fallbackMessage = 'Request failed') => matchError(VIEW_ERROR_RULES, error, fallbackMessage);
|
|
31
|
+
|
|
32
|
+
export function sendApiError(res, error, fallbackMessage = 'Request failed') {
|
|
33
|
+
const { status, code, message } = classifyApiError(error, fallbackMessage);
|
|
34
|
+
return res.status(status).json({
|
|
35
|
+
success: false,
|
|
36
|
+
code,
|
|
37
|
+
error: { code, message },
|
|
38
|
+
message,
|
|
39
|
+
timestamp: new Date().toISOString(),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function sendViewError(res, error, fallbackMessage = 'Request failed') {
|
|
44
|
+
const { status, code, message } = classifyViewError(error, fallbackMessage);
|
|
45
|
+
return res.status(status).json({ code, message });
|
|
46
|
+
}
|
package/lib/src/utils/helpers.js
CHANGED
|
@@ -1,251 +1,212 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared helper utilities used across controllers, services, and CLI.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const HTML_ESCAPE_MAP = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
6
|
-
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
'
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// ---------------------------------------------------------------------------
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
export
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
<
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
body { margin: 0; display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #000; color: #fff; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
|
|
214
|
-
.player-container { width: 100%; max-width: 100vw; max-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
|
|
215
|
-
video { width: 100%; height: auto; max-width: 100%; max-height: calc(100vh - 40px); object-fit: contain; background: #000; }
|
|
216
|
-
.file-name { position: fixed; top: 10px; left: 10px; background: rgba(0, 0, 0, 0.8); padding: 8px 16px; border-radius: 4px; font-size: 14px; z-index: 10; }
|
|
217
|
-
</style>
|
|
218
|
-
</head>
|
|
219
|
-
<body>
|
|
220
|
-
<div class="file-name">${safeFileName}</div>
|
|
221
|
-
<div class="player-container">
|
|
222
|
-
<video controls preload="metadata" src="/mbkbucket/view/${encodedKey}${bucketQuery}">
|
|
223
|
-
Your browser does not support the video tag.
|
|
224
|
-
</video>
|
|
225
|
-
</div>
|
|
226
|
-
</body>
|
|
227
|
-
</html>`;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// ---------------------------------------------------------------------------
|
|
231
|
-
// Common Handlebars helpers
|
|
232
|
-
// ---------------------------------------------------------------------------
|
|
233
|
-
|
|
234
|
-
export const commonHandlebarsHelpers = {
|
|
235
|
-
eq: (a, b) => a === b,
|
|
236
|
-
neq: (a, b) => a !== b,
|
|
237
|
-
or: (...args) => args.slice(0, -1).some(Boolean),
|
|
238
|
-
and: (...args) => args.slice(0, -1).every(Boolean),
|
|
239
|
-
not: (val) => !val,
|
|
240
|
-
gt: (a, b) => a > b,
|
|
241
|
-
gte: (a, b) => a >= b,
|
|
242
|
-
lt: (a, b) => a < b,
|
|
243
|
-
lte: (a, b) => a <= b,
|
|
244
|
-
includes: (arr, val) => Array.isArray(arr) && arr.includes(val),
|
|
245
|
-
json: (obj) => JSON.stringify(obj, null, 2),
|
|
246
|
-
jsonStringify: (obj) => JSON.stringify(obj),
|
|
247
|
-
getInitials: (username) => {
|
|
248
|
-
if (!username) return '?';
|
|
249
|
-
return username.split(/[._]/).map(p => p.charAt(0)).join('').toUpperCase().slice(0, 2);
|
|
250
|
-
},
|
|
251
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Shared helper utilities used across controllers, services, and CLI.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const HTML_ESCAPE_MAP = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
6
|
+
|
|
7
|
+
export const escapeHtml = (value = '') => String(value ?? '').replace(/[&<>"']/g, c => HTML_ESCAPE_MAP[c]);
|
|
8
|
+
|
|
9
|
+
export function getBaseName(path = '') {
|
|
10
|
+
const value = String(path ?? '');
|
|
11
|
+
const lastSlash = value.lastIndexOf('/');
|
|
12
|
+
return lastSlash === -1 ? value : value.slice(lastSlash + 1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getFileExt(fileName = '') {
|
|
16
|
+
const name = getBaseName(fileName);
|
|
17
|
+
const dotIndex = name.lastIndexOf('.');
|
|
18
|
+
return dotIndex === -1 ? '' : name.slice(dotIndex + 1).toLowerCase();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const trimSlashes = (str = '') => String(str ?? '').replace(/^\/+|\/+$/g, '');
|
|
22
|
+
export const trimLeadingSlashes = (str = '') => String(str ?? '').replace(/^\/+/, '');
|
|
23
|
+
|
|
24
|
+
export function getFolderPath(key = '') {
|
|
25
|
+
const s = String(key ?? '');
|
|
26
|
+
const lastSlash = s.lastIndexOf('/');
|
|
27
|
+
return lastSlash === -1 ? '' : s.slice(0, lastSlash + 1);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const normalizeKeyParam = rawKey => Array.isArray(rawKey) ? rawKey.join('/') : String(rawKey || '');
|
|
31
|
+
|
|
32
|
+
export function buildKey(prefix, fileName) {
|
|
33
|
+
const cleanPrefix = trimSlashes(prefix);
|
|
34
|
+
const cleanName = trimLeadingSlashes(fileName);
|
|
35
|
+
return cleanPrefix ? `${cleanPrefix}/${cleanName}` : cleanName;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const nowIso = () => new Date().toISOString();
|
|
39
|
+
|
|
40
|
+
export function formatBytes(bytes) {
|
|
41
|
+
if (!bytes) return '0 B';
|
|
42
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
43
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
44
|
+
return `${(bytes / 1024 ** i).toFixed(i > 0 ? 1 : 0)} ${units[i]}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function formatDate(iso) {
|
|
48
|
+
try {
|
|
49
|
+
return new Date(iso).toLocaleString();
|
|
50
|
+
} catch {
|
|
51
|
+
return iso || '—';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function streamToBuffer(stream) {
|
|
56
|
+
if (Buffer.isBuffer(stream)) return stream;
|
|
57
|
+
const chunks = [];
|
|
58
|
+
if (stream?.[Symbol.asyncIterator] || stream?.transformToWeb) {
|
|
59
|
+
for await (const chunk of stream) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
60
|
+
} else if (stream?.getReader) {
|
|
61
|
+
const reader = stream.getReader();
|
|
62
|
+
for (let res = await reader.read(); !res.done; res = await reader.read()) {
|
|
63
|
+
chunks.push(Buffer.isBuffer(res.value) ? res.value : Buffer.from(res.value));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return Buffer.concat(chunks);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function destroyStream(stream) {
|
|
70
|
+
if (typeof stream?.destroy === 'function') {
|
|
71
|
+
stream.removeAllListeners?.();
|
|
72
|
+
stream.destroy();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// MIME type & file extension constants
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
export const VIEWABLE_TYPES = new Set([
|
|
81
|
+
'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico',
|
|
82
|
+
'mp4', 'webm', 'ogg', 'avi', 'mov',
|
|
83
|
+
'mp3', 'wav', 'flac', 'aac', 'm4a',
|
|
84
|
+
'txt', 'md', 'json', 'xml', 'csv', 'log',
|
|
85
|
+
'js', 'ts', 'html', 'htm', 'css', 'php', 'py', 'java', 'cpp', 'c', 'h',
|
|
86
|
+
'cs', 'rb', 'go', 'rs', 'sql', 'sh', 'bat', 'ps1', 'yaml', 'yml',
|
|
87
|
+
'toml', 'ini', 'conf', 'pdf'
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
export const VIEWABLE_TYPES_SORTED = [...VIEWABLE_TYPES].sort();
|
|
91
|
+
export const STATIC_ASSET_TYPES = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico', 'css', 'js']);
|
|
92
|
+
export const VIDEO_TYPES = new Set(['mp4', 'webm', 'ogg', 'avi', 'mov']);
|
|
93
|
+
export const AUDIO_TYPES = new Set(['mp3', 'wav', 'flac', 'aac', 'm4a']);
|
|
94
|
+
|
|
95
|
+
export const MIME_TYPES = {
|
|
96
|
+
jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png', gif: 'image/gif',
|
|
97
|
+
webp: 'image/webp', svg: 'image/svg+xml', bmp: 'image/bmp', ico: 'image/x-icon',
|
|
98
|
+
mp4: 'video/mp4', webm: 'video/webm', ogg: 'video/ogg', avi: 'video/x-msvideo', mov: 'video/quicktime',
|
|
99
|
+
mp3: 'audio/mpeg', wav: 'audio/wav', flac: 'audio/flac', aac: 'audio/aac', m4a: 'audio/mp4',
|
|
100
|
+
txt: 'text/plain; charset=utf-8', md: 'text/markdown; charset=utf-8',
|
|
101
|
+
json: 'application/json; charset=utf-8', xml: 'application/xml; charset=utf-8',
|
|
102
|
+
csv: 'text/csv; charset=utf-8', log: 'text/plain; charset=utf-8',
|
|
103
|
+
js: 'text/javascript; charset=utf-8', ts: 'text/typescript; charset=utf-8',
|
|
104
|
+
html: 'text/html; charset=utf-8', htm: 'text/html; charset=utf-8',
|
|
105
|
+
css: 'text/css; charset=utf-8', php: 'text/x-php; charset=utf-8',
|
|
106
|
+
py: 'text/x-python; charset=utf-8', java: 'text/x-java-source; charset=utf-8',
|
|
107
|
+
cpp: 'text/x-c++src; charset=utf-8', c: 'text/x-csrc; charset=utf-8',
|
|
108
|
+
h: 'text/x-chdr; charset=utf-8', cs: 'text/x-csharp; charset=utf-8',
|
|
109
|
+
rb: 'text/x-ruby; charset=utf-8', go: 'text/x-go; charset=utf-8',
|
|
110
|
+
rs: 'text/x-rust; charset=utf-8', sql: 'text/x-sql; charset=utf-8',
|
|
111
|
+
sh: 'text/x-shellscript; charset=utf-8', bat: 'text/x-msdos-batch; charset=utf-8',
|
|
112
|
+
ps1: 'text/x-powershell; charset=utf-8', yaml: 'text/x-yaml; charset=utf-8',
|
|
113
|
+
yml: 'text/x-yaml; charset=utf-8', toml: 'text/x-toml; charset=utf-8',
|
|
114
|
+
ini: 'text/x-ini; charset=utf-8', conf: 'text/x-conf; charset=utf-8',
|
|
115
|
+
zip: 'application/zip', gz: 'application/gzip', tar: 'application/x-tar', pdf: 'application/pdf'
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const getMimeType = (fileName, fallback = 'application/octet-stream') => MIME_TYPES[getFileExt(fileName)] || fallback;
|
|
119
|
+
|
|
120
|
+
export function isSensitiveTextType(contentType = '') {
|
|
121
|
+
const type = String(contentType ?? '').toLowerCase();
|
|
122
|
+
return type.startsWith('text/') || /json|xml|javascript|typescript/.test(type);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function buildCacheControl(contentType, { isStaticAsset = false, supportsRanges = false, publicCache = false } = {}) {
|
|
126
|
+
if (publicCache) {
|
|
127
|
+
if (isStaticAsset) return 'public, max-age=3600, immutable';
|
|
128
|
+
return supportsRanges ? 'public, max-age=86400, must-revalidate' : 'public, max-age=300, must-revalidate';
|
|
129
|
+
}
|
|
130
|
+
if (isSensitiveTextType(contentType)) return 'private, no-store';
|
|
131
|
+
return supportsRanges ? 'private, max-age=1800, must-revalidate' : 'private, max-age=300, must-revalidate';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
// Range header parsing
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
|
|
138
|
+
const DEFAULT_CHUNK_SIZE = 8 * 1024 * 1024; // 8 MB
|
|
139
|
+
|
|
140
|
+
export function parseRangeHeader(rangeHeader, totalSize) {
|
|
141
|
+
if (!totalSize || totalSize <= 0) return null;
|
|
142
|
+
|
|
143
|
+
const match = /^bytes=(\d*)-(\d*)$/.exec(String(rangeHeader ?? '').trim());
|
|
144
|
+
if (!match) return null;
|
|
145
|
+
|
|
146
|
+
const [, startStr, endStr] = match;
|
|
147
|
+
let start;
|
|
148
|
+
let end;
|
|
149
|
+
|
|
150
|
+
if (startStr === '') {
|
|
151
|
+
const suffix = parseInt(endStr, 10);
|
|
152
|
+
if (isNaN(suffix) || suffix <= 0) return null;
|
|
153
|
+
start = Math.max(0, totalSize - suffix);
|
|
154
|
+
end = totalSize - 1;
|
|
155
|
+
} else {
|
|
156
|
+
start = parseInt(startStr, 10);
|
|
157
|
+
if (isNaN(start) || start < 0) return null;
|
|
158
|
+
end = endStr === '' ? Math.min(start + DEFAULT_CHUNK_SIZE - 1, totalSize - 1) : parseInt(endStr, 10);
|
|
159
|
+
if (isNaN(end) || end < start) return null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (start >= totalSize || end >= totalSize) return null;
|
|
163
|
+
return { start, end, total: totalSize, size: end - start + 1 };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
// Player page HTML template
|
|
168
|
+
// ---------------------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
export const renderPlayerPage = (safeFileName, encodedKey, bucketQuery) => `<!doctype html>
|
|
171
|
+
<html lang="en">
|
|
172
|
+
<head>
|
|
173
|
+
<meta charset="utf-8">
|
|
174
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
175
|
+
<title>Preview: ${safeFileName}</title>
|
|
176
|
+
<style>
|
|
177
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
178
|
+
body { margin: 0; display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #000; color: #fff; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
|
|
179
|
+
.player-container { width: 100%; max-width: 100vw; max-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
|
|
180
|
+
video { width: 100%; height: auto; max-width: 100%; max-height: calc(100vh - 40px); object-fit: contain; background: #000; }
|
|
181
|
+
.file-name { position: fixed; top: 10px; left: 10px; background: rgba(0, 0, 0, 0.8); padding: 8px 16px; border-radius: 4px; font-size: 14px; z-index: 10; }
|
|
182
|
+
</style>
|
|
183
|
+
</head>
|
|
184
|
+
<body>
|
|
185
|
+
<div class="file-name">${safeFileName}</div>
|
|
186
|
+
<div class="player-container">
|
|
187
|
+
<video controls preload="metadata" src="/mbkbucket/view/${encodedKey}${bucketQuery}">
|
|
188
|
+
Your browser does not support the video tag.
|
|
189
|
+
</video>
|
|
190
|
+
</div>
|
|
191
|
+
</body>
|
|
192
|
+
</html>`;
|
|
193
|
+
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
// Common Handlebars helpers
|
|
196
|
+
// ---------------------------------------------------------------------------
|
|
197
|
+
|
|
198
|
+
export const commonHandlebarsHelpers = {
|
|
199
|
+
eq: (a, b) => a === b,
|
|
200
|
+
neq: (a, b) => a !== b,
|
|
201
|
+
or: (...args) => args.slice(0, -1).some(Boolean),
|
|
202
|
+
and: (...args) => args.slice(0, -1).every(Boolean),
|
|
203
|
+
not: val => !val,
|
|
204
|
+
gt: (a, b) => a > b,
|
|
205
|
+
gte: (a, b) => a >= b,
|
|
206
|
+
lt: (a, b) => a < b,
|
|
207
|
+
lte: (a, b) => a <= b,
|
|
208
|
+
includes: (arr, val) => Array.isArray(arr) && arr.includes(val),
|
|
209
|
+
json: obj => JSON.stringify(obj, null, 2),
|
|
210
|
+
jsonStringify: obj => JSON.stringify(obj),
|
|
211
|
+
getInitials: username => username ? username.split(/[._]/).map(p => p.charAt(0)).join('').toUpperCase().slice(0, 2) : '?',
|
|
212
|
+
};
|
package/lib/src/utils/logger.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import createDebug from "debug";
|
|
2
|
-
import dotenv from "dotenv";
|
|
3
|
-
|
|
4
|
-
dotenv.config();
|
|
5
|
-
|
|
6
|
-
if (process.env.DEBUG) {
|
|
7
|
-
createDebug.enable(process.env.DEBUG);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Creates a namespaced debug logger (e.g., `mbkbucket:s3`, `mbkbucket:api`).
|
|
12
|
-
* @param {string} namespace
|
|
13
|
-
* @returns {import("debug").Debugger}
|
|
14
|
-
*/
|
|
15
|
-
export function createLogger(namespace) {
|
|
16
|
-
return createDebug(`mbkbucket:${namespace}`);
|
|
17
|
-
}
|
|
1
|
+
import createDebug from "debug";
|
|
2
|
+
import dotenv from "dotenv";
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
5
|
+
|
|
6
|
+
if (process.env.DEBUG) {
|
|
7
|
+
createDebug.enable(process.env.DEBUG);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a namespaced debug logger (e.g., `mbkbucket:s3`, `mbkbucket:api`).
|
|
12
|
+
* @param {string} namespace
|
|
13
|
+
* @returns {import("debug").Debugger}
|
|
14
|
+
*/
|
|
15
|
+
export function createLogger(namespace) {
|
|
16
|
+
return createDebug(`mbkbucket:${namespace}`);
|
|
17
|
+
}
|