-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathassets_js_lib_gd_api.js
683 lines (654 loc) · 20.8 KB
/
assets_js_lib_gd_api.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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
$(document).ready(function() {
init_access();
// t_expired
// fetch_creds
// fetch_access
// refcb
// lca_obj
// init_login_dialog
// oauth_pop_delay
// oauth_pop
gd_login_trigger();
submit_gdbu_dialog();
// g_login
// gdlogin_callbacks
// adjust_sp
// g_logout
// activate
// deactivate
// gdlogout_callbacks
Drive_Backup_trigger();
// updateappdata
// createfile
lad_trigger();
// listappdata
deletefiletrigger()
// deletefile
// GD_pass
});
// ** Google api **
// Initializes access based on the provided access key or global password
function init_access(ak) {
if (ak) {
fetch_creds(ak);
return
}
const p = GD_pass();
if (p.pass) {
glob_const.html.addClass("gdauth");
return
}
if (!p.active) {
return
}
}
// Handles token expiration and refreshes access if necessary
function t_expired(expired, callback) {
if (glob_const.hostlocation === "local") {
return
}
if (expired === "norefresh") {
if (callback === "gcb") {
g_login();
return
}
oauth_pop_delay(true);
return
}
fetch_access(lnurl_decode_c(expired), callback);
}
// Fetches credentials using the provided access key
function fetch_creds(k) {
if (k) {
api_proxy({
"custom": "fetch_creds",
"api_url": true,
"proxy": true,
"code": decodeURIComponent(k),
"redirect_uri": glob_const.redirect_uri,
"grant_type": "authorization_code"
}).done(function(e) {
if (e) {
const data = br_result(e);
if (data) {
const result = data.result;
if (result) {
const error = result.error;
if (error) {
const ed = result.error_description,
em = (ed) ? " || " + ed : "";
notify(error + em);
return
}
const ga_token = result.access_token;
if (ga_token) {
const jt = {
"created": now(),
"active": true,
"access_token": ga_token,
"expires_in": result.expires_in
};
br_set_local("dat", JSON.stringify(jt));
const rt = result.refresh_token;
if (rt) {
const jtobj = {
"d": lnurl_encode("xz", rt)
}
br_set_local("rt", JSON.stringify(jtobj));
}
gdlogin_callbacks();
if (glob_const.body.hasClass("showstartpage")) { // only show when logged in
trigger_restore();
}
const timeout = setTimeout(function() {
history.pushState({
"pagename": "settings"
}, "", glob_const.redirect_uri);
}, 5000, function() {
clearTimeout(timeout);
});
}
}
}
}
}).fail(function(e) {
console.error("error", e);
}).always(function(e) {});
}
}
// Fetches a new access token using the provided refresh token
function fetch_access(rt, callback) {
if (rt) {
api_proxy({
"custom": "fetch_creds",
"api_url": true,
"proxy": true,
"refresh_token": rt,
"grant_type": "refresh_token"
}).done(function(e) {
if (e) {
const data = br_result(e);
if (data) {
const result = data.result;
if (result) {
const error = result.error;
if (error) {
const ed = result.error_description,
em = ed ? " || " + ed : "";
if (ed.indexOf("expired") >= 0 || ed.indexOf("revoked") >= 0) {
br_remove_local("rt");
oauth_pop_delay();
return;
}
notify(error + em);
return
}
const ga_token = result.access_token;
if (ga_token) {
const jt = {
"created": now(),
"active": true,
"access_token": ga_token,
"expires_in": result.expires_in
};
br_set_local("dat", JSON.stringify(jt));
refcb(callback);
}
}
}
}
}).fail(function(e) {
console.error("error", e);
});
}
}
// Handles callbacks after refreshing access token
function refcb(cb) {
glob_const.html.addClass("gdauth");
if (cb) {
if (cb === "uad") {
const p = GD_pass();
if (p.pass) {
updateappdata(p);
return
}
}
if (cb === "gcb") {
adjust_sp();
}
}
}
// Retrieves the local access token object
function lca_obj() {
const bdat = br_get_local("dat", true);
if (bdat) {
return bdat;
}
return false;
}
// Retrieves the local refresh token object
function rt_obj() {
const rtdat = br_get_local("rt", true);
return rtdat ? rtdat.d : false;
}
// Initializes the login dialog based on the provided parameters
function init_login_dialog(p) {
if (glob_const.hostlocation === "local") {
notify(translate("ganot"));
return
}
if (p.expired) {
t_expired(p.expired, "gcb");
return
}
if (p.active) {
g_login();
return
}
if (p.token) {
activate();
gdlogin_callbacks();
}
}
// Delays the OAuth popup with an optional abort parameter
function oauth_pop_delay(ab) {
canceldialog();
const timeout = setTimeout(function() {
oauth_pop(ab);
}, 1200, function() {
clearTimeout(timeout);
});
}
// Displays the OAuth popup dialog for Google Drive authentication
function oauth_pop(ab) {
const cbx = ab ? render_html([{
"div": {
"id": "pk_confirmwrap",
"class": "cb_wrap",
"attr": {
"data-checked": "false"
},
"content": [{
"span": {
"class": "checkbox"
}
}]
},
"span": {
"content": translate("stopgauth")
}
}]) : "",
ddat = [{
"div": {
"class": "popform",
"content": [{
"div": {
"class": "inputwrap",
"content": "<p><strong>" + translate("gauthsafely") + "</strong><br/>" + translate("gauthsync") + "</p>"
},
},
{
"div": {
"class": "inputwrap",
"content": "<div id='oauth_onload'><span class='icon-google2'></span>" + translate("signin") + "</div>"
}
}
]
}
},
{
"div": {
"id": "pk_confirm",
"class": "noselect",
"content": cbx
}
},
{
"input": {
"class": "submit",
"attr": {
"type": "submit",
"value": translate("okbttn")
}
}
}
],
content = template_dialog({
"id": "gdbu_dialog",
"icon": "icon-googledrive",
"title": translate("backuptogd"),
"elements": ddat
});
popdialog(content, "triggersubmit");
}
// Sets up event listener for Google Drive login trigger
function gd_login_trigger() {
$(document).on("click", "#oauth_onload", function() {
g_login();
})
}
// Handles submission of Google Drive backup dialog
function submit_gdbu_dialog() {
$(document).on("click", "#gdbu_dialog input.submit", function(e) {
e.preventDefault();
const gdbu_dialog = $("#gdbu_dialog"),
gd_checkbox = gdbu_dialog.find("#pk_confirmwrap"),
gd_checked = gd_checkbox.data("checked");
if (gd_checked == true) {
g_logout();
return
}
g_login();
})
}
// Initiates Google Drive login process
function g_login() {
if (glob_const.hostlocation === "local") {
notify(translate("ganot"));
return
}
const p = GD_pass();
if (p.pass) {
gdlogin_callbacks();
return
}
if (!p.active && !p.expired) {
if (p.token) {
activate();
gdlogin_callbacks();
return
}
}
const consent = p.expired == "norefresh" ? "&prompt=consent" : "",
login_uri = "https://accounts.google.com/o/oauth2/auth?client_id=" + to.ga_id + "&redirect_uri=" + glob_const.redirect_uri + "&response_type=code&scope=" + glob_const.scope + "&access_type=offline" + consent;
glob_const.w_loc.href = login_uri;
}
// Performs callbacks after successful Google Drive login
function gdlogin_callbacks(close) {
glob_const.html.addClass("gdauth");
notify(translate("gdsignedin"));
resetchanges();
adjust_sp();
if (close) {
canceldialog();
}
}
// Adjusts the switch panel after Google Drive login
function adjust_sp() {
const switch_panel = $("#popup.showpu .switchpanel");
if (switch_panel.length) {
switch_panel.addClass("true").removeClass("false");
const lad = $("#listappdata");
if (lad.length) {
listappdata();
return
}
$("#changelog").slideUp(300);
return
}
}
// Initiates Google Drive logout process
function g_logout() {
const result = confirm(translate("stopgdalert"));
if (result) {
deactivate();
gdlogout_callbacks();
}
}
// Activates Google Drive authentication
function activate() {
const bdat = lca_obj();
if (bdat) {
bdat.active = true;
br_set_local("dat", JSON.stringify(bdat));
}
}
// Deactivates Google Drive authentication
function deactivate() {
const bdat = lca_obj();
if (bdat) {
bdat.active = false;
br_set_local("dat", JSON.stringify(bdat));
}
}
// Performs callbacks after Google Drive logout
function gdlogout_callbacks() {
glob_const.html.removeClass("gdauth");
notify(translate("gdsignedout"));
const switch_panel = $("#popup.showpu .switchpanel");
if (switch_panel.length) {
switch_panel.removeClass("true").addClass("false");
$("#changelog").slideDown(300);
return
}
resetchanges();
canceldialog();
}
// Sets up event listener for Drive Backup trigger
function Drive_Backup_trigger() {
$(document).on("click", "#gdtrigger .switchpanel", function() {
const p = GD_pass();
if (p.pass) {
g_logout();
return
}
init_login_dialog(p);
})
}
// Updates app data in Google Drive
function updateappdata(p) {
const gd_timer = br_get_session("gd_timer"); // prevent Ddos
if (gd_timer && (now() - gd_timer) < 3000) {
return;
}
const token = p.token;
if (!token) {
return
}
const bu_id = br_get_local("backupfile_id");
if (bu_id) {
br_set_session("gd_timer", now());
const ddat = {
"api_url": glob_const.drivepath + "/upload/drive/v3/files/" + bu_id + "?uploadType=media&alt=json",
"proxy": false,
"params": {
"method": "PATCH",
"dataType": "json",
"contentType": "application/json",
"headers": {
"Authorization": "Bearer " + token
},
"data": complilebackup()
}
};
api_proxy(ddat).done(function(e) {
// Success handling if needed
}).fail(function(xhr, stat, err) {
if (textStatus === "error") {
const error_object = xhr;
if (error_object) {
const resp_obj = error_object.responseJSON;
if (resp_obj) {
const resp = resp_obj.error;
if (resp) {
if (resp.code === 401) {
notify(translate("unauthorized"));
return
}
if (resp.code === 404) {
createfile(token); // create file
return
}
}
}
}
notify(translate("error"));
}
});
return
}
createfile(token) // create file
}
// Creates a new file in Google Drive with the provided token
function createfile(token) {
const jt = GD_pass(),
jtp = jt.pass,
pass = token || (jtp ? jt.token : false),
backup = complilebackup();
if (pass) {
const file = new Blob([backup], {
"type": "text/plain"
}),
description = {
"modified": now_utc(),
"device": getdevicetype(),
"deviceid": glob_const.deviceid
},
metadata = {
"name": complilefilename(),
"parents": ["appDataFolder"],
"mimeType": "text/plain",
"description": JSON.stringify(description)
},
form = new FormData(),
xhr = new XMLHttpRequest();
form.append("metadata", new Blob([JSON.stringify(metadata)], {
"type": "application/json"
}));
form.append("file", file);
xhr.open("post", "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id");
xhr.setRequestHeader("Authorization", "Bearer " + pass);
xhr.responseType = "json";
xhr.onload = function() {
const response_id = xhr.response.id;
if (response_id) {
br_set_local("backupfile_id", response_id.toString());
}
};
xhr.send(form);
}
}
// Sets up event listener for listing app data
function lad_trigger() {
$(document).on("click", "#listappdata .switchpanel", function() {
listappdata();
})
}
// Lists app data from Google Drive
function listappdata() {
const p = GD_pass();
if (!p.pass) {
init_login_dialog(p);
return
}
const thistrigger = $("#listappdata .switchpanel"),
backuplist = $("#gd_backuplist"),
importjsonlist = $("#importjson");
if (backuplist.find("li").length) {
if (thistrigger.hasClass("true")) {
thistrigger.removeClass("true");
backuplist.slideUp(300);
importjsonlist.slideDown(300);
return
}
thistrigger.addClass("true");
backuplist.slideDown(300);
importjsonlist.slideUp(300);
return
}
api_proxy({
"api_url": glob_const.drivepath + "/drive/v3/files?pageSize=10&spaces=appDataFolder&fields=*",
"proxy": false,
"params": {
"method": "GET",
"headers": {
"Authorization": "Bearer " + p.token
}
}
}).done(function(e) {
const filelist = e.files;
if (filelist.length) {
const sorted_filelist = filelist.sort(function(a, b) { // sort array by timestamp
const amod = a.modifiedTime,
bmod = b.modifiedTime,
d1 = amod ? to_ts(amod) : 2,
d2 = bmod ? to_ts(bmod) : 1;
return d2 - d1; // descending order
}),
gdbackuppush = [];
$.each(sorted_filelist, function(i, value) {
const description = JSON.parse(value.description),
device = description.device,
device_id = description.deviceid,
dmod = short_date(description.modified),
mod = short_date(to_ts(value.modifiedTime)),
trash = (device_id === glob_const.deviceid) ? "<div class='purge_bu icon-bin'></div>" : "",
gdbackups = "<li data-gdbu_id='" + value.id + "' data-device-id='" + device_id + "' data-device='" + device + "'><div class='restorefile icon-" + device + "' title='" + device + " (Created: " + dmod + ")'>" + mod + "<span class='lmodified'> (" + (value.size / 1000).toFixed(0) + " KB)</div>" + trash + "</li>";
gdbackuppush.push(gdbackups);
});
backuplist.prepend(gdbackuppush.join("")).slideDown(300);
} else {
backuplist.prepend("<li>No files found</li>").slideDown(300);
}
importjsonlist.slideUp(300);
thistrigger.addClass("true");
}).fail(function(xhr, stat, err) {
if (stat === "error") {
if (err === "Unauthorized") {
thistrigger.removeClass("true");
backuplist.slideUp(300);
importjsonlist.slideDown(300);
notify(translate("unauthorized"));
return
}
if (err === "Not Found") {
createfile(); // create file
return
}
notify(translate("error"));
}
});
}
// Sets up event listener for deleting a file
function deletefiletrigger() {
$(document).on("click", ".purge_bu", function() {
const p = GD_pass();
if (!p.pass) {
init_login_dialog(p);
return
}
const thislist = $(this).parent("li"),
fileid = thislist.attr("data-gdbu_id"),
result = confirm(translate("deletefile", {
"file": thislist.text()
}));
if (result) {
deletefile(fileid, thislist, p.token);
}
})
}
// Deletes a file from Google Drive
function deletefile(fileId, thislist, pass) {
api_proxy({
"api_url": glob_const.drivepath + "/drive/v3/files/" + fileId,
"proxy": false,
"params": {
"method": "DELETE",
"headers": {
"Authorization": "Bearer " + pass
}
}
}).done(function(e) {
if (thislist) {
thislist.slideUp(300);
notify(translate("filedeleted"));
}
}).fail(function(xhr, stat, err) {
if (stat === "error") {
if (err === "Not Found") {
notify(translate("error") + ": " + translate("filenotfound"));
return
}
notify(translate("error"));
}
});
}
// Retrieves and processes Google Drive authentication information
function GD_pass() {
const jt = {
"token": false,
"active": false,
"expired": false,
"pass": false
},
bdat = lca_obj(),
rtoken = rt_obj(),
can_refresh = rtoken || "norefresh";
if (bdat) {
const token = bdat.access_token,
ttime = (now() - bdat.created) + 60000,
extime = bdat.expires_in * 1000,
expired = (ttime > extime),
expirin = (extime - ttime),
active = bdat.active;
if (token) {
jt.token = token;
jt.expires_in = expirin;
if (expired) {
jt.expired = can_refresh;
}
if (active) {
jt.active = true;
}
}
if (token && !expired && active) {
glob_const.html.addClass("gdauth");
jt.pass = true;
} else {
glob_const.html.removeClass("gdauth");
}
} else {
jt.expired = can_refresh;
}
return jt;
}