-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (122 loc) · 3.97 KB
/
index.js
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import { program } from "commander";
import ora from "ora";
import chalk from "chalk";
import { list } from "@vercel/blob";
dotenv.config();
const VERCEL_PROJECT_ID = process.env.VERCEL_PROJECT_ID;
const VERCEL_ACCESS_TOKEN = process.env.VERCEL_ACCESS_TOKEN;
const VERCEL_STORE_ID = process.env.VERCEL_STORE_ID;
const MAX_RETRIES = 3;
const CONCURRENT_DOWNLOADS = 5;
program
.option("-d, --dir <path>", "Download directory", "./downloads")
.option("-f, --force", "Force download existing files", false)
.option(
"-c, --concurrency <number>",
"Number of concurrent downloads",
CONCURRENT_DOWNLOADS
)
.option("-s, --store <id>", "Vercel Blob Store ID")
.parse(process.argv);
const options = program.opts();
const DOWNLOAD_DIR = path.resolve(process.cwd(), options.dir);
async function listFiles() {
const spinner = ora("Fetching file list...").start();
try {
let cursor;
const allBlobs = [];
do {
const response = await list({
cursor,
limit: 1000,
});
allBlobs.push(...response.blobs);
cursor = response.cursor;
} while (cursor);
spinner.succeed(`Found ${allBlobs.length} files`);
return allBlobs;
} catch (error) {
spinner.fail("Failed to fetch file list");
throw error;
}
}
async function downloadFile(blob) {
const filePath = path.join(DOWNLOAD_DIR, blob.pathname);
if (!options.force && fs.existsSync(filePath)) {
return { skipped: true, path: blob.pathname };
}
// Get the download URL from the blob object
const downloadUrl = blob.downloadUrl || blob.url;
const response = await fetch(downloadUrl);
if (!response.ok) {
throw new Error(
`Failed to download: ${response.statusText} (${response.status})`
);
}
// Ensure the directory exists
fs.mkdirSync(path.dirname(filePath), { recursive: true });
// Download and write the file
const buffer = await response.arrayBuffer();
fs.writeFileSync(filePath, Buffer.from(buffer));
return { skipped: false, path: blob.pathname };
}
async function downloadAllFiles() {
try {
if (!VERCEL_PROJECT_ID || !VERCEL_ACCESS_TOKEN) {
throw new Error(
"Missing required environment variables: VERCEL_PROJECT_ID or VERCEL_ACCESS_TOKEN"
);
}
const blobs = await listFiles();
if (blobs.length === 0) {
console.log(chalk.yellow("No files found to download."));
return;
}
console.log(chalk.blue("\nStarting downloads..."));
const results = { successful: 0, skipped: 0, failed: 0 };
const errors = [];
// Process downloads in chunks
for (let i = 0; i < blobs.length; i += options.concurrency) {
const chunk = blobs.slice(i, i + options.concurrency);
const downloads = chunk.map(async (blob) => {
try {
const result = await downloadFile(blob);
if (result.skipped) {
console.log(
chalk.yellow(`Skipped: ${result.path} (already exists)`)
);
results.skipped++;
} else {
console.log(chalk.green(`Downloaded: ${result.path}`));
results.successful++;
}
} catch (error) {
console.log(chalk.red(`Failed: ${blob.pathname}`));
errors.push({ path: blob.pathname, error: error.message });
results.failed++;
}
});
await Promise.all(downloads);
}
// Print summary
console.log("\nDownload Summary:");
console.log(
chalk.green(`✓ Successfully downloaded: ${results.successful}`)
);
console.log(chalk.yellow(`⚠ Skipped: ${results.skipped}`));
console.log(chalk.red(`✗ Failed: ${results.failed}`));
if (errors.length > 0) {
console.log("\nErrors:");
errors.forEach(({ path, error }) => {
console.log(chalk.red(`${path}: ${error}`));
});
}
} catch (error) {
console.error(chalk.red(`\nError: ${error.message}`));
process.exit(1);
}
}
downloadAllFiles();