1- import path from 'path' ;
1+ import path , { parse } from 'path' ;
22import { existsSync , watch } from 'fs' ;
33import { base64Decode } from '../libs_drpy/crypto-util.js' ;
44import * as drpy from '../libs/drpyS.js' ;
5+ import * as drpy2 from '../libs/dr2Adapter.js' ;
6+ import * as pyadapter from '../libs/pyAdapter.js' ;
57import { ENV } from "../utils/env.js" ;
68import { validatePwd } from "../utils/api_validate.js" ;
79
@@ -50,22 +52,49 @@ export default (fastify, options, done) => {
5052 } ,
5153 handler : async ( request , reply ) => {
5254 const moduleName = request . params . module ;
53- const modulePath = path . join ( options . jsDir , `${ moduleName } .js` ) ;
54- if ( ! existsSync ( modulePath ) ) {
55- reply . status ( 404 ) . send ( { error : `Module ${ moduleName } not found` } ) ;
56- return ;
57- }
5855 const method = request . method . toUpperCase ( ) ;
5956 // 根据请求方法选择参数来源
6057 const query = method === 'GET' ? request . query : request . body ;
58+ let parseEngine ;
59+ let _ext ;
60+ let moduleDir ;
61+ // 根据查询参数选择解析引擎和脚本路径(adpt=ds || dr || py)
62+ const adpt = query . adpt ;
63+ switch ( adpt ) {
64+ case 'ds' :
65+ parseEngine = drpy ;
66+ moduleDir = options . jsDir ;
67+ _ext = '.js' ;
68+ break ;
69+ case 'dr' :
70+ parseEngine = drpy2 ;
71+ moduleDir = options . dr2Dir ;
72+ _ext = '.js' ;
73+ break ;
74+ case 'py' :
75+ parseEngine = pyadapter ;
76+ moduleDir = options . pyDir ;
77+ _ext = '.py' ;
78+ break ;
79+ default :
80+ parseEngine = drpy ;
81+ moduleDir = options . jsDir ;
82+ _ext = '.js' ;
83+ break ;
84+ }
85+ const modulePath = path . join ( moduleDir , `${ moduleName } ${ _ext } ` ) ;
86+ if ( ! existsSync ( modulePath ) ) {
87+ reply . status ( 404 ) . send ( { error : `Module ${ moduleName } not found` } ) ;
88+ return ;
89+ }
90+
6191 const moduleExt = query . extend || '' ;
6292 // console.log('moduleExt:', typeof moduleExt, moduleExt);
6393 const protocol = request . protocol ;
6494 const hostname = request . hostname ;
6595 // const proxyUrl = `${protocol}://${hostname}${request.url}`.split('?')[0].replace('/api/', '/proxy/') + '/?do=js';
6696 // const proxyUrl = `${protocol}://${hostname}/proxy/${moduleName}/?do=js`;
6797 // console.log('proxyUrl:', proxyUrl);
68-
6998 const publicUrl = `${ protocol } ://${ hostname } /public/` ;
7099 const jsonUrl = `${ protocol } ://${ hostname } /json/` ;
71100 const httpUrl = `${ protocol } ://${ hostname } /http` ;
@@ -95,12 +124,12 @@ export default (fastify, options, done) => {
95124
96125 const env = getEnv ( moduleName ) ;
97126 env . getRule = async function ( _moduleName ) {
98- const _modulePath = path . join ( options . jsDir , `${ _moduleName } .js ` ) ;
127+ const _modulePath = path . join ( moduleDir , `${ _moduleName } ${ _ext } ` ) ;
99128 if ( ! existsSync ( _modulePath ) ) {
100129 return null ;
101130 }
102131 const _env = getEnv ( _moduleName ) ;
103- const RULE = await drpy . getRule ( _modulePath , _env ) ;
132+ const RULE = await parseEngine . getRule ( _modulePath , _env ) ;
104133 RULE . callRuleFn = async function ( _method , _args ) {
105134 let invokeMethod = null ;
106135 switch ( _method ) {
@@ -136,7 +165,7 @@ export default (fastify, options, done) => {
136165 return await RULE [ _method ]
137166 }
138167 }
139- return await drpy [ invokeMethod ] ( _modulePath , _env , ..._args )
168+ return await parseEngine [ invokeMethod ] ( _modulePath , _env , ..._args )
140169 } ;
141170 return RULE
142171 } ;
@@ -146,7 +175,7 @@ export default (fastify, options, done) => {
146175 if ( 'play' in query ) {
147176 // 处理播放逻辑
148177 // console.log('play query:', query);
149- const result = await drpy . play ( modulePath , env , query . flag , query . play ) ;
178+ const result = await parseEngine . play ( modulePath , env , query . flag , query . play ) ;
150179 return reply . send ( result ) ;
151180 }
152181
@@ -162,7 +191,7 @@ export default (fastify, options, done) => {
162191 }
163192 }
164193 // 分类逻辑
165- const result = await drpy . cate ( modulePath , env , query . t , pg , 1 , extend ) ;
194+ const result = await parseEngine . cate ( modulePath , env , query . t , pg , 1 , extend ) ;
166195 return reply . send ( result ) ;
167196 }
168197
@@ -171,36 +200,36 @@ export default (fastify, options, done) => {
171200 fastify . log . info ( `[${ moduleName } ] 二级已接收post数据: ${ query . ids } ` ) ;
172201 }
173202 // 详情逻辑
174- const result = await drpy . detail ( modulePath , env , query . ids . split ( ',' ) ) ;
203+ const result = await parseEngine . detail ( modulePath , env , query . ids . split ( ',' ) ) ;
175204 return reply . send ( result ) ;
176205 }
177206
178207 if ( 'ac' in query && 'action' in query ) {
179208 // 处理动作逻辑
180- const result = await drpy . action ( modulePath , env , query . action , query . value ) ;
209+ const result = await parseEngine . action ( modulePath , env , query . action , query . value ) ;
181210 return reply . send ( result ) ;
182211 }
183212
184213
185214 if ( 'wd' in query ) {
186215 // 搜索逻辑
187216 const quick = 'quick' in query ? query . quick : 0 ;
188- const result = await drpy . search ( modulePath , env , query . wd , quick , pg ) ;
217+ const result = await parseEngine . search ( modulePath , env , query . wd , quick , pg ) ;
189218 return reply . send ( result ) ;
190219 }
191220
192221 if ( 'refresh' in query ) {
193222 // 强制刷新初始化逻辑
194- const refreshedObject = await drpy . init ( modulePath , env , true ) ;
223+ const refreshedObject = await parseEngine . init ( modulePath , env , true ) ;
195224 return reply . send ( refreshedObject ) ;
196225 }
197226 if ( ! ( 'filter' in query ) ) {
198227 query . filter = 1
199228 }
200229 // 默认逻辑,返回 home + homeVod 接口
201230 const filter = 'filter' in query ? query . filter : 1 ;
202- const resultHome = await drpy . home ( modulePath , env , filter ) ;
203- const resultHomeVod = await drpy . homeVod ( modulePath , env ) ;
231+ const resultHome = await parseEngine . home ( modulePath , env , filter ) ;
232+ const resultHomeVod = await parseEngine . homeVod ( modulePath , env ) ;
204233 let result = {
205234 ...resultHome ,
206235 // list: resultHomeVod,
@@ -266,7 +295,7 @@ export default (fastify, options, done) => {
266295
267296 const env = getEnv ( moduleName ) ;
268297 try {
269- const backRespList = await drpy . proxy ( modulePath , env , query ) ;
298+ const backRespList = await parseEngine . proxy ( modulePath , env , query ) ;
270299 const statusCode = backRespList [ 0 ] ;
271300 const mediaType = backRespList [ 1 ] || 'application/octet-stream' ;
272301 let content = backRespList [ 2 ] || '' ;
0 commit comments