forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.odc.api.js
753 lines (628 loc) · 22.1 KB
/
drive.odc.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
* One Drive CN API
* 使用官方API挂载
*/
const name = 'OneDriveCNAPI'
const version = '1.0'
const protocols = ['odc']
const defaultProtocol = 'odc'
const fs = require('fs')
const path = require('path')
const { PassThrough } = require('stream')
const onedrive_max_age_dir = 3600 * 1000 * 0.9
const { URL } = require('url')
const urlFormat = require('url').format
const isSecretUrl = (url) => {
return !(url.includes('://localhost') == false && url.startsWith('https') == false)
}
class oauth2ForOD {
constructor(request, qs, handleUpdate) {
this.request = request
this.qs = qs
this.handleUpdate = handleUpdate
this.clientMap = {}
this.pathAppMap = {}
this.SCOPES = ['offline_access','files.readwrite.all'];
this.OAUTH2_AUTH_BASE_URL = 'https://login.partner.microsoftonline.cn/common/oauth2/authorize'
this.OAUTH2_TOKEN_URL = 'https://login.partner.microsoftonline.cn/common/oauth2/token'
this.PROXY_URL = 'https://reruin.github.io/redirect/onedrive.html'
}
init(data) {
for(let { path ,credentials } of data){
let { client_id , client_secret , refresh_token , redirect_uri , tenant } = credentials
if( client_id ){
this.clientMap[client_id] = {
client_id, client_secret, refresh_token,redirect_uri,tenant,
update_time:0,
expires_in:0
}
}
}
}
async generateAuthUrl(config) {
let { client_id, client_secret, tenant, redirect_uri } = config
const opts = {
client_id,
scope: this.SCOPES.join(' '),
// access_type: 'offline',
// prompt:'consent',
response_type: 'code',
redirect_uri
};
if(!isSecretUrl(redirect_uri)){
opts.redirect_uri = this.PROXY_URL
opts.state = redirect_uri
//redirect_uri = this.PROXY_URL
}
this.pathAppMap[redirect_uri] = { client_id, client_secret, tenant, redirect_uri:opts.redirect_uri, create_time: Date.now() }
return `${this.OAUTH2_AUTH_BASE_URL}?${this.qs.stringify(opts)}`;
}
//验证code 并获取 credentials
async getToken(key, code) {
let appConfig = this.pathAppMap[key]
if (!appConfig) return { error: true, msg: '没有匹配到app_id. key:'+key }
let { client_id, client_secret, tenant, redirect_uri } = appConfig
return await this.authToken({ client_id, client_secret, tenant, redirect_uri , code },key)
}
async authToken({client_id, client_secret, tenant, redirect_uri , code} , key){
let params = {
client_id,
client_secret,
redirect_uri,
code,
resource:`https://${tenant}-my.sharepoint.cn`,
grant_type: 'authorization_code'
}
let resp
try {
resp = await this.request.post(this.OAUTH2_TOKEN_URL, params, { json: true })
} catch (e) {
console.log(e)
resp = e
}
if(!resp.body) return { error: true, msg: '没有收到返回结果。' }
if (resp.body && !resp.body.error) {
let { refresh_token, expires_in, access_token } = resp.body
this.clientMap[client_id] = { client_id, client_secret, tenant, redirect_uri, refresh_token, expires_in, access_token , update_time:Date.now() }
if(key) delete this.pathAppMap[key]
await this.handleUpdate(this.clientMap[client_id])
return this.clientMap[client_id]
} else {
return { error: true, msg: resp.body ? resp.body.error_description : 'unknow error' }
}
}
async getCredentials(client_id) {
let credentials = this.clientMap[client_id]
if (credentials) {
let { update_time, expires_in , refresh_token } = credentials
if ((Date.now() - update_time) < expires_in * 1000 * 0.9) {
return credentials
}
else if(refresh_token){
return await this.refreshAccessToken(credentials)
}
else{
return credentials
}
}
}
async refreshAccessToken(credentials) {
let { client_id, client_secret, redirect_uri, refresh_token , tenant } = credentials
if (client_id && client_secret /*&& redirect_uri*/ && refresh_token) {
let params = {
client_id,
client_secret,
redirect_uri,
refresh_token,
resource:`https://${tenant}-my.sharepoint.cn`,
grant_type: 'refresh_token'
}
try {
let resp = await this.request.post(this.OAUTH2_TOKEN_URL, params, { json: true })
let body = resp.body
if (body.access_token) {
credentials.access_token = body.access_token
credentials.expires_in = body.expires_in
credentials.update_time = Date.now()
if(body.refresh_token) credentials.refresh_token = body.refresh_token
this.clientMap[client_id] = credentials
await this.handleUpdate(this.clientMap[client_id])
console.log('refreshAccessToken success')
return credentials
}
} catch (e) {
return {error:true , msg:e.body ? e.body.error_description:'挂载失败'}
}
}
return {error:true , msg:'refreshAccessToken 失败,缺少参数'}
}
}
const error = async (msg, href) => {
return `
<div class="auth">
<h3>挂载 OneDrive 失败</h3>
<p style="font-size:12px;">${msg}</p>
<p style="font-size:12px;"><a style="font-size:12px;margin-right:5px;color:#337ab7;" href="${href}">点此重新开始</a></p>
</div>
`
}
const install = async (redirect_uri , createLink) => {
const isSecret = !isSecretUrl(redirect_uri) ? '<p>当前地址不符合OneDrive安全要求,故采用<a target="_blank" href="https://github.com/reruin/reruin.github.io/blob/master/redirect/onedrive.html">页面中转</a>验证</p>' : ''
return `
<div class="auth">
<h3>OneDrive 挂载向导</h3>
<p style="font-size:12px;">前往 <a href="https://portal.azure.cn/?whr=azure.com#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps" target="_blank" style="font-size:12px;margin-right:5px;color:#337ab7;">Azure管理后台</a> 注册应用获取应用机密 和 应用ID。</p>
<div>
<form class="form-horizontal" method="post">
<input type="hidden" name="act" value="install" />
<input type="hidden" name="redirect_uri" value="${redirect_uri}" />
<div class="form-group"><input class="sl-input" type="text" name="client_secret" value="" placeholder="应用机密 / app_secret" /></div>
<div class="form-group"><input class="sl-input" type="text" name="client_id" value="" placeholder="应用ID / app_id" /></div>
<div class="form-group"><input class="sl-input" type="text" name="tenant" value="" placeholder="组织名 / tenant" /></div>
<button class="sl-button btn-primary" id="signin" type="submit">验证</button></form>
</div>
</div>
`
}
const parseCredentials = ({name,path}) => {
let data = new URL(path)
let credentials = { client_id : data.host }
for (const [key, value] of data.searchParams) {
credentials[key] = value
}
return {
name,
protocol:data.protocol.split(':')[0],
path: decodeURIComponent(data.pathname), //.replace(/^\//,''),
credentials
}
}
const createId = (client_id , path) => {
return urlFormat({
protocol: defaultProtocol,
slashes:true,
hostname: client_id,
pathname: path,
})
}
module.exports = ({ request, cache, getConfig, querystring, base64 , saveDrive , getDrive,getDrives, extname , getRuntime , pathNormalize, chunkStream}) => {
const oauth2 = new oauth2ForOD(request, querystring , async (c) => {
let paths = await getDrives()
let data = paths
.map(i => parseCredentials(i))
//是否有其他配置参数
//let hit = data.filter(i => i.credentials.client_id == c.client_id && i.credentials.client_secret == c.client_secret)
//无配置参数匹配路径名
//if( hit.length == 0 ){
const name = decodeURIComponent(getRuntime('req').path.replace(/^\//g,''))
let hit = data.filter(i => i.name == name)
//}
//路径也无法匹配
if( hit.length == 0 ){
//仅有一个可用挂载源
if(data.length == 1 && paths.length == 1 && paths[0].root){
hit = data
}
}
console.log('update hit',hit)
hit.forEach(i => {
let key = urlFormat({
protocol: i.protocol,
hostname: c.client_id,
pathname: (i.path == '/' || i.path == '' ) ? '/' : i.path,
slashes:true,
query:{
client_secret:c.client_secret,
redirect_uri:c.redirect_uri,
tenant:c.tenant,
refresh_token:c.refresh_token
}
})
saveDrive(key , i.name)
})
})
//获取所有相关根目录,并创建凭证
getDrives().then(resp => {
let credentials = resp.map(i => parseCredentials(i))
oauth2.init(credentials)
})
const getCredentials = async (id) => {
let data = new URL(id)
let ret = { path : decodeURIComponent(data.pathname) }
let client_id = data.host
if (client_id) {
let credentials = await oauth2.getCredentials(client_id)
if(credentials){
ret.credentials = credentials
}
}
return ret
}
const prepare = async (id) => {
if(!id.startsWith(defaultProtocol)){
id = defaultProtocol + ':' + id
}
const req = getRuntime('req')
const baseUrl = req.origin + req.path
let { path, credentials } = await getCredentials(id)
console.log('prepare -->',id)
// 无credentials
if(!credentials){
if (req.body && req.body.act && req.body.act == 'install') {
let { client_id, client_secret, tenant, proxy_url } = req.body
if (client_id && client_secret && tenant) {
console.log('redirect')
return {
id,
type: 'folder',
protocol: defaultProtocol,
redirect: await oauth2.generateAuthUrl({ client_id, client_secret, tenant, redirect_uri: baseUrl })
}
}
}
// 挂载验证回调
if (req.query.code) {
let credentials = await oauth2.getToken(baseUrl, req.query.code)
if (credentials.error) {
return {
id,
type: 'folder',
protocol: defaultProtocol,
body: await error(credentials.msg, baseUrl)
}
} else {
return {
id,
type: 'folder',
protocol: defaultProtocol,
redirect: baseUrl
}
}
}
}
// 存在无credentials
else{
//credentials验证过程出错
if (credentials.error){
return {
id,
type: 'folder',
protocol: defaultProtocol,
body: await error(credentials.msg, baseUrl)
}
}
if (credentials.client_id && credentials.client_secret && credentials.tenant && credentials.redirect_uri) {
if (credentials.refresh_token) {
return { path, credentials }
}
// 缺少 refresh_token 跳转至验证页面
else{
return {
id,
type: 'folder',
protocol: defaultProtocol,
redirect: await oauth2.generateAuthUrl(credentials)
}
}
}
}
//finally 开始挂载向导
return {
id,
type: 'folder',
protocol: defaultProtocol,
body: await install(baseUrl)
}
}
// https://docs.microsoft.com/zh-cn/onedrive/developer/rest-api/resources/driveitem?view=odsp-graph-online
// There are two primary ways of addressing a driveItem resource:
// By the driveItem unique identifier using drive/items/{item-id}
// By file system path using /drive/root:/path/to/file
const folder = async (id) => {
console.log('>>>>id',id)
let predata = await prepare(id)
if (!predata.credentials) return predata
let { path, credentials } = predata
let r = cache.get(id)
if (r) {
if (
r.$cached_at &&
r.children &&
(Date.now() - r.$cached_at < onedrive_max_age_dir)
) {
console.log(Date.now()+' CACHE OneDriveCNAPI '+ id)
return r
}
}
let api = `https://${credentials.tenant}-my.sharepoint.cn/_api/v2.0` + ((!path || path == '/') ? `/me/drive/root/` : `/me/drive/items/root:${encodeURIComponent(path).replace(/\/+$/g,'/')}:/`) + 'children'
let resp = await request.get(api, {
headers: {
'Authorization': `Bearer ${credentials.access_token}`,
'Content-Type': 'application/json'
},
qs: {
select:'id,name,size,file,folder,@microsoft.graph.downloadUrl,thumbnails,createdDateTime,lastModifiedDateTime',
top:999999
},
json: true
})
console.log(resp.body)
if (!resp.body) return false
if(resp.body.error) return {
protocol: defaultProtocol,
type:'folder',
body:'<p style="padding:24px;font-size:12px;">'+resp.body.error.message+'</p>'
}
const ts = Date.now()
let children = resp.body.value.map((i) => {
return {
id: createId(credentials.client_id , (path+'/'+i.name).replace(/\/{2,}/g,'/')),
fid:i.id,
name: i.name,
ext: extname(i.name),
protocol: defaultProtocol,
size: i.size,
created_at: i.createdDateTime,
updated_at: i.lastModifiedDateTime,
url:i['@microsoft.graph.downloadUrl'],
type: i.folder ? 'folder' : 'other',
$cached_at:ts
}
})
let result = { id, path, type: 'folder', protocol: defaultProtocol }
result.$cached_at = Date.now()
result.children = children
cache.set(id, result)
return result
}
const file = async (id, { data = {} } = {}) => {
let predata = await prepare(id)
if (!predata.credentials) return predata
let { path, credentials } = predata
if(
data &&
data.url &&
data.$cached_at &&
( Date.now() - data.$cached_at < onedrive_max_age_dir)
){
console.log('get od download url from upstream')
return data
}
// console.log()
// let api = `https://${credentials.tenant}-my.sharepoint.cn/_api/v2.0/me/drive/root:${encodeURIComponent(path).replace(/\/+$/g,'/')}:/`
let api = `https://${credentials.tenant}-my.sharepoint.cn/_api/v2.0/me/drive/root:${encodeURIComponent(path).replace(/\/+$/g,'/')}:/`
let resp = await request.get(api , {headers:{
'Authorization':`Bearer ${credentials.access_token}`,
'Content-Type': 'application/json'
},json:true})
if(resp.body){
if( resp.body.error ){
return {
error:true,
protocol: defaultProtocol,
body:'<p style="padding:24px;font-size:12px;">'+resp.body.error.message+'</p>'
}
}
data = {
id: id,
fid:resp.body.id,
name: resp.body.name,
protocol: defaultProtocol,
size: resp.body.size,
created_at: resp.body.createdDateTime,
updated_at: resp.body.lastModifiedDateTime,
ext: extname(resp.body.name),
url:resp.body['@content.downloadUrl'],
type: resp.body.folder ? 'folder' : 'other',
}
if(!data.url && resp.body.webUrl){
data.type = 'redirect'
data.redirect = resp.body.webUrl
}
return data
}
return false
}
const mkdir = async (path , target, credentials) => {
let children = target.replace(/(^\/|\/$)/g,'').split('/')
//无需创建
if(children.length <=1 ) return true
//递归创建
for(let i = 0; i < children.length - 1; i++){
let p = path + children.slice(0,i)
let npath = (!p || p == '/') ? '/me/drive/root/children' : `/me/drive/items/root:${encodeURIComponent(p).replace(/\/+$/g,'/')}:/children`
///me/drive/root/children
// https://graph.microsoft.com/v1.0/me/drive/root/children
let api = `https://${credentials.tenant}-my.sharepoint.cn/_api/v2.0` + npath
let resp = {}
try{
resp = await request.post(api,{
"name":children[i],
"folder":{},
"@microsoft.graph.conflictBehavior": "fail"
},{
headers:{
'Authorization':`bearer ${credentials.access_token}`,
},
json:true,
is_body:true
})
}catch(e){
resp = e.body
}
// 409 文件夹已存在
if(resp && resp.error){
console.log('info:',resp.error.message)
//return false
}else{
console.log('mkdir error')
}
}
return true
}
const request_fix = (...rest) => {
let req = request(...rest,function(error, response, body){
// console.log('chunk from',offset)
if(!error) {
this.emit('finish' , body)
}else{
console.log('e',error)
}
})
return req
}
const createChunkStream = ( url , size , offset = 0, chunkSize, retry = 3) => {
let currentChunkSize = ( size - offset < chunkSize ) ? (size - offset) : chunkSize
//console.log('create' , currentChunkSize,`bytes ${offset}-${offset+currentChunkSize-1}/${size}`)
let req = request({
url:url,
method:'put' ,
headers:{
'Content-Length':currentChunkSize,
'Content-Range':`bytes ${offset}-${offset+currentChunkSize-1}/${size}`,
'Content-Type': 'application/json'
},
json:true
},function(error, response, body){
// console.log('chunk from',offset)
if(error) {
console.log(`error bytes(${retry}) ${offset}-${offset+currentChunkSize-1}/${size}` , error)
if(retry > 0){
// return
this.emit('retry' , { retry:retry-1 , offset })
}else{
this.emit('fail' , {error:true , msg:error})
}
}
else {
console.log(`finish bytes ${offset}-${offset+currentChunkSize-1}/${size}`)
if(body.error){
if(retry > 0){
this.emit('retry' , { retry:retry-1 , offset })
}else{
this.emit('fail' , {error:true , msg:JSON.stringify(body.error)})
}
}else{
this.emit('finish' , body)
}
}
})
return req
}
const createRangeStream = ({url , chunkSize , size}) => {
//只读流会通过整理后 进入 req,并在req异常时 重试
let stream = chunkStream(createChunkStream(url , size , 0 , chunkSize) , {chunkSize , size} )
stream.on('update' , (offset, resp) => {
if(resp.error){
return { error: resp.msg }
}else{
// successd return {id , ...}
if(resp.id){
console.log('finish' , resp)
stream.finish(resp)
}else{
stream.next( createChunkStream(url , size , offset , chunkSize) )
}
}
})
stream.on('retry' , ({ retry, offset }) => {
stream.retry( createChunkStream(url , size , offset , chunkSize , retry) )
})
stream.on('error' , (err) => {
console.log('stream said: error',err)
})
stream.on('finish' , (err) => {
console.log('stream said: finish')
})
return stream
}
const upload = async (path , size , credentials) => {
let p = path.split('/')
let name = decodeURIComponent( p.pop() )
let api = `https://${credentials.tenant}-my.sharepoint.cn/_api/v2.0` + ((!path || path == '/') ? `/me/drive/root/` : `/me/drive/items/root:${encodeURIComponent(path).replace(/\/+$/g,'/')}:/`) + 'createUploadSession'
// api = 'https://graph.microsoft.com/v1.0/me/drive/items/root:/Amlogic USB Burning Tool_v2.1.6.8.zip:/createUploadSession'
console.log('createUploadSession', api)
let resp
try{
resp = await request({
url:api,
method:'post',
body:{
"item": {
"@microsoft.graph.conflictBehavior": "rename",
"name":name
}
},
headers:{
'Authorization': `Bearer ${credentials.access_token}`,
'Content-Type': 'application/json'
},
json:true,
async:true
})
}catch(e){
resp = e.body || e
}
if( resp.error ){
console.log('error',resp.error)
return { error : resp.error }
}
if(resp.body && resp.body.uploadUrl){
console.log('Start Upload : '+resp.body.uploadUrl)
// chunkSize = 327680 10485760
return createRangeStream({url : resp.body.uploadUrl , chunkSize:10485760 , size})
}
}
// <= 4194304
const uploadFast = async (path , size , credentials) => {
let api = `https://${credentials.tenant}-my.sharepoint.cn/_api/v2.0` + ((!path || path == '/') ? `/me/drive/root/` : `/me/drive/items/root:${encodeURIComponent(path).replace(/\/+$/g,'/')}:/`) + 'content'
//{"error":{"code":"invalidRequest","message":"The size of the provided stream is not known. Make sure the request is not chunked, and the Content-Length header is specified"}}
let req = request_fix({
url:api ,
method:'put',
headers:{
'Content-Length':size,
'Authorization':`Bearer ${credentials.access_token}`,
}
})
return req
}
const createReadStream = async ({id , options = {}} = {}) => {
let predata = await prepare(id)
if (!predata.credentials) return predata
let { path, credentials } = predata
let api = `https://${credentials.tenant}-my.sharepoint.cn/_api/v2.0/me/drive/root:${encodeURIComponent(path).replace(/\/+$/g,'/')}:/`
let resp = await request.get(api , {headers:{
'Authorization':`bearer ${credentials.access_token}`,
'Content-Type': 'application/json'
},json:true})
if(resp.body){
let downloadUrl = resp.body['@content.downloadUrl']
return request({url:downloadUrl , method:'get'})
}
}
// id 当前有效路径
// target 后续实际路径
const createWriteStream = async ({ id , size , name, target = ''} = {}) => {
let predata = await prepare(id)
if (!predata.credentials) return null
let { path, credentials } = predata
//为path 创建目的地目录
await mkdir(path , target , credentials)
let paths = [filepath,target]
if( type == 'folder' ){
paths.push(name)
}
let fullpath = pathNormalize(paths.join('/'))
if( size !== undefined ){
console.log('clear',id)
cache.clear(id)
if( size <= 4194304 ){
return await uploadFast(fullpath , size , credentials)
}else{
return await upload(fullpath , size , credentials)
}
}
}
return { name, label:'OD 世纪互联', version, drive: { protocols, folder, file , createReadStream , createWriteStream } }
}