in util/upload/src/upload.ts [61:149]
export async function main() {
const startTime = performance.now();
const path = programOptions["<path>"];
const batchSize = programOptions["--batchsize"];
let promiseBatch: Promise<Result>[] = [];
const counter: Counter = {
files: 0,
skipped: 0,
completed: 0,
errors: 0
};
// delete flag
if (programOptions["--clean"]) {
await minioUtils.cleanBucket();
}
if (path === null || path === undefined) {
if (programOptions["--verbose"]) {
console.log("Path is not set");
}
return 0;
}
// Check if path exists
const pathFoundAsDir = await fs.exists(path);
if (!pathFoundAsDir) {
console.error(`ERROR: ${path} is not a directory`);
return;
}
// list files in path recursively
const glob = new Glob(`${path}/**/*`);
for await (const file of glob.scan({onlyFiles: true})) {
// remove path from folder and prepend the result to entry
let fileAddr = file.substring(path.length + 1);
if (fileAddr.startsWith("/")) {
fileAddr = fileAddr.substring(1);
}
const promise: Promise<Result> = (async () => {
counter.files++;
// upload command
const fileItem = Bun.file(file);
if (fileItem.size > 0) {
return await minioUtils.uploadContent(file, fileAddr);
} else {
console.log(`Skipped empty file ${file}`);
return await new Promise((resolve) => {
resolve(Result.SKIPPED);
});
}
})();
promiseBatch.push(promise);
// If the batch size reaches batchSize, wait for them to resolve
if (promiseBatch.length === batchSize) {
const results = await Promise.all<Result>(promiseBatch);
parseResults(results, counter);
promiseBatch = []; // clear the batch
}
}
// Process remaining promises (if any) in the last batch
if (promiseBatch.length > 0) {
const results = await Promise.all<Result>(promiseBatch);
parseResults(results, counter);
}
const endTime = performance.now();
const executionTime = endTime - startTime;
console.log(`==================| UPLOAD RESULTS |==================`);
console.log(`| FILES : ${counter.files}`);
console.log(`| COMPLETED : ${counter.completed}`);
console.log(`| ERRORS : ${counter.errors}`);
console.log(`| SKIPPED : ${counter.skipped}`);
console.log(`| EXEC. TIME : ${executionTime.toFixed(2)} ms`);
console.log("======================================================");
return 0;
}