Skip to content

Commit b9740e2

Browse files
committed
fix: proxying image request properly
1 parent 9057658 commit b9740e2

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

lib/page/forum/image_viewer.dart

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'dart:io';
1919

2020
import 'package:cached_network_image/cached_network_image.dart';
2121
import 'package:dan_xi/generated/l10n.dart';
22+
import 'package:dan_xi/util/io/cache_manager_with_proxy.dart';
2223
import 'package:dan_xi/util/io/dio_utils.dart';
2324
import 'package:dan_xi/util/noticing.dart';
2425
import 'package:dan_xi/util/platform_universal.dart';
@@ -132,8 +133,8 @@ class ImageViewerPageState extends State<ImageViewerPage> {
132133
}
133134

134135
Future<void> shareImage(BuildContext context) async {
135-
File image =
136-
await DefaultCacheManager().getSingleFile(_imageList[showIndex].hdUrl);
136+
File image = await DefaultCacheManagerWithProxy()
137+
.getSingleFile(_imageList[showIndex].hdUrl);
137138
if (!mounted) return;
138139

139140
if (PlatformX.isMobile) {
@@ -159,8 +160,8 @@ class ImageViewerPageState extends State<ImageViewerPage> {
159160
}
160161

161162
Future<void> saveImage(BuildContext context) async {
162-
File image =
163-
await DefaultCacheManager().getSingleFile(_imageList[showIndex].hdUrl);
163+
File image = await DefaultCacheManagerWithProxy()
164+
.getSingleFile(_imageList[showIndex].hdUrl);
164165
if (PlatformX.isAndroid) {
165166
bool hasPermission = await PlatformX.galleryStorageGranted;
166167
if (!hasPermission && !(await Permission.storage.request().isGranted)) {
@@ -309,7 +310,8 @@ class ImageViewerBodyViewState extends State<ImageViewerBodyView> {
309310
Future<void> cacheOriginalImage() async {
310311
if (widget.imageInfo.thumbUrl == null) return;
311312
try {
312-
await DefaultCacheManager().getSingleFile(widget.imageInfo.hdUrl);
313+
await DefaultCacheManagerWithProxy()
314+
.getSingleFile(widget.imageInfo.hdUrl);
313315
setState(() => originalLoading = false);
314316
} catch (e, st) {
315317
setState(() {

lib/page/subpage_settings.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import 'package:dan_xi/repository/forum/forum_repository.dart';
3232
import 'package:dan_xi/util/browser_util.dart';
3333
import 'package:dan_xi/util/flutter_app.dart';
3434
import 'package:dan_xi/util/forum/clean_mode_filter.dart';
35+
import 'package:dan_xi/util/io/cache_manager_with_proxy.dart';
3536
import 'package:dan_xi/util/master_detail_view.dart';
3637
import 'package:dan_xi/util/noticing.dart';
3738
import 'package:dan_xi/util/platform_universal.dart';
@@ -51,7 +52,6 @@ import 'package:flutter/cupertino.dart';
5152
import 'package:flutter/gestures.dart';
5253
import 'package:flutter/material.dart';
5354
import 'package:flutter/services.dart';
54-
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
5555
import 'package:flutter_email_sender/flutter_email_sender.dart';
5656
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
5757
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
@@ -797,7 +797,7 @@ class SettingsSubpageState extends PlatformSubpageState<SettingsSubpage> {
797797
subtitle: Text(_clearCacheSubtitle ??
798798
S.of(context).clear_cache_description),
799799
onTap: () async {
800-
await DefaultCacheManager().emptyCache();
800+
await DefaultCacheManagerWithProxy().emptyCache();
801801
setState(() {
802802
_clearCacheSubtitle = S.of(context).cache_cleared;
803803
});
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2024 DanXi-Dev
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
import 'dart:io';
19+
20+
import 'package:dan_xi/provider/settings_provider.dart';
21+
import 'package:dan_xi/util/platform_universal.dart';
22+
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
23+
import 'package:http/io_client.dart';
24+
25+
/// Exactly the same as [DefaultCacheManager], but also implements proxy support.
26+
///
27+
/// Note: It uses the same key as [DefaultCacheManager], so you can even
28+
/// manage the cache of this class with [DefaultCacheManager].
29+
class DefaultCacheManagerWithProxy extends CacheManager with ImageCacheManager {
30+
static const key = DefaultCacheManager.key;
31+
32+
static final DefaultCacheManagerWithProxy _instance =
33+
DefaultCacheManagerWithProxy._();
34+
35+
factory DefaultCacheManagerWithProxy() {
36+
return _instance;
37+
}
38+
39+
DefaultCacheManagerWithProxy._()
40+
: super(Config(key, fileService: _buildProxiedHttpFileService()));
41+
42+
static _buildProxiedHttpFileService() {
43+
String? proxy = SettingsProvider.getInstance().proxy;
44+
if (PlatformX.isWeb) {
45+
return HttpFileService();
46+
} else {
47+
return HttpFileService(
48+
httpClient: IOClient(HttpClient()
49+
..findProxy = (uri) => proxy != null ? "PROXY $proxy" : "DIRECT"));
50+
}
51+
}
52+
}

lib/widget/forum/auto_bbs_image.dart

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import 'package:cached_network_image/cached_network_image.dart';
19+
import 'package:dan_xi/util/io/cache_manager_with_proxy.dart';
1920
import 'package:dan_xi/util/io/dio_utils.dart';
2021
import 'package:dan_xi/widget/forum/render/base_render.dart';
2122
import 'package:dio/dio.dart';
@@ -70,6 +71,7 @@ class AutoBBSImage extends StatelessWidget {
7071
imageUrl: src,
7172
width: maxWidth,
7273
height: maxWidth,
74+
cacheManager: DefaultCacheManagerWithProxy(),
7375
// Ensure shape is the same as the loading indicator
7476
fit: BoxFit.contain,
7577
errorWidget: (context, url, error) {

0 commit comments

Comments
 (0)