-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud-sync.js
More file actions
580 lines (499 loc) · 17.3 KB
/
Copy pathcloud-sync.js
File metadata and controls
580 lines (499 loc) · 17.3 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
const { google } = require('googleapis');
const fs = require('fs').promises;
const path = require('path');
const { pcToSwitch, switchToPc } = require('./converter.js');
const os = require('os');
const AdmZip = require('adm-zip');
class CloudSyncService {
constructor(settings) {
this.settings = settings || {};
this.auth = null;
this.drive = null;
this.authenticated = false;
this.jksvFolderId = null;
this.pendingOAuthClient = null;
this.deviceCode = null;
this.deviceCodeInterval = null;
this.deviceCodeExpiresIn = 1800;
}
async authenticate(credentialsPath) {
try {
if (!credentialsPath || !await this.fileExists(credentialsPath)) {
throw new Error('Credentials file not found. Please set up Google Drive credentials.');
}
const credentials = JSON.parse(await fs.readFile(credentialsPath, 'utf-8'));
// device flow oauth
const oauth2Client = new google.auth.OAuth2(
credentials.installed.client_id,
credentials.installed.client_secret,
'urn:ietf:wg:oauth:2.0:oob'
);
// check for stored tokens
const tokenPath = path.join(os.homedir(), '.stagstation', 'google-tokens.json');
let tokens = null;
try {
if (await this.fileExists(tokenPath)) {
tokens = JSON.parse(await fs.readFile(tokenPath, 'utf-8'));
oauth2Client.setCredentials(tokens);
}
} catch (error) {
// no tokens, need auth
}
if (!tokens) {
// device flow auth
const deviceCodeResponse = await this.requestDeviceCode(
credentials.installed.client_id,
credentials.installed.client_secret
);
// store oauth client and device code
this.pendingOAuthClient = oauth2Client;
this.deviceCode = deviceCodeResponse.device_code;
this.deviceCodeExpiresIn = deviceCodeResponse.expires_in || 1800;
this.deviceCodeInterval = deviceCodeResponse.interval || 5;
// return device code for user
return {
success: false,
needsAuth: true,
userCode: deviceCodeResponse.user_code,
verificationUrl: deviceCodeResponse.verification_url || 'https://google.com/device',
interval: deviceCodeResponse.interval || 5,
expiresIn: deviceCodeResponse.expires_in || 1800
};
}
this.auth = oauth2Client;
this.drive = google.drive({ version: 'v3', auth: oauth2Client });
this.authenticated = true;
// find/create JKSV folder
await this.findJKSVFolder();
return { success: true, authenticated: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async requestDeviceCode(clientId, clientSecret) {
const https = require('https');
const querystring = require('querystring');
const postData = querystring.stringify({
client_id: clientId,
scope: 'https://www.googleapis.com/auth/drive.file'
});
return new Promise((resolve, reject) => {
const options = {
hostname: 'oauth2.googleapis.com',
path: '/device/code',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const response = JSON.parse(data);
if (response.error) {
reject(new Error(response.error_description || response.error));
} else {
resolve(response);
}
} catch (error) {
reject(new Error(`Failed to parse device code response: ${error.message}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.write(postData);
req.end();
});
}
async pollForToken(clientId, clientSecret, deviceCode, interval = 5, expiresIn = 1800) {
const https = require('https');
const querystring = require('querystring');
const startTime = Date.now();
const timeout = expiresIn * 1000; // convert to milliseconds
return new Promise((resolve, reject) => {
const poll = async () => {
// check if we've exceeded the expiration time
if (Date.now() - startTime > timeout) {
reject(new Error('Device code expired. Please try connecting again.'));
return;
}
const postData = querystring.stringify({
client_id: clientId,
client_secret: clientSecret,
device_code: deviceCode,
grant_type: 'urn:ietf:params:oauth:grant-type:device_code'
});
const options = {
hostname: 'oauth2.googleapis.com',
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const response = JSON.parse(data);
if (response.error) {
if (response.error === 'authorization_pending') {
// still waiting for user authorization, poll again
setTimeout(poll, interval * 1000);
} else if (response.error === 'slow_down') {
// rate limited, wait longer
setTimeout(poll, (interval + 5) * 1000);
} else {
reject(new Error(response.error_description || response.error));
}
} else {
// got the token
resolve(response);
}
} catch (error) {
reject(new Error(`Failed to parse token response: ${error.message}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.write(postData);
req.end();
};
// start polling
poll();
});
}
async completeAuth(credentialsPath) {
try {
if (!this.pendingOAuthClient || !this.deviceCode) {
throw new Error('No pending OAuth client or device code. Please call authenticate first.');
}
const credentials = JSON.parse(await fs.readFile(credentialsPath, 'utf-8'));
// poll for token
const tokenResponse = await this.pollForToken(
credentials.installed.client_id,
credentials.installed.client_secret,
this.deviceCode,
this.deviceCodeInterval,
this.deviceCodeExpiresIn
);
// set credentials on OAuth client
this.pendingOAuthClient.setCredentials({
access_token: tokenResponse.access_token,
refresh_token: tokenResponse.refresh_token,
expiry_date: Date.now() + (tokenResponse.expires_in * 1000)
});
// save tokens
const tokenPath = path.join(os.homedir(), '.stagstation');
await fs.mkdir(tokenPath, { recursive: true });
await fs.writeFile(
path.join(tokenPath, 'google-tokens.json'),
JSON.stringify({
access_token: tokenResponse.access_token,
refresh_token: tokenResponse.refresh_token,
expiry_date: Date.now() + (tokenResponse.expires_in * 1000)
}, null, 2)
);
this.auth = this.pendingOAuthClient;
this.drive = google.drive({ version: 'v3', auth: this.pendingOAuthClient });
this.authenticated = true;
this.pendingOAuthClient = null; // clear pending client
this.deviceCode = null; // clear device code
await this.findJKSVFolder();
return { success: true, authenticated: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async findJKSVFolder() {
try {
// search for JKSV folder
const response = await this.drive.files.list({
q: "name='JKSV' and mimeType='application/vnd.google-apps.folder' and trashed=false",
fields: 'files(id, name)',
spaces: 'drive'
});
if (response.data.files.length > 0) {
this.jksvFolderId = response.data.files[0].id;
return this.jksvFolderId;
}
// create JKSV folder if it doesn't exist
const folderMetadata = {
name: 'JKSV',
mimeType: 'application/vnd.google-apps.folder'
};
const folder = await this.drive.files.create({
resource: folderMetadata,
fields: 'id'
});
this.jksvFolderId = folder.data.id;
return this.jksvFolderId;
} catch (error) {
throw new Error(`Failed to find/create JKSV folder: ${error.message}`);
}
}
// NOTE: This method is currently unused. JKSV saves are stored directly in game folders,
// not in user-specific subfolders. This method was originally intended for user folder support
// but is not needed with the current implementation.
async findUserFolder(userName) {
if (!this.jksvFolderId) {
await this.findJKSVFolder();
}
try {
const response = await this.drive.files.list({
q: `'${this.jksvFolderId}' in parents and name='${userName}' and mimeType='application/vnd.google-apps.folder' and trashed=false`,
fields: 'files(id, name)'
});
if (response.data.files.length > 0) {
return response.data.files[0].id;
}
// create user folder
const folderMetadata = {
name: userName,
mimeType: 'application/vnd.google-apps.folder',
parents: [this.jksvFolderId]
};
const folder = await this.drive.files.create({
resource: folderMetadata,
fields: 'id'
});
return folder.data.id;
} catch (error) {
throw new Error(`Failed to find/create user folder: ${error.message}`);
}
}
async findGameFolder(gameName) {
if (!this.jksvFolderId) {
await this.findJKSVFolder();
}
try {
// look directly in JKSV folder, skip user folder
const response = await this.drive.files.list({
q: `'${this.jksvFolderId}' in parents and name='${gameName}' and mimeType='application/vnd.google-apps.folder' and trashed=false`,
fields: 'files(id, name)'
});
if (response.data.files.length > 0) {
return response.data.files[0].id;
}
// create game folder directly in JKSV
const folderMetadata = {
name: gameName,
mimeType: 'application/vnd.google-apps.folder',
parents: [this.jksvFolderId]
};
const folder = await this.drive.files.create({
resource: folderMetadata,
fields: 'id'
});
return folder.data.id;
} catch (error) {
throw new Error(`Failed to find/create game folder: ${error.message}`);
}
}
async listSaves(game) {
if (!this.authenticated) {
throw new Error('Not authenticated with Google Drive');
}
const gameName = game === 'hollowknight' ? 'Hollow Knight' : 'Hollow Knight Silksong';
const gameFolderId = await this.findGameFolder(gameName);
try {
// JKSV stores saves as .zip files directly in the game folder
const response = await this.drive.files.list({
q: `'${gameFolderId}' in parents and name contains '.zip' and trashed=false`,
fields: 'files(id, name, modifiedTime)',
orderBy: 'modifiedTime desc'
});
const saves = [];
for (const zipFile of response.data.files) {
// download and inspect zip to get slot information
try {
const tempZipPath = path.join(os.tmpdir(), `stagstation_${Date.now()}_${zipFile.name}`);
await this.downloadSaveFile(zipFile.id, tempZipPath);
const zip = new AdmZip(tempZipPath);
const zipEntries = zip.getEntries();
const saveFiles = zipEntries
.filter(entry => entry.entryName.match(/^user\d+\.dat$/i) && !entry.isDirectory)
.map(entry => {
const match = entry.entryName.match(/^user(\d+)\.dat$/i);
return {
id: zipFile.id, // Use zip file ID
name: entry.entryName,
modifiedTime: zipFile.modifiedTime,
slotNumber: parseInt(match ? match[1] : '0'),
entryName: entry.entryName
};
});
// clean up temp file
await fs.unlink(tempZipPath).catch(() => {});
if (saveFiles.length > 0) {
// remove .zip extension from name for display
const displayName = zipFile.name.replace(/\.zip$/i, '');
saves.push({
id: zipFile.id,
name: displayName,
modifiedTime: zipFile.modifiedTime,
files: saveFiles,
zipFileName: zipFile.name
});
}
} catch (error) {
console.error(`Error processing zip ${zipFile.name}:`, error);
// continue with other files
}
}
return saves;
} catch (error) {
throw new Error(`Failed to list saves: ${error.message}`);
}
}
async downloadSaveFile(fileId, outputPath) {
try {
const response = await this.drive.files.get(
{ fileId, alt: 'media' },
{ responseType: 'stream' }
);
const writeStream = require('fs').createWriteStream(outputPath);
response.data.pipe(writeStream);
return new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
} catch (error) {
throw new Error(`Failed to download file: ${error.message}`);
}
}
async uploadFile(filePath, fileName, parentFolderId) {
try {
const fileMetadata = {
name: fileName,
parents: [parentFolderId]
};
const media = {
mimeType: 'application/octet-stream',
body: require('fs').createReadStream(filePath)
};
const file = await this.drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id, name, modifiedTime'
});
return file.data;
} catch (error) {
throw new Error(`Failed to upload file: ${error.message}`);
}
}
async compareSlot(game, slotNumber, localPath) {
try {
if (!await this.fileExists(localPath)) {
return {
status: 'cloud-only',
localTime: null,
cloudTime: null,
action: null
};
}
const localStats = await fs.stat(localPath);
const localTime = localStats.mtime;
// find cloud version (no userName needed)
const saves = await this.listSaves(game);
let cloudTime = null;
let cloudFile = null;
for (const save of saves) {
const slotFile = save.files.find(f => f.slotNumber === slotNumber);
if (slotFile) {
cloudTime = new Date(slotFile.modifiedTime);
cloudFile = {
...slotFile,
saveId: save.id,
zipFileName: save.zipFileName || save.name + '.zip'
};
break;
}
}
if (!cloudTime) {
return {
status: 'local-only',
localTime: localTime,
cloudTime: null,
action: 'upload'
};
}
const timeDiff = localTime.getTime() - cloudTime.getTime();
const threshold = 1000; // 1 second threshold
if (Math.abs(timeDiff) < threshold) {
return {
status: 'in-sync',
localTime: localTime,
cloudTime: cloudTime,
action: null,
cloudFile: cloudFile
};
} else if (timeDiff > 0) {
return {
status: 'local-newer',
localTime: localTime,
cloudTime: cloudTime,
action: 'upload',
cloudFile: cloudFile
};
} else {
return {
status: 'cloud-newer',
localTime: localTime,
cloudTime: cloudTime,
action: 'download',
cloudFile: cloudFile
};
}
} catch (error) {
throw new Error(`Failed to compare slot: ${error.message}`);
}
}
async fileExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}
async updateLocalFileTimestamp(localPath, cloudModifiedTime) {
try {
const cloudTime = new Date(cloudModifiedTime);
await fs.utimes(localPath, cloudTime, cloudTime);
} catch (error) {
// non-fatal - log but don't throw
console.warn(`Failed to update local file timestamp: ${error.message}`);
}
}
async createBackup(filePath, backupDir) {
await fs.mkdir(backupDir, { recursive: true });
// generate timestamp
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const timestamp = `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
const fileName = path.basename(filePath);
const backupPath = path.join(backupDir, `${fileName}_${timestamp}`);
await fs.copyFile(filePath, backupPath);
return backupPath;
}
}
module.exports = { CloudSyncService };