@@ -6,7 +6,7 @@ const suffix = '-https://github.com/alist-org/alist'
6
6
const http = async function ( url , options = { } ) {
7
7
if ( options . method == 'POST' && options . data ) {
8
8
options . body = JSON . stringify ( options . data ) ;
9
- options . headers = Object . assign ( { 'content-type' : 'application/json' } , options . headers ) ;
9
+ options . headers = Object . assign ( { 'content-type' : 'application/json' } , options . headers ) ;
10
10
}
11
11
const res = await req ( url , options ) ;
12
12
res . json = ( ) => ( res . data ? res . data : null ) ;
@@ -15,7 +15,7 @@ const http = async function (url, options = {}) {
15
15
} ;
16
16
[ 'get' , 'post' ] . forEach ( ( method ) => {
17
17
http [ method ] = function ( url , options = { } ) {
18
- return http ( url , Object . assign ( options , { method : method . toUpperCase ( ) } ) ) ;
18
+ return http ( url , Object . assign ( options , { method : method . toUpperCase ( ) } ) ) ;
19
19
} ;
20
20
} ) ;
21
21
@@ -26,11 +26,11 @@ async function get_drives_path(tid) {
26
26
const index = tid . indexOf ( '/' , 1 ) ;
27
27
const name = tid . substring ( 1 , index ) ;
28
28
const path = tid . substring ( index ) ;
29
- return { drives : await get_drives ( name ) , path } ;
29
+ return { drives : await get_drives ( name ) , path} ;
30
30
}
31
31
32
32
async function get_drives ( name ) {
33
- const { settings, api, server } = __drives [ name ] ;
33
+ const { settings, api, server} = __drives [ name ] ;
34
34
if ( settings . v3 == null ) {
35
35
//获取 设置
36
36
settings . v3 = false ;
@@ -59,102 +59,102 @@ async function get_drives(name) {
59
59
async function init ( inReq , _outResp ) {
60
60
inReq . server . config . alist . forEach (
61
61
( item ) =>
62
- ( __drives [ item . name ] = {
63
- name : item . name ,
64
- server : item . server . endsWith ( '/' ) ? item . server . substring ( 0 , item . server . length - 1 ) : item . server ,
65
- startPage : item . startPage || '/' , //首页
66
- showAll : item . showAll === true , //默认只显示 视频和文件夹,如果想显示全部 showAll 设置true
67
- login : item . login || { } ,
68
- params : item . params || { } ,
69
- _path_param : item . params
70
- ? Object . keys ( item . params ) . sort ( function ( x , y ) {
71
- return y . length - x . length ;
72
- } )
73
- : [ ] ,
74
- settings : { } ,
75
- api : { } ,
76
- getLogin ( ) {
77
- const pass = CryptoJS . SHA256 ( this . login . password + suffix ) . toString ( CryptoJS . enc . Hex ) ;
78
- const res = {
79
- " username" : this . login . username ,
80
- " password" : pass ,
81
- " otp_code" : this . login . otp_code
82
- }
83
- return res ;
84
- } ,
85
- getParams ( path ) {
86
- const key = this . _path_param . find ( ( x ) => path . startsWith ( x ) ) ;
87
- return Object . assign ( { } , this . params [ key ] , { path } ) ;
88
- } ,
89
- async getHeaders ( ) {
90
- const res = ( await http . post ( this . server + this . api . login , { data : this . getLogin ( ) } ) ) . json ( ) ;
91
- return { "Authorization" : res . data . token } ;
92
- } ,
93
- async getRes ( api , path ) {
94
- let re ;
95
- if ( JSON . stringify ( this . login ) === "{}" ) re = ( await http . post ( this . server + api , { data : this . getParams ( path ) } ) ) . json ( ) ;
96
- else re = ( await http . post ( this . server + api , { data : this . getParams ( path ) , headers : await this . getHeaders ( ) } ) ) . json ( ) ;
97
- return re ;
98
- } ,
99
- async getPath ( path ) {
100
- const res = await this . getRes ( this . api . path , path ) ;
101
- return this . settings . v3 ? res . data . content : res . data . files ;
102
- } ,
103
- async getFile ( path ) {
104
- const res = await this . getRes ( this . api . file , path ) ;
105
- const data = this . settings . v3 ? res . data : res . data . files [ 0 ] ;
106
- if ( ! this . settings . v3 ) data . raw_url = data . url ; //v2 的url和v3不一样
107
- return data ;
108
- } ,
109
- async getOther ( method , path ) {
110
- const data = this . getParams ( path ) ;
111
- data . method = method ;
112
- const res = await this . getRes ( this . api . other , path ) ;
113
- return res ;
114
- } ,
115
- isFolder ( data ) {
116
- return data . type == 1 ;
117
- } ,
118
- isVideo ( data ) {
119
- //判断是否是 视频文件
120
- return this . settings . v3 ? data . type == 2 : data . type == 3 ;
121
- } ,
122
- isSubtitle ( data ) {
123
- if ( data . type == 1 ) return false ;
124
- const ext = [ '.srt' , '.ass' , '.scc' , '.stl' , '.ttml' ] ;
125
- return ext . some ( ( x ) => data . name . endsWith ( x ) ) ;
126
- } ,
127
- getType ( data ) {
128
- const isVideo = this . isVideo ( data ) ;
129
- return this . isFolder ( data ) ? 0 : isVideo ? 10 : 1 ;
130
- } ,
131
- getPic ( data ) {
132
- let pic = this . settings . v3 ? data . thumb : data . thumbnail ;
133
- return pic || ( this . isFolder ( data ) ? 'http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png' : '' ) ;
134
- } ,
135
- getSize ( data ) {
136
- let sz = data . size || 0 ;
137
- if ( sz <= 0 ) return '' ;
138
- let filesize = '' ;
139
- if ( sz > 1024 * 1024 * 1024 * 1024.0 ) {
140
- sz /= 1024 * 1024 * 1024 * 1024.0 ;
141
- filesize = 'TB' ;
142
- } else if ( sz > 1024 * 1024 * 1024.0 ) {
143
- sz /= 1024 * 1024 * 1024.0 ;
144
- filesize = 'GB' ;
145
- } else if ( sz > 1024 * 1024.0 ) {
146
- sz /= 1024 * 1024.0 ;
147
- filesize = 'MB' ;
148
- } else {
149
- sz /= 1024.0 ;
150
- filesize = 'KB' ;
151
- }
152
- return sz . toFixed ( 2 ) + filesize ;
153
- } ,
154
- getRemark ( _data ) {
155
- return '' ;
156
- } ,
157
- } )
62
+ ( __drives [ item . name ] = {
63
+ name : item . name ,
64
+ server : item . server . endsWith ( '/' ) ? item . server . substring ( 0 , item . server . length - 1 ) : item . server ,
65
+ startPage : item . startPage || '/' , //首页
66
+ showAll : item . showAll === true , //默认只显示 视频和文件夹,如果想显示全部 showAll 设置true
67
+ sort : item . sort === true ,
68
+ login : item . login || { } ,
69
+ params : item . params || { } ,
70
+ _path_param : item . params
71
+ ? Object . keys ( item . params ) . sort ( function ( x , y ) {
72
+ return y . length - x . length ;
73
+ } )
74
+ : [ ] ,
75
+ settings : { } ,
76
+ api : { } ,
77
+ getLogin ( ) {
78
+ return {
79
+ username : this . login . username ,
80
+ password : CryptoJS . SHA256 ( this . login . password + suffix ) . toString ( CryptoJS . enc . Hex ) ,
81
+ otp_code : this . login . otp_code
82
+ } ;
83
+ } ,
84
+ getParams ( path ) {
85
+ const key = this . _path_param . find ( ( x ) => path . startsWith ( x ) ) ;
86
+ return Object . assign ( { } , this . params [ key ] , { path} ) ;
87
+ } ,
88
+ async getHeaders ( ) {
89
+ return this . login . username && this . login . username . toLowerCase ( ) !== 'guest'
90
+ ? { Authorization : ( await http . post ( this . server + this . api . login , { data : this . getLogin ( ) } ) ) . json ( ) . data . token }
91
+ : { } ;
92
+ } ,
93
+ async getRes ( api , path ) {
94
+ return ( await http . post ( this . server + api , {
95
+ data : this . getParams ( path ) ,
96
+ headers : Object . keys ( this . login ) . length ? await this . getHeaders ( ) : { }
97
+ } ) ) . json ( ) ;
98
+ } ,
99
+ async getPath ( path ) {
100
+ const res = await this . getRes ( this . api . path , path ) ;
101
+ return this . settings . v3 ? res . data . content : res . data . files ;
102
+ } ,
103
+ async getFile ( path ) {
104
+ const res = await this . getRes ( this . api . file , path ) ;
105
+ const data = this . settings . v3 ? res . data : res . data . files [ 0 ] ;
106
+ if ( ! this . settings . v3 ) data . raw_url = data . url ; //v2 的url和v3不一样
107
+ return data ;
108
+ } ,
109
+ async getOther ( method , path ) {
110
+ const data = this . getParams ( path ) ;
111
+ data . method = method ;
112
+ const res = await this . getRes ( this . api . other , path ) ;
113
+ return res ;
114
+ } ,
115
+ isFolder ( data ) {
116
+ return data . type == 1 ;
117
+ } ,
118
+ isVideo ( data ) {
119
+ //判断是否是 视频文件
120
+ return this . settings . v3 ? data . type == 2 : data . type == 3 ;
121
+ } ,
122
+ isSubtitle ( data ) {
123
+ if ( data . type == 1 ) return false ;
124
+ const ext = [ '.srt' , '.ass' , '.scc' , '.stl' , '.ttml' ] ;
125
+ return ext . some ( ( x ) => data . name . endsWith ( x ) ) ;
126
+ } ,
127
+ getType ( data ) {
128
+ const isVideo = this . isVideo ( data ) ;
129
+ return this . isFolder ( data ) ? 0 : isVideo ? 10 : 1 ;
130
+ } ,
131
+ getPic ( data ) {
132
+ let pic = this . settings . v3 ? data . thumb : data . thumbnail ;
133
+ return pic || ( this . isFolder ( data ) ? 'http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png' : '' ) ;
134
+ } ,
135
+ getSize ( data ) {
136
+ let sz = data . size || 0 ;
137
+ if ( sz <= 0 ) return '' ;
138
+ let filesize = '' ;
139
+ if ( sz > 1024 * 1024 * 1024 * 1024.0 ) {
140
+ sz /= 1024 * 1024 * 1024 * 1024.0 ;
141
+ filesize = 'TB' ;
142
+ } else if ( sz > 1024 * 1024 * 1024.0 ) {
143
+ sz /= 1024 * 1024 * 1024.0 ;
144
+ filesize = 'GB' ;
145
+ } else if ( sz > 1024 * 1024.0 ) {
146
+ sz /= 1024 * 1024.0 ;
147
+ filesize = 'MB' ;
148
+ } else {
149
+ sz /= 1024.0 ;
150
+ filesize = 'KB' ;
151
+ }
152
+ return sz . toFixed ( 2 ) + filesize ;
153
+ } ,
154
+ getRemark ( _data ) {
155
+ return '' ;
156
+ } ,
157
+ } )
158
158
) ;
159
159
// const deviceKey = inReq.server.prefix + '/device';
160
160
// device = await inReq.server.db.getObjectDefault(deviceKey, {});
@@ -178,7 +178,7 @@ async function dir(inReq, _outResp) {
178
178
if ( dir === '/' || dir === '' ) {
179
179
const result = Object . keys ( __drives ) . map ( function ( n ) {
180
180
const d = __drives [ n ] ;
181
- return { name : d . name , path : '/' + d . name + d . startPage , type : 0 , thumb : '' } ;
181
+ return { name : d . name , path : '/' + d . name + d . startPage , type : 0 , thumb : '' } ;
182
182
} ) ;
183
183
return {
184
184
parent : '' ,
@@ -187,13 +187,24 @@ async function dir(inReq, _outResp) {
187
187
list : result ,
188
188
} ;
189
189
}
190
-
191
- let { drives, path } = await get_drives_path ( dir ) ;
190
+ let { drives, path} = await get_drives_path ( dir ) ;
192
191
const id = dir . endsWith ( '/' ) ? dir : dir + '/' ;
193
192
const list = await drives . getPath ( path ) ;
194
193
let subtList = [ ] ;
195
194
let videos = [ ] ;
196
195
let allList = [ ] ;
196
+ if ( drives . sort ) {
197
+ list . sort ( ( a , b ) => {
198
+ const numA = parseInt ( a . name . match ( / ^ \d + / ) || 0 ) ;
199
+ const numB = parseInt ( b . name . match ( / ^ \d + / ) || 0 ) ;
200
+ if ( numA && numB ) {
201
+ return numA - numB ;
202
+ }
203
+ if ( numA ) return - 1 ;
204
+ if ( numB ) return 1 ;
205
+ return a . name . toLowerCase ( ) . localeCompare ( b . name . toLowerCase ( ) ) ;
206
+ } ) ;
207
+ }
197
208
list . forEach ( ( item ) => {
198
209
if ( drives . isSubtitle ( item ) ) subtList . push ( item . name ) ;
199
210
const isVideo = drives . isVideo ( item ) ;
@@ -225,7 +236,7 @@ async function dir(inReq, _outResp) {
225
236
226
237
async function file ( inReq , _outResp ) {
227
238
const file = inReq . body . path ;
228
- let { drives, path } = await get_drives_path ( file ) ;
239
+ let { drives, path} = await get_drives_path ( file ) ;
229
240
const item = await drives . getFile ( path ) ;
230
241
const subs = [ ] ;
231
242
if ( __subtitle_cache [ file ] ) {
@@ -234,7 +245,8 @@ async function file(inReq, _outResp) {
234
245
let subP = await get_drives_path ( sub ) ;
235
246
const subItem = await drives . getFile ( subP . path ) ;
236
247
subs . push ( subItem . raw_url ) ;
237
- } catch ( error ) { }
248
+ } catch ( error ) {
249
+ }
238
250
}
239
251
}
240
252
if ( ( item . provider === 'AliyundriveShare2Open' || item . provider == 'AliyundriveOpen' ) && drives . api . other ) {
@@ -247,7 +259,8 @@ async function file(inReq, _outResp) {
247
259
urls . push ( live . url ) ;
248
260
}
249
261
}
250
- } catch ( error ) { }
262
+ } catch ( error ) {
263
+ }
251
264
const result = {
252
265
name : item . name ,
253
266
url : urls ,
@@ -263,7 +276,8 @@ async function file(inReq, _outResp) {
263
276
let url = item . raw_url ;
264
277
try {
265
278
url = ( await http . get ( url ) ) . json ( ) . data . redirect_url ;
266
- } catch ( error ) { }
279
+ } catch ( error ) {
280
+ }
267
281
const result = {
268
282
name : item . name ,
269
283
url : url ,
@@ -309,15 +323,15 @@ async function test(inReq, outResp) {
309
323
dataResult . dir = resp . json ( ) ;
310
324
printErr ( resp . json ( ) ) ;
311
325
resp = await inReq . server . inject ( ) . post ( `${ prefix } /file` ) . payload ( {
312
- path : '/🐉神族九帝/天翼云盘/音乐/周杰伦 - 七里香.flac ' ,
326
+ path : '/短剧/迟到的正义(39集)/23.mp4 ' ,
313
327
} ) ;
314
328
dataResult . file = resp . json ( ) ;
315
329
printErr ( resp . json ( ) ) ;
316
330
return dataResult ;
317
331
} catch ( err ) {
318
332
console . error ( err ) ;
319
333
outResp . code ( 500 ) ;
320
- return { err : err . message , tip : 'check debug console output' } ;
334
+ return { err : err . message , tip : 'check debug console output' } ;
321
335
}
322
336
}
323
337
0 commit comments