11import req from '../../util/req.js' ;
2+ import CryptoJS from 'crypto-js' ;
3+
4+ const suffix = '-https://github.com/alist-org/alist'
25
36const http = async function ( url , options = { } ) {
47 if ( options . method == 'POST' && options . data ) {
@@ -44,6 +47,7 @@ async function get_drives(name) {
4447 settings . enableSearch = false ; //v3 没有找到 搜索配置
4548 }
4649 //不同版本 接口不一样
50+ api . login = settings . v3 ? '/api/auth/login/hash' : '/api/auth/login/hash' ;
4751 api . path = settings . v3 ? '/api/fs/list' : '/api/public/path' ;
4852 api . file = settings . v3 ? '/api/fs/get' : '/api/public/path' ;
4953 api . search = settings . v3 ? '/api/public/search' : '/api/public/search' ;
@@ -55,82 +59,102 @@ async function get_drives(name) {
5559async function init ( inReq , _outResp ) {
5660 inReq . server . config . alist . forEach (
5761 ( item ) =>
58- ( __drives [ item . name ] = {
59- name : item . name ,
60- server : item . server . endsWith ( '/' ) ? item . server . substring ( 0 , item . server . length - 1 ) : item . server ,
61- startPage : item . startPage || '/' , //首页
62- showAll : item . showAll === true , //默认只显示 视频和文件夹,如果想显示全部 showAll 设置true
63- params : item . params || { } ,
64- _path_param : item . params
65- ? Object . keys ( item . params ) . sort ( function ( x , y ) {
66- return y . length - x . length ;
67- } )
68- : [ ] ,
69- settings : { } ,
70- api : { } ,
71- getParams ( path ) {
72- const key = this . _path_param . find ( ( x ) => path . startsWith ( x ) ) ;
73- return Object . assign ( { } , this . params [ key ] , { path } ) ;
74- } ,
75- async getPath ( path ) {
76- const res = ( await http . post ( this . server + this . api . path , { data : this . getParams ( path ) } ) ) . json ( ) ;
77- return this . settings . v3 ? res . data . content : res . data . files ;
78- } ,
79- async getFile ( path ) {
80- const res = ( await http . post ( this . server + this . api . file , { data : this . getParams ( path ) } ) ) . json ( ) ;
81- const data = this . settings . v3 ? res . data : res . data . files [ 0 ] ;
82- if ( ! this . settings . v3 ) data . raw_url = data . url ; //v2 的url和v3不一样
83- return data ;
84- } ,
85- async getOther ( method , path ) {
86- const data = this . getParams ( path ) ;
87- data . method = method ;
88- const res = ( await http . post ( this . server + this . api . other , { data : data } ) ) . json ( ) ;
89- return res ;
90- } ,
91- isFolder ( data ) {
92- return data . type == 1 ;
93- } ,
94- isVideo ( data ) {
95- //判断是否是 视频文件
96- return this . settings . v3 ? data . type == 2 : data . type == 3 ;
97- } ,
98- isSubtitle ( data ) {
99- if ( data . type == 1 ) return false ;
100- const ext = [ '.srt' , '.ass' , '.scc' , '.stl' , '.ttml' ] ;
101- return ext . some ( ( x ) => data . name . endsWith ( x ) ) ;
102- } ,
103- getType ( data ) {
104- const isVideo = this . isVideo ( data ) ;
105- return this . isFolder ( data ) ? 0 : isVideo ? 10 : 1 ;
106- } ,
107- getPic ( data ) {
108- let pic = this . settings . v3 ? data . thumb : data . thumbnail ;
109- return pic || ( this . isFolder ( data ) ? 'http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png' : '' ) ;
110- } ,
111- getSize ( data ) {
112- let sz = data . size || 0 ;
113- if ( sz <= 0 ) return '' ;
114- let filesize = '' ;
115- if ( sz > 1024 * 1024 * 1024 * 1024.0 ) {
116- sz /= 1024 * 1024 * 1024 * 1024.0 ;
117- filesize = 'TB' ;
118- } else if ( sz > 1024 * 1024 * 1024.0 ) {
119- sz /= 1024 * 1024 * 1024.0 ;
120- filesize = 'GB' ;
121- } else if ( sz > 1024 * 1024.0 ) {
122- sz /= 1024 * 1024.0 ;
123- filesize = 'MB' ;
124- } else {
125- sz /= 1024.0 ;
126- filesize = 'KB' ;
127- }
128- return sz . toFixed ( 2 ) + filesize ;
129- } ,
130- getRemark ( _data ) {
131- return '' ;
132- } ,
133- } )
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+ } )
134158 ) ;
135159 // const deviceKey = inReq.server.prefix + '/device';
136160 // device = await inReq.server.db.getObjectDefault(deviceKey, {});
@@ -210,7 +234,7 @@ async function file(inReq, _outResp) {
210234 let subP = await get_drives_path ( sub ) ;
211235 const subItem = await drives . getFile ( subP . path ) ;
212236 subs . push ( subItem . raw_url ) ;
213- } catch ( error ) { }
237+ } catch ( error ) { }
214238 }
215239 }
216240 if ( ( item . provider === 'AliyundriveShare2Open' || item . provider == 'AliyundriveOpen' ) && drives . api . other ) {
@@ -223,7 +247,7 @@ async function file(inReq, _outResp) {
223247 urls . push ( live . url ) ;
224248 }
225249 }
226- } catch ( error ) { }
250+ } catch ( error ) { }
227251 const result = {
228252 name : item . name ,
229253 url : urls ,
@@ -239,7 +263,7 @@ async function file(inReq, _outResp) {
239263 let url = item . raw_url ;
240264 try {
241265 url = ( await http . get ( url ) ) . json ( ) . data . redirect_url ;
242- } catch ( error ) { }
266+ } catch ( error ) { }
243267 const result = {
244268 name : item . name ,
245269 url : url ,
0 commit comments