From dbe29a4875b6be291337d5c75f5e7f9523032935 Mon Sep 17 00:00:00 2001 From: MiaoMint <1981324730@qq.com> Date: Thu, 24 Aug 2023 17:37:54 +0800 Subject: [PATCH] Feat: Image, video requests with custom headers --- lib/models/extension.dart | 11 +++++- lib/models/extension.g.dart | 16 ++++++++ lib/pages/detail/view.dart | 3 ++ .../widgets/detail_appbar_flexible_space.dart | 2 + lib/pages/watch/video_controller.dart | 2 +- .../reader/comic/comic_reader_content.dart | 2 + lib/utils/extension_runtime.dart | 39 ++++++++++++++++--- lib/widgets/cache_network_image.dart | 3 ++ 8 files changed, 71 insertions(+), 7 deletions(-) diff --git a/lib/models/extension.dart b/lib/models/extension.dart index c0766f10..19d3d093 100644 --- a/lib/models/extension.dart +++ b/lib/models/extension.dart @@ -55,12 +55,14 @@ class ExtensionListItem { required this.url, required this.cover, this.update, + this.headers, }); final String title; final String url; final String cover; final String? update; + late Map? headers; factory ExtensionListItem.fromJson(Map json) => _$ExtensionListItemFromJson(json); @@ -77,12 +79,14 @@ class ExtensionDetail { required this.cover, this.desc, this.episodes, + this.headers, }); final String title; final String cover; final String? desc; final List? episodes; + late Map? headers; factory ExtensionDetail.fromJson(Map json) => _$ExtensionDetailFromJson(json); @@ -126,10 +130,12 @@ class ExtensionBangumiWatch { required this.type, required this.url, this.subtitles, + this.headers, }); final ExtensionWatchBangumiType type; final String url; final List? subtitles; + late Map? headers; factory ExtensionBangumiWatch.fromJson(Map json) => _$ExtensionBangumiWatchFromJson(json); @@ -156,11 +162,14 @@ class ExtensionBangumiWatchSubtitle { @JsonSerializable() class ExtensionMangaWatch { - final List urls; ExtensionMangaWatch({ required this.urls, + this.headers, }); + final List urls; + late Map? headers; + factory ExtensionMangaWatch.fromJson(Map json) => _$ExtensionMangaWatchFromJson(json); diff --git a/lib/models/extension.g.dart b/lib/models/extension.g.dart index d938c128..69c59644 100644 --- a/lib/models/extension.g.dart +++ b/lib/models/extension.g.dart @@ -48,6 +48,9 @@ ExtensionListItem _$ExtensionListItemFromJson(Map json) => url: json['url'] as String, cover: json['cover'] as String, update: json['update'] as String?, + headers: (json['headers'] as Map?)?.map( + (k, e) => MapEntry(k, e as String), + ), ); Map _$ExtensionListItemToJson(ExtensionListItem instance) => @@ -56,6 +59,7 @@ Map _$ExtensionListItemToJson(ExtensionListItem instance) => 'url': instance.url, 'cover': instance.cover, 'update': instance.update, + 'headers': instance.headers, }; ExtensionDetail _$ExtensionDetailFromJson(Map json) => @@ -67,6 +71,9 @@ ExtensionDetail _$ExtensionDetailFromJson(Map json) => ?.map( (e) => ExtensionEpisodeGroup.fromJson(e as Map)) .toList(), + headers: (json['headers'] as Map?)?.map( + (k, e) => MapEntry(k, e as String), + ), ); Map _$ExtensionDetailToJson(ExtensionDetail instance) => @@ -75,6 +82,7 @@ Map _$ExtensionDetailToJson(ExtensionDetail instance) => 'cover': instance.cover, 'desc': instance.desc, 'episodes': instance.episodes, + 'headers': instance.headers, }; ExtensionEpisodeGroup _$ExtensionEpisodeGroupFromJson( @@ -114,6 +122,9 @@ ExtensionBangumiWatch _$ExtensionBangumiWatchFromJson( ?.map((e) => ExtensionBangumiWatchSubtitle.fromJson(e as Map)) .toList(), + headers: (json['headers'] as Map?)?.map( + (k, e) => MapEntry(k, e as String), + ), ); Map _$ExtensionBangumiWatchToJson( @@ -122,6 +133,7 @@ Map _$ExtensionBangumiWatchToJson( 'type': _$ExtensionWatchBangumiTypeEnumMap[instance.type]!, 'url': instance.url, 'subtitles': instance.subtitles, + 'headers': instance.headers, }; const _$ExtensionWatchBangumiTypeEnumMap = { @@ -148,12 +160,16 @@ Map _$ExtensionBangumiWatchSubtitleToJson( ExtensionMangaWatch _$ExtensionMangaWatchFromJson(Map json) => ExtensionMangaWatch( urls: (json['urls'] as List).map((e) => e as String).toList(), + headers: (json['headers'] as Map?)?.map( + (k, e) => MapEntry(k, e as String), + ), ); Map _$ExtensionMangaWatchToJson( ExtensionMangaWatch instance) => { 'urls': instance.urls, + 'headers': instance.headers, }; ExtensionFikushonWatch _$ExtensionFikushonWatchFromJson( diff --git a/lib/pages/detail/view.dart b/lib/pages/detail/view.dart index 9770dd30..be8370c3 100644 --- a/lib/pages/detail/view.dart +++ b/lib/pages/detail/view.dart @@ -145,6 +145,7 @@ class _DetailPageState extends State { url, width: 50, height: 50, + headers: c.detail?.headers, ), ), title: Text(cast.name), @@ -202,6 +203,7 @@ class _DetailPageState extends State { c.backgorund, width: double.infinity, height: double.infinity, + headers: c.detail?.headers, ), ).blur( begin: const Offset(10, 10), @@ -237,6 +239,7 @@ class _DetailPageState extends State { ), child: CacheNetWorkImage( c.detail?.cover ?? '', + headers: c.detail?.headers, ), ), ), diff --git a/lib/pages/detail/widgets/detail_appbar_flexible_space.dart b/lib/pages/detail/widgets/detail_appbar_flexible_space.dart index 5ffc5730..7b17b33a 100644 --- a/lib/pages/detail/widgets/detail_appbar_flexible_space.dart +++ b/lib/pages/detail/widgets/detail_appbar_flexible_space.dart @@ -58,6 +58,7 @@ class _DetailAppbarflexibleSpaceState extends State { height: double.infinity, fit: BoxFit.cover, width: double.infinity, + headers: c.detail?.headers, ), ), Positioned.fill( @@ -94,6 +95,7 @@ class _DetailAppbarflexibleSpaceState extends State { : CacheNetWorkImage( c.data.value!.cover, fit: BoxFit.cover, + headers: c.detail?.headers, ), ), ), diff --git a/lib/pages/watch/video_controller.dart b/lib/pages/watch/video_controller.dart index 222d0249..5ad39eb2 100644 --- a/lib/pages/watch/video_controller.dart +++ b/lib/pages/watch/video_controller.dart @@ -177,7 +177,7 @@ class VideoPlayerController extends GetxController { selectedSubtitle.value = -1; final playUrl = playList[index.value].url; final watchData = await runtime.watch(playUrl) as ExtensionBangumiWatch; - player.open(Media(watchData.url)); + player.open(Media(watchData.url, httpHeaders: watchData.headers)); subtitles.addAll(watchData.subtitles ?? []); } catch (e) { debugPrint(e.toString()); diff --git a/lib/pages/watch/widgets/reader/comic/comic_reader_content.dart b/lib/pages/watch/widgets/reader/comic/comic_reader_content.dart index d8857006..cd99275a 100644 --- a/lib/pages/watch/widgets/reader/comic/comic_reader_content.dart +++ b/lib/pages/watch/widgets/reader/comic/comic_reader_content.dart @@ -104,6 +104,7 @@ class _ComicReaderContentState extends State { return CacheNetWorkImage( url, fit: BoxFit.fitWidth, + headers: _c.watchData.value?.headers, ); }, itemCount: images.length, @@ -126,6 +127,7 @@ class _ComicReaderContentState extends State { url, key: ValueKey(url), fit: BoxFit.contain, + headers: _c.watchData.value?.headers, ), ); }, diff --git a/lib/utils/extension_runtime.dart b/lib/utils/extension_runtime.dart index fcbdd361..5aa27829 100644 --- a/lib/utils/extension_runtime.dart +++ b/lib/utils/extension_runtime.dart @@ -17,6 +17,7 @@ class ExtensionRuntime { late Extension extension; late PersistCookieJar _cookieJar; final _dio = Dio(); + String _cuurentRequestUrl = ''; initRuntime(Extension ext) async { extension = ext; @@ -54,6 +55,7 @@ class ExtensionRuntime { ExtensionLogLevel.info, "GET: ${args[0]} , ${args[1]}", ); + _cuurentRequestUrl = args[0]; return (await _dio.get(args[0], options: Options( headers: args[1]['headers'] ?? {}, @@ -388,9 +390,16 @@ class ExtensionRuntime { final jsResult = await runtime.handlePromise( await runtime.evaluateAsync('stringify(()=>extenstion.latest($page))'), ); - return jsonDecode(jsResult.stringResult).map((e) { + List result = + jsonDecode(jsResult.stringResult).map((e) { return ExtensionListItem.fromJson(e); }).toList(); + for (var element in result) { + element.headers ??= { + "Referer": _cuurentRequestUrl, + }; + } + return result; }); } @@ -400,9 +409,16 @@ class ExtensionRuntime { await runtime .evaluateAsync('stringify(()=>extenstion.search("$kw",$page))'), ); - return jsonDecode(jsResult.stringResult).map((e) { + List result = + jsonDecode(jsResult.stringResult).map((e) { return ExtensionListItem.fromJson(e); }).toList(); + for (var element in result) { + element.headers ??= { + "Referer": _cuurentRequestUrl, + }; + } + return result; }); } @@ -411,7 +427,12 @@ class ExtensionRuntime { final jsResult = await runtime.handlePromise( await runtime.evaluateAsync('stringify(()=>extenstion.detail("$url"))'), ); - return ExtensionDetail.fromJson(jsonDecode(jsResult.stringResult)); + final result = + ExtensionDetail.fromJson(jsonDecode(jsResult.stringResult)); + result.headers ??= { + "Referer": _cuurentRequestUrl, + }; + return result; }); } @@ -424,9 +445,17 @@ class ExtensionRuntime { switch (extension.type) { case ExtensionType.bangumi: - return ExtensionBangumiWatch.fromJson(data); + final result = ExtensionBangumiWatch.fromJson(data); + result.headers ??= { + "Referer": _cuurentRequestUrl, + }; + return result; case ExtensionType.manga: - return ExtensionMangaWatch.fromJson(data); + final result = ExtensionMangaWatch.fromJson(data); + result.headers ??= { + "Referer": _cuurentRequestUrl, + }; + return result; default: return ExtensionFikushonWatch.fromJson(data); } diff --git a/lib/widgets/cache_network_image.dart b/lib/widgets/cache_network_image.dart index 4ad2ea86..c31d09a2 100644 --- a/lib/widgets/cache_network_image.dart +++ b/lib/widgets/cache_network_image.dart @@ -10,12 +10,14 @@ class CacheNetWorkImage extends StatelessWidget { this.width, this.height, this.fallback, + this.headers, }) : super(key: key); final String url; final BoxFit fit; final double? width; final double? height; final Widget? fallback; + final Map? headers; _errorBuild() { if (fallback != null) { @@ -28,6 +30,7 @@ class CacheNetWorkImage extends StatelessWidget { Widget build(BuildContext context) { return CachedNetworkImage( imageUrl: url, + httpHeaders: headers, fit: fit, width: width, height: height,