-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.js
More file actions
226 lines (181 loc) · 8.02 KB
/
Copy pathmigrate.js
File metadata and controls
226 lines (181 loc) · 8.02 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
214
215
216
217
218
219
220
221
222
223
224
225
226
require('dotenv').config();
const { OneDriveClient, createDownloadStream, makeProgressState } = require('./onedrive');
const { GooglePhotosClient } = require('./google-photos');
const { Database } = require('./db');
const { logger } = require('./logger');
// ── Config ────────────────────────────────────────────────────
const BATCH_SIZE = 5;
const MAX_RETRIES = 3;
const MAX_FILE_SIZE_MB = process.env.MAX_FILE_SIZE_MB
? parseInt(process.env.MAX_FILE_SIZE_MB)
: null; // null = no limit
// ── Entry point ───────────────────────────────────────────────
async function main() {
const args = process.argv.slice(2);
if (args.includes('--status')) {
return showStatus();
}
logger.section('OneDrive → Google Photos Migration');
logger.info('Starting up...');
const db = new Database();
await db.init();
db.resetStuckFiles();
const onedrive = new OneDriveClient();
const gphotos = new GooglePhotosClient();
logger.section('Authentication');
await onedrive.authenticate();
await gphotos.authenticate();
const totalInDb = db.getTotalCount();
const pendingCount = db.getPendingCount();
if (totalInDb === 0) {
logger.section('Phase 1 — Scanning OneDrive');
logger.info('No files in DB yet. Scanning OneDrive for all images and videos...');
await scanOneDrive(onedrive, db);
} else {
logger.section('Phase 1 — Scan Skipped (Resuming)');
logger.info(`Found ${totalInDb} files already in DB`);
logger.info(`${pendingCount} files pending/failed → will upload those`);
if (pendingCount === 0) {
logger.success('Nothing left to do! All files already uploaded.');
logger.summary(db.getStats());
return;
}
}
logger.section('Phase 2 — Uploading to Google Photos');
await uploadAll(onedrive, gphotos, db);
logger.section('Migration Complete');
const stats = db.getStats();
logger.summary(stats);
if (stats.failed > 0) {
logger.warn(`${stats.failed} files failed. Run again to retry them automatically.`);
logger.warn('Run node migrate.js --status to see error details.');
}
}
// ── Scan ──────────────────────────────────────────────────────
async function scanOneDrive(onedrive, db) {
let totalFound = 0;
let pageNum = 0;
for await (const batch of onedrive.scanAllFiles()) {
pageNum++;
for (const file of batch) {
db.insertFile({
onedrive_id: file.id,
name: file.name,
size: file.size,
modified_date: file.lastModifiedDateTime,
mime_type: file.file?.mimeType || 'application/octet-stream',
onedrive_path: file.parentReference?.path || '/',
});
totalFound++;
}
logger.info(` Scan page ${pageNum}: ${totalFound} media files found so far...`);
}
logger.success(`Scan complete — ${totalFound} images/videos queued for upload`);
}
// ── Upload ────────────────────────────────────────────────────
async function uploadAll(onedrive, gphotos, db) {
let uploaded = 0;
let failed = 0;
let skipped = 0;
while (true) {
const batch = db.getPendingBatch(BATCH_SIZE);
if (batch.length === 0) break;
for (const file of batch) {
// Skip files that have permanently failed too many times
if (file.retry_count >= MAX_RETRIES) {
logger.warn(`Skipping "${file.name}" — failed ${file.retry_count} times, exceeded MAX_RETRIES`);
skipped++;
continue;
}
// Skip files exceeding size limit (if set)
if (MAX_FILE_SIZE_MB && file.size && file.size > MAX_FILE_SIZE_MB * 1024 * 1024) {
logger.warn(`Skipping "${file.name}" (${formatSize(file.size)}) — exceeds MAX_FILE_SIZE_MB limit of ${MAX_FILE_SIZE_MB}MB`);
skipped++;
continue;
}
logger.info(`Processing: "${file.name}" (${formatSize(file.size)}) [retry: ${file.retry_count}]`);
// ── Stream: OneDrive → Google Photos ─────────────────────
// Bytes flow chunk by chunk (~256KB at a time).
// The full file is never held in RAM — safe for files of any size.
let stream = null;
try {
db.markDownloading(file.id);
logger.debug(` → Resolving download URL...`);
const { finalUrl, fileSize } = await onedrive.getDownloadStream(
file.onedrive_id, file.name, file.size
);
// Open stream — data starts flowing immediately
// progressState is shared between download and upload so both bars
// render atomically on two dedicated lines without overwriting each other
const progressState = makeProgressState(fileSize);
stream = createDownloadStream(finalUrl, fileSize, progressState);
db.markUploading(file.id);
logger.debug(` → Streaming to Google Photos...`);
// onPhotoIdReceived is called the moment Google confirms the photo ID,
// before uploadPhoto returns — crash-safe checkpoint in DB
const photoId = await gphotos.uploadPhoto(
stream,
file.name,
file.mime_type,
fileSize,
file.modified_date,
(id) => db.markUploaded(file.id, id),
progressState
);
// stream is destroyed inside uploadPhoto on success — no cleanup needed here
stream = null;
db.markDone(file.id, photoId);
logger.success(` ✓ Uploaded: "${file.name}" → Google Photos ID: ${photoId}`);
uploaded++;
} catch (err) {
// Ensure stream is always destroyed on any error —
// this aborts the OneDrive CDN connection and frees the socket
if (stream && !stream.destroyed) {
stream.destroy();
stream = null;
}
// Classify error stage for accurate DB logging
const isDownloadErr = err.message.includes('Download') || err.message.includes('timed out');
const stage = isDownloadErr ? 'download' : 'upload';
const msg = `${stage === 'download' ? 'Download' : 'Upload'} failed: ${err.message}`;
logger.error(` ✗ ${file.name} — ${msg}`);
db.markFailed(file.id, stage, msg);
failed++;
}
// Progress snapshot every 10 files
if ((uploaded + failed) % 10 === 0 && (uploaded + failed) > 0) {
const stats = db.getStats();
logger.info(`── Progress: ${stats.done} done | ${stats.failed} failed | ${stats.pending} remaining ──`);
}
}
}
logger.info(`Upload phase complete: ${uploaded} uploaded, ${failed} failed, ${skipped} skipped`);
}
// ── Status command ────────────────────────────────────────────
async function showStatus() {
const db = new Database();
await db.init();
const stats = db.getStats();
logger.summary(stats);
if (stats.failed > 0) {
const logs = db.getErrorLogs();
logger.section('Recent Errors');
logs.forEach((log) => {
logger.error(`[${log.timestamp}] ${log.file_name} (stage: ${log.stage})`);
logger.error(` → ${log.error}`);
});
}
}
// ── Helpers ───────────────────────────────────────────────────
function formatSize(bytes) {
if (!bytes) return '?';
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`;
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
return `${(bytes / 1024 / 1024 / 1024).toFixed(2)} GB`;
}
main().catch((err) => {
logger.error(`Fatal: ${err.message}`);
logger.error(err.stack);
process.exit(1);
});