Skip to content

Commit

Permalink
Feat: Image, video requests with custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint committed Aug 24, 2023
1 parent 80840a9 commit dbe29a4
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 7 deletions.
11 changes: 10 additions & 1 deletion lib/models/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>? headers;

factory ExtensionListItem.fromJson(Map<String, dynamic> json) =>
_$ExtensionListItemFromJson(json);
Expand All @@ -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<ExtensionEpisodeGroup>? episodes;
late Map<String, String>? headers;

factory ExtensionDetail.fromJson(Map<String, dynamic> json) =>
_$ExtensionDetailFromJson(json);
Expand Down Expand Up @@ -126,10 +130,12 @@ class ExtensionBangumiWatch {
required this.type,
required this.url,
this.subtitles,
this.headers,
});
final ExtensionWatchBangumiType type;
final String url;
final List<ExtensionBangumiWatchSubtitle>? subtitles;
late Map<String, String>? headers;

factory ExtensionBangumiWatch.fromJson(Map<String, dynamic> json) =>
_$ExtensionBangumiWatchFromJson(json);
Expand All @@ -156,11 +162,14 @@ class ExtensionBangumiWatchSubtitle {

@JsonSerializable()
class ExtensionMangaWatch {
final List<String> urls;
ExtensionMangaWatch({
required this.urls,
this.headers,
});

final List<String> urls;
late Map<String, String>? headers;

factory ExtensionMangaWatch.fromJson(Map<String, dynamic> json) =>
_$ExtensionMangaWatchFromJson(json);

Expand Down
16 changes: 16 additions & 0 deletions lib/models/extension.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/pages/detail/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class _DetailPageState extends State<DetailPage> {
url,
width: 50,
height: 50,
headers: c.detail?.headers,
),
),
title: Text(cast.name),
Expand Down Expand Up @@ -202,6 +203,7 @@ class _DetailPageState extends State<DetailPage> {
c.backgorund,
width: double.infinity,
height: double.infinity,
headers: c.detail?.headers,
),
).blur(
begin: const Offset(10, 10),
Expand Down Expand Up @@ -237,6 +239,7 @@ class _DetailPageState extends State<DetailPage> {
),
child: CacheNetWorkImage(
c.detail?.cover ?? '',
headers: c.detail?.headers,
),
),
),
Expand Down
2 changes: 2 additions & 0 deletions lib/pages/detail/widgets/detail_appbar_flexible_space.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class _DetailAppbarflexibleSpaceState extends State<DetailAppbarflexibleSpace> {
height: double.infinity,
fit: BoxFit.cover,
width: double.infinity,
headers: c.detail?.headers,
),
),
Positioned.fill(
Expand Down Expand Up @@ -94,6 +95,7 @@ class _DetailAppbarflexibleSpaceState extends State<DetailAppbarflexibleSpace> {
: CacheNetWorkImage(
c.data.value!.cover,
fit: BoxFit.cover,
headers: c.detail?.headers,
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/watch/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class _ComicReaderContentState extends State<ComicReaderContent> {
return CacheNetWorkImage(
url,
fit: BoxFit.fitWidth,
headers: _c.watchData.value?.headers,
);
},
itemCount: images.length,
Expand All @@ -126,6 +127,7 @@ class _ComicReaderContentState extends State<ComicReaderContent> {
url,
key: ValueKey(url),
fit: BoxFit.contain,
headers: _c.watchData.value?.headers,
),
);
},
Expand Down
39 changes: 34 additions & 5 deletions lib/utils/extension_runtime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ExtensionRuntime {
late Extension extension;
late PersistCookieJar _cookieJar;
final _dio = Dio();
String _cuurentRequestUrl = '';

initRuntime(Extension ext) async {
extension = ext;
Expand Down Expand Up @@ -54,6 +55,7 @@ class ExtensionRuntime {
ExtensionLogLevel.info,
"GET: ${args[0]} , ${args[1]}",
);
_cuurentRequestUrl = args[0];
return (await _dio.get<String>(args[0],
options: Options(
headers: args[1]['headers'] ?? {},
Expand Down Expand Up @@ -388,9 +390,16 @@ class ExtensionRuntime {
final jsResult = await runtime.handlePromise(
await runtime.evaluateAsync('stringify(()=>extenstion.latest($page))'),
);
return jsonDecode(jsResult.stringResult).map<ExtensionListItem>((e) {
List<ExtensionListItem> result =
jsonDecode(jsResult.stringResult).map<ExtensionListItem>((e) {
return ExtensionListItem.fromJson(e);
}).toList();
for (var element in result) {
element.headers ??= {
"Referer": _cuurentRequestUrl,
};
}
return result;
});
}

Expand All @@ -400,9 +409,16 @@ class ExtensionRuntime {
await runtime
.evaluateAsync('stringify(()=>extenstion.search("$kw",$page))'),
);
return jsonDecode(jsResult.stringResult).map<ExtensionListItem>((e) {
List<ExtensionListItem> result =
jsonDecode(jsResult.stringResult).map<ExtensionListItem>((e) {
return ExtensionListItem.fromJson(e);
}).toList();
for (var element in result) {
element.headers ??= {
"Referer": _cuurentRequestUrl,
};
}
return result;
});
}

Expand All @@ -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;
});
}

Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/widgets/cache_network_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>? headers;

_errorBuild() {
if (fallback != null) {
Expand All @@ -28,6 +30,7 @@ class CacheNetWorkImage extends StatelessWidget {
Widget build(BuildContext context) {
return CachedNetworkImage(
imageUrl: url,
httpHeaders: headers,
fit: fit,
width: width,
height: height,
Expand Down

0 comments on commit dbe29a4

Please sign in to comment.