@arela/uploader 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -0
- package/package.json +2 -3
- package/src/index.js +639 -127
- package/upload.log +1323 -158
package/src/index.js
CHANGED
|
@@ -7,7 +7,6 @@ import fs from 'fs';
|
|
|
7
7
|
import { globby } from 'globby';
|
|
8
8
|
import mime from 'mime-types';
|
|
9
9
|
import { createRequire } from 'module';
|
|
10
|
-
import ora from 'ora';
|
|
11
10
|
import path from 'path';
|
|
12
11
|
|
|
13
12
|
const require = createRequire(import.meta.url);
|
|
@@ -27,14 +26,236 @@ const sources = process.env.UPLOAD_SOURCES?.split('|')
|
|
|
27
26
|
|
|
28
27
|
const supabase = createClient(supabaseUrl, supabaseKey);
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
// Pre-compiled regex patterns for better performance
|
|
30
|
+
const SANITIZATION_PATTERNS = [
|
|
31
|
+
// Character replacements (grouped for efficiency)
|
|
32
|
+
[/[áàâäãåāăą]/gi, 'a'],
|
|
33
|
+
[/[éèêëēĕėę]/gi, 'e'],
|
|
34
|
+
[/[íìîïīĭį]/gi, 'i'],
|
|
35
|
+
[/[óòôöõōŏő]/gi, 'o'],
|
|
36
|
+
[/[úùûüūŭů]/gi, 'u'],
|
|
37
|
+
[/[ñň]/gi, 'n'],
|
|
38
|
+
[/[ç]/gi, 'c'],
|
|
39
|
+
[/[ý]/gi, 'y'],
|
|
40
|
+
// Korean characters (compiled once)
|
|
41
|
+
[/[멕]/g, 'meok'],
|
|
42
|
+
[/[시]/g, 'si'],
|
|
43
|
+
[/[코]/g, 'ko'],
|
|
44
|
+
[/[용]/g, 'yong'],
|
|
45
|
+
[/[가-힣]/g, 'kr'],
|
|
46
|
+
// Unicode diacritics (after normalize)
|
|
47
|
+
[/[\u0300-\u036f]/g, ''],
|
|
48
|
+
// Problematic symbols
|
|
49
|
+
[/[\\?%*:|"<>[\]~`^]/g, '-'],
|
|
50
|
+
[/[{}]/g, '-'],
|
|
51
|
+
[/[&]/g, 'and'],
|
|
52
|
+
[/[()]/g, ''], // Remove parentheses
|
|
53
|
+
// Cleanup patterns
|
|
54
|
+
[/\s+/g, '-'], // Replace spaces with dashes
|
|
55
|
+
[/-+/g, '-'], // Replace multiple dashes with single dash
|
|
56
|
+
[/^-+|-+$/g, ''], // Remove leading/trailing dashes
|
|
57
|
+
[/^\.+/, ''], // Remove leading dots
|
|
58
|
+
[/[^\w.-]/g, ''], // Remove any remaining non-alphanumeric chars
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
// Cache for sanitized filenames to avoid repeated processing
|
|
62
|
+
const sanitizationCache = new Map();
|
|
63
|
+
|
|
64
|
+
// Enhanced sanitization function with caching and pre-compiled regex
|
|
65
|
+
const sanitizeFileName = (fileName) => {
|
|
66
|
+
// Check cache first
|
|
67
|
+
if (sanitizationCache.has(fileName)) {
|
|
68
|
+
return sanitizationCache.get(fileName);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Get file extension
|
|
72
|
+
const ext = path.extname(fileName);
|
|
73
|
+
const nameWithoutExt = path.basename(fileName, ext);
|
|
74
|
+
|
|
75
|
+
// Fast path for already clean filenames
|
|
76
|
+
if (/^[a-zA-Z0-9._-]+$/.test(nameWithoutExt)) {
|
|
77
|
+
const result = fileName;
|
|
78
|
+
sanitizationCache.set(fileName, result);
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Normalize unicode first (more efficient to do once)
|
|
83
|
+
let sanitized = nameWithoutExt.normalize('NFD');
|
|
84
|
+
|
|
85
|
+
// Apply all sanitization patterns
|
|
86
|
+
for (const [pattern, replacement] of SANITIZATION_PATTERNS) {
|
|
87
|
+
sanitized = sanitized.replace(pattern, replacement);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Ensure the filename is not empty
|
|
91
|
+
if (!sanitized) {
|
|
92
|
+
sanitized = 'unnamed_file';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const result = sanitized + ext;
|
|
96
|
+
|
|
97
|
+
// Cache the result for future use
|
|
98
|
+
sanitizationCache.set(fileName, result);
|
|
99
|
+
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// Pre-compiled regex patterns for path sanitization
|
|
104
|
+
const PATH_SANITIZATION_PATTERNS = [
|
|
105
|
+
[/[\\?%*:|"<>[\]~]/g, '-'],
|
|
106
|
+
[/ +/g, ' '],
|
|
107
|
+
[/^\.+/, ''],
|
|
108
|
+
[/\/+/g, '/'],
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
// Cache for sanitized paths
|
|
112
|
+
const pathSanitizationCache = new Map();
|
|
113
|
+
|
|
114
|
+
// Batch logging system for performance
|
|
115
|
+
class LogBatcher {
|
|
116
|
+
constructor(batchSize = 50, flushInterval = 5000) {
|
|
117
|
+
this.batch = [];
|
|
118
|
+
this.batchSize = batchSize;
|
|
119
|
+
this.flushInterval = flushInterval;
|
|
120
|
+
this.lastFlush = Date.now();
|
|
121
|
+
this.flushTimer = null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
add(logEntry) {
|
|
125
|
+
this.batch.push({
|
|
126
|
+
filename: path.basename(logEntry.file),
|
|
127
|
+
path: logEntry.uploadPath,
|
|
128
|
+
status: logEntry.status,
|
|
129
|
+
message: logEntry.message,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Auto-flush if batch is full or enough time has passed
|
|
133
|
+
if (
|
|
134
|
+
this.batch.length >= this.batchSize ||
|
|
135
|
+
Date.now() - this.lastFlush > this.flushInterval
|
|
136
|
+
) {
|
|
137
|
+
this.flush();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async flush() {
|
|
142
|
+
if (this.batch.length === 0) return;
|
|
143
|
+
|
|
144
|
+
const logsToSend = [...this.batch];
|
|
145
|
+
this.batch = [];
|
|
146
|
+
this.lastFlush = Date.now();
|
|
147
|
+
|
|
148
|
+
// Clear any pending timer
|
|
149
|
+
if (this.flushTimer) {
|
|
150
|
+
clearTimeout(this.flushTimer);
|
|
151
|
+
this.flushTimer = null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
const { error } = await supabase.from('upload_logs').insert(logsToSend);
|
|
156
|
+
if (error) {
|
|
157
|
+
console.error(
|
|
158
|
+
`⚠️ Error saving batch of ${logsToSend.length} logs to Supabase: ${error.message}`,
|
|
159
|
+
);
|
|
160
|
+
// Re-add failed logs to batch for retry (optional)
|
|
161
|
+
this.batch.unshift(...logsToSend);
|
|
162
|
+
} else {
|
|
163
|
+
// Only show verbose output if requested
|
|
164
|
+
if (process.env.LOG_BATCH_VERBOSE === 'true') {
|
|
165
|
+
console.log(`📊 Flushed ${logsToSend.length} logs to Supabase`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
} catch (err) {
|
|
169
|
+
console.error(`⚠️ Error during batch flush: ${err.message}`);
|
|
170
|
+
// Re-add failed logs to batch for retry (optional)
|
|
171
|
+
this.batch.unshift(...logsToSend);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Schedule auto-flush if not already scheduled
|
|
176
|
+
scheduleFlush() {
|
|
177
|
+
if (!this.flushTimer && this.batch.length > 0) {
|
|
178
|
+
this.flushTimer = setTimeout(() => {
|
|
179
|
+
this.flush();
|
|
180
|
+
}, this.flushInterval);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Force flush all pending logs (called at end of process)
|
|
185
|
+
async forceFlush() {
|
|
186
|
+
if (this.flushTimer) {
|
|
187
|
+
clearTimeout(this.flushTimer);
|
|
188
|
+
this.flushTimer = null;
|
|
189
|
+
}
|
|
190
|
+
await this.flush();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Global log batcher instance
|
|
195
|
+
const logBatcher = new LogBatcher();
|
|
196
|
+
|
|
197
|
+
// Function to manage cache size (prevent memory issues in long sessions)
|
|
198
|
+
const manageCaches = () => {
|
|
199
|
+
const MAX_CACHE_SIZE = 1000;
|
|
200
|
+
|
|
201
|
+
if (sanitizationCache.size > MAX_CACHE_SIZE) {
|
|
202
|
+
// Keep only the most recent 500 entries
|
|
203
|
+
const entries = Array.from(sanitizationCache.entries());
|
|
204
|
+
sanitizationCache.clear();
|
|
205
|
+
entries.slice(-500).forEach(([key, value]) => {
|
|
206
|
+
sanitizationCache.set(key, value);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (pathSanitizationCache.size > MAX_CACHE_SIZE) {
|
|
211
|
+
const entries = Array.from(pathSanitizationCache.entries());
|
|
212
|
+
pathSanitizationCache.clear();
|
|
213
|
+
entries.slice(-500).forEach(([key, value]) => {
|
|
214
|
+
pathSanitizationCache.set(key, value);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const sanitizePath = (inputPath) => {
|
|
220
|
+
// Check cache first
|
|
221
|
+
if (pathSanitizationCache.has(inputPath)) {
|
|
222
|
+
return pathSanitizationCache.get(inputPath);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Fast path for already clean paths
|
|
226
|
+
if (!/[\\?%*:|"<>[\]~]|^ +|^\.+|\/\/+/.test(inputPath)) {
|
|
227
|
+
pathSanitizationCache.set(inputPath, inputPath);
|
|
228
|
+
return inputPath;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let sanitized = inputPath;
|
|
232
|
+
|
|
233
|
+
// Apply path sanitization patterns
|
|
234
|
+
for (const [pattern, replacement] of PATH_SANITIZATION_PATTERNS) {
|
|
235
|
+
sanitized = sanitized.replace(pattern, replacement);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Cache the result
|
|
239
|
+
pathSanitizationCache.set(inputPath, sanitized);
|
|
240
|
+
|
|
241
|
+
return sanitized;
|
|
242
|
+
};
|
|
36
243
|
|
|
37
244
|
const sendLogToSupabase = async ({ file, uploadPath, status, message }) => {
|
|
245
|
+
// Add to batch instead of sending immediately
|
|
246
|
+
logBatcher.add({ file, uploadPath, status, message });
|
|
247
|
+
|
|
248
|
+
// Schedule auto-flush if needed
|
|
249
|
+
logBatcher.scheduleFlush();
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// Enhanced version for immediate sending (used for critical errors)
|
|
253
|
+
const sendLogToSupabaseImmediate = async ({
|
|
254
|
+
file,
|
|
255
|
+
uploadPath,
|
|
256
|
+
status,
|
|
257
|
+
message,
|
|
258
|
+
}) => {
|
|
38
259
|
const { error } = await supabase.from('upload_logs').insert([
|
|
39
260
|
{
|
|
40
261
|
filename: path.basename(file),
|
|
@@ -45,7 +266,9 @@ const sendLogToSupabase = async ({ file, uploadPath, status, message }) => {
|
|
|
45
266
|
]);
|
|
46
267
|
|
|
47
268
|
if (error) {
|
|
48
|
-
console.error(
|
|
269
|
+
console.error(
|
|
270
|
+
`⚠️ Error saving immediate log to Supabase: ${error.message}`,
|
|
271
|
+
);
|
|
49
272
|
}
|
|
50
273
|
};
|
|
51
274
|
|
|
@@ -55,7 +278,7 @@ const checkCredentials = async () => {
|
|
|
55
278
|
'⚠️ Missing Supabase credentials. Please set SUPABASE_URL, SUPABASE_KEY, and SUPABASE_BUCKET in your environment variables.',
|
|
56
279
|
);
|
|
57
280
|
writeLog('⚠️ Missing Supabase credentials.');
|
|
58
|
-
await
|
|
281
|
+
await sendLogToSupabaseImmediate({
|
|
59
282
|
file: 'Error',
|
|
60
283
|
uploadPath: 'Error',
|
|
61
284
|
status: 'error',
|
|
@@ -69,7 +292,7 @@ const checkCredentials = async () => {
|
|
|
69
292
|
if (error) {
|
|
70
293
|
console.error('⚠️ Error connecting to Supabase:', error.message);
|
|
71
294
|
writeLog(`⚠️ Error connecting to Supabase: ${error.message}`);
|
|
72
|
-
await
|
|
295
|
+
await sendLogToSupabaseImmediate({
|
|
73
296
|
file: 'Error',
|
|
74
297
|
uploadPath: 'Error',
|
|
75
298
|
status: 'error',
|
|
@@ -80,7 +303,7 @@ const checkCredentials = async () => {
|
|
|
80
303
|
} catch (err) {
|
|
81
304
|
console.error('⚠️ Error:', err.message);
|
|
82
305
|
writeLog(`⚠️ Error: ${err.message}`);
|
|
83
|
-
await
|
|
306
|
+
await sendLogToSupabaseImmediate({
|
|
84
307
|
file: 'Error',
|
|
85
308
|
uploadPath: 'Error',
|
|
86
309
|
status: 'error',
|
|
@@ -101,7 +324,7 @@ const fileExistsInBucket = async (pathInBucket) => {
|
|
|
101
324
|
if (error) {
|
|
102
325
|
console.error(`⚠️ Could not verify duplicate: ${error.message}`);
|
|
103
326
|
writeLog(`⚠️ Could not verify duplicate: ${error.message}`);
|
|
104
|
-
await
|
|
327
|
+
await sendLogToSupabaseImmediate({
|
|
105
328
|
file: 'Error',
|
|
106
329
|
uploadPath: 'Error',
|
|
107
330
|
status: 'error',
|
|
@@ -189,23 +412,355 @@ const getProcessedPaths = async () => {
|
|
|
189
412
|
|
|
190
413
|
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
191
414
|
|
|
192
|
-
const uploadWithRetry = async (uploadFn, maxRetries =
|
|
415
|
+
const uploadWithRetry = async (uploadFn, maxRetries = 5, delayMs = 2000) => {
|
|
193
416
|
let attempt = 0;
|
|
417
|
+
let lastError;
|
|
418
|
+
|
|
194
419
|
while (attempt < maxRetries) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
420
|
+
try {
|
|
421
|
+
const result = await uploadFn();
|
|
422
|
+
if (!result.error) return result;
|
|
423
|
+
lastError = result.error;
|
|
424
|
+
attempt++;
|
|
425
|
+
|
|
426
|
+
// Exponential backoff with jitter
|
|
427
|
+
if (attempt < maxRetries) {
|
|
428
|
+
const backoffDelay =
|
|
429
|
+
delayMs * Math.pow(2, attempt - 1) + Math.random() * 1000;
|
|
430
|
+
console.log(
|
|
431
|
+
`Retry ${attempt}/${maxRetries} after ${Math.round(backoffDelay)}ms...`,
|
|
432
|
+
);
|
|
433
|
+
await delay(backoffDelay);
|
|
434
|
+
}
|
|
435
|
+
} catch (error) {
|
|
436
|
+
lastError = error;
|
|
437
|
+
attempt++;
|
|
438
|
+
|
|
439
|
+
if (attempt < maxRetries) {
|
|
440
|
+
const backoffDelay =
|
|
441
|
+
delayMs * Math.pow(2, attempt - 1) + Math.random() * 1000;
|
|
442
|
+
console.log(
|
|
443
|
+
`Retry ${attempt}/${maxRetries} after ${Math.round(backoffDelay)}ms due to exception...`,
|
|
444
|
+
);
|
|
445
|
+
await delay(backoffDelay);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
199
448
|
}
|
|
200
|
-
|
|
449
|
+
|
|
450
|
+
return {
|
|
451
|
+
error: new Error(
|
|
452
|
+
`Max retries exceeded. Last error: ${lastError?.message || 'Unknown error'}`,
|
|
453
|
+
),
|
|
454
|
+
};
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// Function to process a single file
|
|
458
|
+
const processFile = async (
|
|
459
|
+
file,
|
|
460
|
+
options,
|
|
461
|
+
basePath,
|
|
462
|
+
folder,
|
|
463
|
+
sourcePath,
|
|
464
|
+
processedPaths,
|
|
465
|
+
) => {
|
|
466
|
+
let currentFile = file;
|
|
467
|
+
let result = {
|
|
468
|
+
success: false,
|
|
469
|
+
skipped: false,
|
|
470
|
+
error: null,
|
|
471
|
+
message: '',
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
try {
|
|
475
|
+
// Check if we need to rename the file
|
|
476
|
+
if (options.renameFiles) {
|
|
477
|
+
const originalName = path.basename(file);
|
|
478
|
+
const sanitizedName = sanitizeFileName(originalName);
|
|
479
|
+
|
|
480
|
+
if (originalName !== sanitizedName) {
|
|
481
|
+
const newFilePath = path.join(path.dirname(file), sanitizedName);
|
|
482
|
+
|
|
483
|
+
if (options.dryRun) {
|
|
484
|
+
result.message = `Would rename: ${originalName} → ${sanitizedName}`;
|
|
485
|
+
result.skipped = true;
|
|
486
|
+
return result;
|
|
487
|
+
} else {
|
|
488
|
+
try {
|
|
489
|
+
fs.renameSync(file, newFilePath);
|
|
490
|
+
currentFile = newFilePath;
|
|
491
|
+
writeLog(`RENAMED: ${originalName} → ${sanitizedName}`);
|
|
492
|
+
await sendLogToSupabase({
|
|
493
|
+
file: originalName,
|
|
494
|
+
uploadPath: sanitizedName,
|
|
495
|
+
status: 'renamed',
|
|
496
|
+
message: `Renamed from ${originalName}`,
|
|
497
|
+
});
|
|
498
|
+
} catch (renameError) {
|
|
499
|
+
result.error = `Failed to rename ${originalName}: ${renameError.message}`;
|
|
500
|
+
writeLog(`RENAME_ERROR: ${originalName} | ${renameError.message}`);
|
|
501
|
+
return result;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const content = fs.readFileSync(currentFile);
|
|
508
|
+
const relativePathRaw = path
|
|
509
|
+
.relative(basePath, currentFile)
|
|
510
|
+
.replace(/^[\\/]+/, '')
|
|
511
|
+
.replace(/\\/g, '/');
|
|
512
|
+
|
|
513
|
+
// Always sanitize the filename for upload path
|
|
514
|
+
const pathParts = relativePathRaw.split('/');
|
|
515
|
+
const originalFileName = pathParts[pathParts.length - 1];
|
|
516
|
+
const sanitizedFileName = sanitizeFileName(originalFileName);
|
|
517
|
+
pathParts[pathParts.length - 1] = sanitizedFileName;
|
|
518
|
+
const sanitizedRelativePath = pathParts.join('/');
|
|
519
|
+
|
|
520
|
+
const uploadPathRaw = options.prefix
|
|
521
|
+
? path.posix.join(options.prefix, sanitizedRelativePath)
|
|
522
|
+
: sanitizedRelativePath;
|
|
523
|
+
const uploadPath = sanitizePath(uploadPathRaw);
|
|
524
|
+
|
|
525
|
+
if (
|
|
526
|
+
uploadPath !== uploadPathRaw ||
|
|
527
|
+
originalFileName !== sanitizedFileName
|
|
528
|
+
) {
|
|
529
|
+
writeLog(`SANITIZED: ${relativePathRaw} → ${uploadPath}`);
|
|
530
|
+
await sendLogToSupabase({
|
|
531
|
+
file: currentFile,
|
|
532
|
+
uploadPath: relativePathRaw,
|
|
533
|
+
status: 'sanitized',
|
|
534
|
+
message: `Sanitized to ${uploadPath} (Arela Version: ${version})`,
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
if (processedPaths.has(uploadPath)) {
|
|
539
|
+
result.skipped = true;
|
|
540
|
+
result.message = `Already processed (log): ${currentFile}`;
|
|
541
|
+
return result;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const contentType = mime.lookup(currentFile) || 'application/octet-stream';
|
|
545
|
+
|
|
546
|
+
const exists = await fileExistsInBucket(uploadPath);
|
|
547
|
+
|
|
548
|
+
if (exists) {
|
|
549
|
+
result.skipped = true;
|
|
550
|
+
result.message = `Skipped (already exists): ${currentFile}`;
|
|
551
|
+
writeLog(`SKIPPED: ${currentFile} -> ${uploadPath}`);
|
|
552
|
+
await sendLogToSupabase({
|
|
553
|
+
file: currentFile,
|
|
554
|
+
uploadPath,
|
|
555
|
+
status: 'skipped',
|
|
556
|
+
message: 'Already exists in bucket',
|
|
557
|
+
});
|
|
558
|
+
return result;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
const { error } = await uploadWithRetry(() =>
|
|
562
|
+
supabase.storage.from(bucket).upload(uploadPath, content, {
|
|
563
|
+
upsert: true,
|
|
564
|
+
contentType,
|
|
565
|
+
metadata: {
|
|
566
|
+
originalName: path.basename(currentFile),
|
|
567
|
+
sanitizedName: path.basename(uploadPath),
|
|
568
|
+
clientPath: path.posix.join(
|
|
569
|
+
basePath,
|
|
570
|
+
folder,
|
|
571
|
+
path.relative(sourcePath, currentFile).replace(/\\/g, '/'),
|
|
572
|
+
),
|
|
573
|
+
arelaVersion: version,
|
|
574
|
+
},
|
|
575
|
+
}),
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
if (error) {
|
|
579
|
+
result.error = error.message || JSON.stringify(error);
|
|
580
|
+
writeLog(`ERROR: ${currentFile} -> ${uploadPath} | ${result.error}`);
|
|
581
|
+
await sendLogToSupabase({
|
|
582
|
+
file: currentFile,
|
|
583
|
+
uploadPath,
|
|
584
|
+
status: 'error',
|
|
585
|
+
message: result.error,
|
|
586
|
+
});
|
|
587
|
+
} else {
|
|
588
|
+
result.success = true;
|
|
589
|
+
result.message = `Uploaded ${currentFile} -> ${uploadPath}`;
|
|
590
|
+
writeLog(`SUCCESS: ${currentFile} -> ${uploadPath}`);
|
|
591
|
+
await sendLogToSupabase({
|
|
592
|
+
file: currentFile,
|
|
593
|
+
uploadPath,
|
|
594
|
+
status: 'success',
|
|
595
|
+
message: 'Uploaded successfully',
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
} catch (err) {
|
|
599
|
+
result.error = err.message || JSON.stringify(err);
|
|
600
|
+
writeLog(`ERROR: ${currentFile} | ${result.error}`);
|
|
601
|
+
await sendLogToSupabase({
|
|
602
|
+
file: currentFile,
|
|
603
|
+
uploadPath: currentFile,
|
|
604
|
+
status: 'error',
|
|
605
|
+
message: result.error,
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
return result;
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
// Function to process files in parallel batches
|
|
613
|
+
const processFilesInBatches = async (
|
|
614
|
+
files,
|
|
615
|
+
batchSize,
|
|
616
|
+
options,
|
|
617
|
+
basePath,
|
|
618
|
+
folder,
|
|
619
|
+
sourcePath,
|
|
620
|
+
processedPaths,
|
|
621
|
+
) => {
|
|
622
|
+
let successCount = 0;
|
|
623
|
+
let failureCount = 0;
|
|
624
|
+
let skippedCount = 0;
|
|
625
|
+
|
|
626
|
+
// Buffer for messages to show after progress bar completes
|
|
627
|
+
const messageBuffer = [];
|
|
628
|
+
|
|
629
|
+
const progressBar = new cliProgress.SingleBar({
|
|
630
|
+
format:
|
|
631
|
+
'📂 Processing [{bar}] {percentage}% | {value}/{total} files | Success: {successCount} | Errors: {failureCount} | Skipped: {skippedCount}',
|
|
632
|
+
barCompleteChar: '█',
|
|
633
|
+
barIncompleteChar: '░',
|
|
634
|
+
hideCursor: true,
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
progressBar.start(files.length, 0, {
|
|
638
|
+
successCount: 0,
|
|
639
|
+
failureCount: 0,
|
|
640
|
+
skippedCount: 0,
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
for (let i = 0; i < files.length; i += batchSize) {
|
|
644
|
+
const batch = files.slice(i, i + batchSize);
|
|
645
|
+
|
|
646
|
+
// Process batch in parallel
|
|
647
|
+
const batchResults = await Promise.all(
|
|
648
|
+
batch.map((file) =>
|
|
649
|
+
processFile(
|
|
650
|
+
file,
|
|
651
|
+
options,
|
|
652
|
+
basePath,
|
|
653
|
+
folder,
|
|
654
|
+
sourcePath,
|
|
655
|
+
processedPaths,
|
|
656
|
+
),
|
|
657
|
+
),
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
// Update counters and buffer messages (don't print them yet)
|
|
661
|
+
for (const result of batchResults) {
|
|
662
|
+
if (result.success) {
|
|
663
|
+
successCount++;
|
|
664
|
+
// Only buffer verbose success messages if needed
|
|
665
|
+
if (
|
|
666
|
+
process.env.UPLOAD_VERBOSE === 'true' &&
|
|
667
|
+
result.message &&
|
|
668
|
+
!result.error
|
|
669
|
+
) {
|
|
670
|
+
messageBuffer.push(`✅ ${result.message}`);
|
|
671
|
+
}
|
|
672
|
+
} else if (result.skipped) {
|
|
673
|
+
skippedCount++;
|
|
674
|
+
// Only buffer verbose skip messages if needed
|
|
675
|
+
if (process.env.UPLOAD_VERBOSE === 'true' && result.message) {
|
|
676
|
+
messageBuffer.push(`⏭️ ${result.message}`);
|
|
677
|
+
}
|
|
678
|
+
} else if (result.error) {
|
|
679
|
+
failureCount++;
|
|
680
|
+
// Always buffer error messages to show later
|
|
681
|
+
messageBuffer.push(`❌ ${result.error}`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
progressBar.update(i + batch.length, {
|
|
686
|
+
successCount,
|
|
687
|
+
failureCount,
|
|
688
|
+
skippedCount,
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
// Manage cache size periodically (every 100 files processed)
|
|
692
|
+
if ((i + batch.length) % 100 === 0) {
|
|
693
|
+
manageCaches();
|
|
694
|
+
// Also flush logs every 100 files to maintain responsiveness
|
|
695
|
+
await logBatcher.flush();
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// Small delay between batches to prevent overwhelming the server
|
|
699
|
+
if (i + batchSize < files.length) {
|
|
700
|
+
await delay(100);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// Stop progress bar cleanly before showing any messages
|
|
705
|
+
progressBar.stop();
|
|
706
|
+
|
|
707
|
+
// Now show buffered messages if there are any important ones to show
|
|
708
|
+
const errorMessages = messageBuffer.filter((msg) => msg.startsWith('❌'));
|
|
709
|
+
if (errorMessages.length > 0) {
|
|
710
|
+
console.log('\n🚨 Errors encountered during processing:');
|
|
711
|
+
errorMessages.forEach((msg) => console.error(msg));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Show verbose messages only if requested
|
|
715
|
+
if (process.env.UPLOAD_VERBOSE === 'true') {
|
|
716
|
+
const otherMessages = messageBuffer.filter((msg) => !msg.startsWith('❌'));
|
|
717
|
+
if (otherMessages.length > 0) {
|
|
718
|
+
console.log('\n📝 Detailed processing log:');
|
|
719
|
+
otherMessages.forEach((msg) => console.log(msg));
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
return {
|
|
724
|
+
successCount,
|
|
725
|
+
failureCount,
|
|
726
|
+
skippedCount,
|
|
727
|
+
};
|
|
201
728
|
};
|
|
202
729
|
|
|
203
730
|
program
|
|
204
731
|
.name('supabase-uploader')
|
|
205
732
|
.description('CLI to upload folders from a base path to Supabase Storage')
|
|
206
|
-
.
|
|
733
|
+
.option('-v, --version', 'output the version number')
|
|
207
734
|
.option('-p, --prefix <prefix>', 'Prefix path in bucket', '')
|
|
735
|
+
.option(
|
|
736
|
+
'-r, --rename-files',
|
|
737
|
+
'Rename files with problematic characters before uploading',
|
|
738
|
+
)
|
|
739
|
+
.option(
|
|
740
|
+
'--dry-run',
|
|
741
|
+
'Show what files would be renamed without actually renaming them',
|
|
742
|
+
)
|
|
743
|
+
.option(
|
|
744
|
+
'-c, --concurrency <number>',
|
|
745
|
+
'Number of files to process concurrently (default: 3)',
|
|
746
|
+
'3',
|
|
747
|
+
)
|
|
748
|
+
.option(
|
|
749
|
+
'--show-cache-stats',
|
|
750
|
+
'Show cache statistics for performance analysis',
|
|
751
|
+
)
|
|
752
|
+
.option(
|
|
753
|
+
'--batch-size <number>',
|
|
754
|
+
'Number of logs to batch before sending to Supabase (default: 50)',
|
|
755
|
+
'50',
|
|
756
|
+
)
|
|
208
757
|
.action(async (options) => {
|
|
758
|
+
// Handle version option
|
|
759
|
+
if (options.version) {
|
|
760
|
+
console.log(version);
|
|
761
|
+
process.exit(0);
|
|
762
|
+
}
|
|
763
|
+
|
|
209
764
|
if (!basePath || !sources || sources.length === 0) {
|
|
210
765
|
console.error(
|
|
211
766
|
'⚠️ UPLOAD_BASE_PATH or UPLOAD_SOURCES not defined in environment variables.',
|
|
@@ -213,6 +768,15 @@ program
|
|
|
213
768
|
process.exit(1);
|
|
214
769
|
}
|
|
215
770
|
|
|
771
|
+
const concurrency = parseInt(options.concurrency) || 3;
|
|
772
|
+
const batchSize = parseInt(options.batchSize) || 50;
|
|
773
|
+
|
|
774
|
+
// Configure log batcher with custom batch size
|
|
775
|
+
logBatcher.batchSize = batchSize;
|
|
776
|
+
|
|
777
|
+
console.log(`🚀 Using concurrency level: ${concurrency}`);
|
|
778
|
+
console.log(`📦 Using log batch size: ${batchSize}`);
|
|
779
|
+
|
|
216
780
|
const processedPaths = await getProcessedPaths();
|
|
217
781
|
let globalSuccess = 0;
|
|
218
782
|
let globalFailure = 0;
|
|
@@ -227,122 +791,61 @@ program
|
|
|
227
791
|
? await globby([`${sourcePath}/**/*`], { onlyFiles: true })
|
|
228
792
|
: [sourcePath];
|
|
229
793
|
|
|
230
|
-
|
|
231
|
-
format: '📂 Reading [{bar}] {percentage}% | {value}/{total} files',
|
|
232
|
-
barCompleteChar: '█',
|
|
233
|
-
barIncompleteChar: '░',
|
|
234
|
-
hideCursor: true,
|
|
235
|
-
});
|
|
236
|
-
progressBar.start(files.length, 0);
|
|
237
|
-
|
|
238
|
-
let successCount = 0;
|
|
239
|
-
let failureCount = 0;
|
|
240
|
-
|
|
241
|
-
for (const file of files) {
|
|
242
|
-
progressBar.increment();
|
|
243
|
-
const content = fs.readFileSync(file);
|
|
244
|
-
const relativePathRaw = path
|
|
245
|
-
.relative(basePath, file)
|
|
246
|
-
.replace(/^[\\/]+/, '')
|
|
247
|
-
.replace(/\\/g, '/');
|
|
248
|
-
const uploadPathRaw = options.prefix
|
|
249
|
-
? path.posix.join(options.prefix, relativePathRaw)
|
|
250
|
-
: relativePathRaw;
|
|
251
|
-
const uploadPath = sanitizePath(uploadPathRaw);
|
|
252
|
-
|
|
253
|
-
if (uploadPath !== uploadPathRaw) {
|
|
254
|
-
writeLog(`SANITIZED: ${uploadPathRaw} → ${uploadPath}`);
|
|
255
|
-
await sendLogToSupabase({
|
|
256
|
-
file,
|
|
257
|
-
uploadPath: uploadPathRaw,
|
|
258
|
-
status: 'sanitized',
|
|
259
|
-
message: `Sanitized to ${uploadPath}`,
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (processedPaths.has(uploadPath)) {
|
|
264
|
-
ora().info(`⏭️ Already processed (log): ${file}`);
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const contentType = mime.lookup(file) || 'application/octet-stream';
|
|
794
|
+
console.log(`📊 Found ${files.length} files to process`);
|
|
269
795
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
});
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
796
|
+
// Process files in parallel batches
|
|
797
|
+
const { successCount, failureCount, skippedCount } =
|
|
798
|
+
await processFilesInBatches(
|
|
799
|
+
files,
|
|
800
|
+
concurrency,
|
|
801
|
+
options,
|
|
802
|
+
basePath,
|
|
803
|
+
folder,
|
|
804
|
+
sourcePath,
|
|
805
|
+
processedPaths,
|
|
806
|
+
);
|
|
284
807
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
const { error } = await uploadWithRetry(() =>
|
|
289
|
-
supabase.storage.from(bucket).upload(uploadPath, content, {
|
|
290
|
-
upsert: true,
|
|
291
|
-
contentType,
|
|
292
|
-
metadata: {
|
|
293
|
-
originalName: path.basename(file),
|
|
294
|
-
clientPath: path.posix.join(
|
|
295
|
-
basePath,
|
|
296
|
-
folder,
|
|
297
|
-
path.relative(sourcePath, file).replace(/\\/g, '/'),
|
|
298
|
-
),
|
|
299
|
-
},
|
|
300
|
-
}),
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
if (error) {
|
|
304
|
-
failureCount++;
|
|
305
|
-
globalFailure++;
|
|
306
|
-
spinner.fail(
|
|
307
|
-
`❌ Failed to upload ${file}: ${JSON.stringify(error, null, 2)}`,
|
|
308
|
-
);
|
|
309
|
-
writeLog(`ERROR: ${file} -> ${uploadPath} | ${error.message}`);
|
|
310
|
-
await sendLogToSupabase({
|
|
311
|
-
file,
|
|
312
|
-
uploadPath,
|
|
313
|
-
status: 'error',
|
|
314
|
-
message: error.message,
|
|
315
|
-
});
|
|
316
|
-
} else {
|
|
317
|
-
successCount++;
|
|
318
|
-
globalSuccess++;
|
|
319
|
-
spinner.succeed(`✅ Uploaded ${file} -> ${uploadPath}`);
|
|
320
|
-
writeLog(`SUCCESS: ${file} -> ${uploadPath}`);
|
|
321
|
-
await sendLogToSupabase({
|
|
322
|
-
file,
|
|
323
|
-
uploadPath,
|
|
324
|
-
status: 'success',
|
|
325
|
-
message: 'Uploaded successfully',
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
} catch (err) {
|
|
329
|
-
spinner.fail(`❌ Error uploading ${file}: ${err.message}`);
|
|
330
|
-
writeLog(`❌ Error uploading ${file}: ${err.message}`);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
808
|
+
globalSuccess += successCount;
|
|
809
|
+
globalFailure += failureCount;
|
|
333
810
|
|
|
334
|
-
|
|
811
|
+
// Small delay to ensure progress bar is fully cleared
|
|
812
|
+
await delay(100);
|
|
335
813
|
|
|
336
|
-
console.log(`\n📦 Upload Summary:`);
|
|
814
|
+
console.log(`\n📦 Upload Summary for ${folder}:`);
|
|
337
815
|
console.log(` ✅ Successfully uploaded files: ${successCount}`);
|
|
338
816
|
console.log(` ❌ Files with errors: ${failureCount}`);
|
|
339
|
-
console.log(
|
|
340
|
-
` ⏭️ Files skipped (already exist): ${files.length - successCount - failureCount}`,
|
|
341
|
-
);
|
|
817
|
+
console.log(` ⏭️ Files skipped (already exist): ${skippedCount}`);
|
|
342
818
|
console.log(` 📜 Log file: ${logFilePath} \n`);
|
|
343
819
|
|
|
820
|
+
// Show cache statistics if requested
|
|
821
|
+
if (options.showCacheStats) {
|
|
822
|
+
console.log(`📊 Cache Statistics:`);
|
|
823
|
+
console.log(
|
|
824
|
+
` 🗂️ Filename sanitization cache: ${sanitizationCache.size} entries`,
|
|
825
|
+
);
|
|
826
|
+
console.log(
|
|
827
|
+
` 📁 Path sanitization cache: ${pathSanitizationCache.size} entries`,
|
|
828
|
+
);
|
|
829
|
+
console.log(
|
|
830
|
+
` 📋 Log batch pending: ${logBatcher.batch.length} entries`,
|
|
831
|
+
);
|
|
832
|
+
|
|
833
|
+
// Calculate cache hit rate (rough estimation)
|
|
834
|
+
const totalProcessed = successCount + failureCount + skippedCount;
|
|
835
|
+
const estimatedCacheHitRate =
|
|
836
|
+
totalProcessed > 0
|
|
837
|
+
? Math.round(
|
|
838
|
+
((totalProcessed - sanitizationCache.size) / totalProcessed) *
|
|
839
|
+
100,
|
|
840
|
+
)
|
|
841
|
+
: 0;
|
|
842
|
+
console.log(
|
|
843
|
+
` 🎯 Estimated cache hit rate: ${Math.max(0, estimatedCacheHitRate)}%\n`,
|
|
844
|
+
);
|
|
845
|
+
}
|
|
846
|
+
|
|
344
847
|
writeLog(
|
|
345
|
-
`📦 Upload Summary for folder ${folder}: Success: ${successCount}, Errors: ${failureCount}, Skipped: ${
|
|
848
|
+
`📦 Upload Summary for folder ${folder}: Success: ${successCount}, Errors: ${failureCount}, Skipped: ${skippedCount}`,
|
|
346
849
|
);
|
|
347
850
|
} catch (err) {
|
|
348
851
|
console.error(`⚠️ Error processing folder ${folder}:`, err.message);
|
|
@@ -353,13 +856,22 @@ program
|
|
|
353
856
|
status: 'error',
|
|
354
857
|
message: err.message,
|
|
355
858
|
});
|
|
859
|
+
globalFailure++;
|
|
356
860
|
}
|
|
357
861
|
}
|
|
358
862
|
|
|
359
|
-
|
|
863
|
+
// Force flush any remaining logs before finishing
|
|
864
|
+
console.log(`\n📤 Flushing remaining logs...`);
|
|
865
|
+
await logBatcher.forceFlush();
|
|
866
|
+
|
|
867
|
+
// Final summary with clear separation
|
|
868
|
+
console.log(`\n${'='.repeat(50)}`);
|
|
869
|
+
console.log(`🎯 UPLOAD COMPLETED`);
|
|
870
|
+
console.log(`${'='.repeat(50)}`);
|
|
360
871
|
console.log(` ✅ Total uploaded: ${globalSuccess}`);
|
|
361
872
|
console.log(` ❌ Total with errors: ${globalFailure}`);
|
|
362
873
|
console.log(` 📜 Log file: ${logFilePath}`);
|
|
874
|
+
console.log(`${'='.repeat(50)}\n`);
|
|
363
875
|
});
|
|
364
876
|
|
|
365
877
|
program.parse();
|