pluginManager.ts (b50427a2113c3b0d9efb6cb4e9cb65a118ae9bda) | pluginManager.ts (cfa0fc0757dad620cd0b0533a949d86b17086d32) |
---|---|
1import { 2 copyFile, 3 exists, 4 readDir, 5 readFile, 6 unlink, 7 writeFile, 8} from 'react-native-fs'; --- 5 unchanged lines hidden (view full) --- 14import {satisfies} from 'compare-versions'; 15import DeviceInfo from 'react-native-device-info'; 16import StateMapper from '@/utils/stateMapper'; 17import MediaMeta from './mediaMeta'; 18import {nanoid} from 'nanoid'; 19import {errorLog, trace} from '../utils/log'; 20import Cache from './cache'; 21import {isSameMediaItem, resetMediaItem} from '@/utils/mediaItem'; | 1import { 2 copyFile, 3 exists, 4 readDir, 5 readFile, 6 unlink, 7 writeFile, 8} from 'react-native-fs'; --- 5 unchanged lines hidden (view full) --- 14import {satisfies} from 'compare-versions'; 15import DeviceInfo from 'react-native-device-info'; 16import StateMapper from '@/utils/stateMapper'; 17import MediaMeta from './mediaMeta'; 18import {nanoid} from 'nanoid'; 19import {errorLog, trace} from '../utils/log'; 20import Cache from './cache'; 21import {isSameMediaItem, resetMediaItem} from '@/utils/mediaItem'; |
22import {internalSerialzeKey, internalSymbolKey} from '@/constants/commonConst'; | 22import { 23 CacheControl, 24 internalSerialzeKey, 25 internalSymbolKey, 26} from '@/constants/commonConst'; |
23import Download from './download'; 24import delay from '@/utils/delay'; 25import * as cheerio from 'cheerio'; 26 27axios.defaults.timeout = 1500; 28 29const sha256 = CryptoJs.SHA256; 30 | 27import Download from './download'; 28import delay from '@/utils/delay'; 29import * as cheerio from 'cheerio'; 30 31axios.defaults.timeout = 1500; 32 33const sha256 = CryptoJs.SHA256; 34 |
31enum PluginStateCode { | 35export enum PluginStateCode { |
32 /** 版本不匹配 */ 33 VersionNotMatch = 'VERSION NOT MATCH', 34 /** 无法解析 */ 35 CannotParse = 'CANNOT PARSE', 36} 37 38export class Plugin { 39 /** 插件名 */ --- 128 unchanged lines hidden (view full) --- 168 Download.getDownloaded(musicItem)?.[internalSymbolKey]?.localPath; 169 if (localPath && (await exists(localPath))) { 170 trace('播放', '本地播放'); 171 return { 172 url: localPath, 173 }; 174 } 175 // 2. 缓存播放 | 36 /** 版本不匹配 */ 37 VersionNotMatch = 'VERSION NOT MATCH', 38 /** 无法解析 */ 39 CannotParse = 'CANNOT PARSE', 40} 41 42export class Plugin { 43 /** 插件名 */ --- 128 unchanged lines hidden (view full) --- 172 Download.getDownloaded(musicItem)?.[internalSymbolKey]?.localPath; 173 if (localPath && (await exists(localPath))) { 174 trace('播放', '本地播放'); 175 return { 176 url: localPath, 177 }; 178 } 179 // 2. 缓存播放 |
180 // todo: 无网络情况下强制使用缓存播放 no-cache: 无网络情况下使用cache |
|
176 const mediaCache = Cache.get(musicItem); | 181 const mediaCache = Cache.get(musicItem); |
177 if (mediaCache && mediaCache?.url) { | 182 if ( 183 mediaCache && 184 mediaCache?.url && 185 mediaCache.cache === CacheControl.Cache 186 ) { |
178 trace('播放', '缓存播放'); 179 return { 180 url: mediaCache.url, 181 headers: mediaCache.headers, 182 userAgent: 183 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 184 }; 185 } 186 // 3. 插件解析 187 if (!this.plugin.instance.getMediaSource) { 188 return {url: musicItem.url}; 189 } 190 try { | 187 trace('播放', '缓存播放'); 188 return { 189 url: mediaCache.url, 190 headers: mediaCache.headers, 191 userAgent: 192 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 193 }; 194 } 195 // 3. 插件解析 196 if (!this.plugin.instance.getMediaSource) { 197 return {url: musicItem.url}; 198 } 199 try { |
191 const {url, headers, cache} = | 200 const {url, headers, cacheControl} = |
192 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 193 if (!url) { 194 throw new Error(); 195 } 196 trace('播放', '插件播放'); 197 const result = { 198 url, 199 headers, 200 userAgent: headers?.['user-agent'], | 201 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 202 if (!url) { 203 throw new Error(); 204 } 205 trace('播放', '插件播放'); 206 const result = { 207 url, 208 headers, 209 userAgent: headers?.['user-agent'], |
201 }; | 210 cacheControl: cacheControl ?? CacheControl.Cache, 211 } as IPlugin.IMediaSourceResult; |
202 | 212 |
203 if (cache !== false) { | 213 if (cacheControl !== CacheControl.NoStore) { |
204 Cache.update(musicItem, result); 205 } | 214 Cache.update(musicItem, result); 215 } |
216 |
|
206 return result; 207 } catch (e: any) { 208 if (retryCount > 0) { 209 await delay(150); 210 return this.getMediaSource(musicItem, --retryCount); 211 } 212 errorLog('获取真实源失败', e?.message); 213 throw e; --- 248 unchanged lines hidden (view full) --- 462 ); 463 errorLog('插件初始化失败', e?.message); 464 throw e; 465 } 466} 467 468// 安装插件 469async function installPlugin(pluginPath: string) { | 217 return result; 218 } catch (e: any) { 219 if (retryCount > 0) { 220 await delay(150); 221 return this.getMediaSource(musicItem, --retryCount); 222 } 223 errorLog('获取真实源失败', e?.message); 224 throw e; --- 248 unchanged lines hidden (view full) --- 473 ); 474 errorLog('插件初始化失败', e?.message); 475 throw e; 476 } 477} 478 479// 安装插件 480async function installPlugin(pluginPath: string) { |
470 let checkPath = decodeURIComponent(pluginPath); 471 trace(checkPath, await exists(checkPath)); 472 trace(pluginPath, await exists(pluginPath)); 473 trace(pluginPath.substring(7), await exists(pluginPath.substring(7))); 474 trace(checkPath.substring(7), await exists(checkPath.substring(7))); | 481 // let checkPath = decodeURIComponent(pluginPath); 482 // trace(checkPath, await exists(checkPath)); 483 // trace(pluginPath, await exists(pluginPath)); 484 // trace(pluginPath.substring(7), await exists(pluginPath.substring(7))); 485 // trace(checkPath.substring(7), await exists(checkPath.substring(7))); |
475 if (pluginPath.endsWith('.js')) { 476 const funcCode = await readFile(pluginPath, 'utf8'); 477 const plugin = new Plugin(funcCode, pluginPath); 478 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 479 if (_pluginIndex !== -1) { 480 throw new Error('插件已安装'); 481 } 482 if (plugin.hash !== '') { --- 53 unchanged lines hidden --- | 486 if (pluginPath.endsWith('.js')) { 487 const funcCode = await readFile(pluginPath, 'utf8'); 488 const plugin = new Plugin(funcCode, pluginPath); 489 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 490 if (_pluginIndex !== -1) { 491 throw new Error('插件已安装'); 492 } 493 if (plugin.hash !== '') { --- 53 unchanged lines hidden --- |