From d38c37a36580cd3ec6628d8ef9dd445e1c339ca4 Mon Sep 17 00:00:00 2001 From: Hamlet Jiang Su <30667958+hjiangsu@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:28:36 -0700 Subject: [PATCH] Improve handling of instances using image proxies (#1528) * fix: fix issue with markdown images not loading for proxied images * refactor: adjust image dimension logic to account for proxied images, remove proxy bypass from markdown images * fix: fix parsing for proxied image url --- lib/post/utils/post.dart | 7 ------- lib/shared/common_markdown_body.dart | 17 ++++++++++++++--- lib/utils/media/image.dart | 21 +++++++++++++-------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/post/utils/post.dart b/lib/post/utils/post.dart index 108782dad..642ac8078 100644 --- a/lib/post/utils/post.dart +++ b/lib/post/utils/post.dart @@ -325,13 +325,6 @@ Future parsePostView(PostView postView, bool fetchImageDimensions String? thumbnailUrl = postView.post.thumbnailUrl; String? videoUrl = postView.post.embedVideoUrl; - // Handle thumbnail urls that are proxied via /image_proxy - Uri? thumbnailUri = Uri.tryParse(thumbnailUrl ?? ''); - - if (thumbnailUri?.path == '/api/v3/image_proxy') { - thumbnailUrl = thumbnailUri?.queryParameters['url']; - } - // First, check what type of link we're dealing with based on the url (MediaType.image, MediaType.video, MediaType.link, MediaType.text) bool isImage = isImageUrl(url); bool isVideo = isVideoUrl(videoUrl ?? url); diff --git a/lib/shared/common_markdown_body.dart b/lib/shared/common_markdown_body.dart index cef4a281b..1adb25144 100644 --- a/lib/shared/common_markdown_body.dart +++ b/lib/shared/common_markdown_body.dart @@ -136,9 +136,20 @@ class CommonMarkdownBody extends StatelessWidget { showFullHeightImages: true, maxWidth: imageMaxWidth, ) - : ScalableImageWidget.fromSISource( - si: ScalableImageSource.fromSvgHttpUrl(uri), - ) + : Container( + constraints: isComment == true + ? BoxConstraints( + maxHeight: MediaQuery.of(context).size.width * 0.55, + maxWidth: MediaQuery.of(context).size.width * 0.60, + ) + : BoxConstraints( + maxWidth: imageMaxWidth ?? MediaQuery.of(context).size.width - 24, + ), + child: ScalableImageWidget.fromSISource( + fit: BoxFit.contain, + si: ScalableImageSource.fromSvgHttpUrl(uri), + ), + ), ], ), ); diff --git a/lib/utils/media/image.dart b/lib/utils/media/image.dart index 93d8211d7..fdbd6ee52 100644 --- a/lib/utils/media/image.dart +++ b/lib/utils/media/image.dart @@ -28,7 +28,14 @@ bool isImageUrl(String url) { } catch (e) { return false; } - final path = uri.path.toLowerCase(); + + String path = uri.path.toLowerCase(); + + // Handle thumbnail urls that are proxied via /image_proxy + if (uri.path == '/api/v3/image_proxy') { + Uri? parsedUri = Uri.tryParse(uri.queryParameters['url'] ?? ''); + if (parsedUri != null) path = parsedUri.path; + } for (final extension in imageExtensions) { if (path.endsWith(extension)) { @@ -74,11 +81,9 @@ Future retrieveImageDimensions({String? imageUrl, Uint8List? imageBytes}) return Size(uiImage.width.toDouble(), uiImage.height.toDouble()); } - // We know imageUrl is not null here due to the assertion. - bool isImage = isImageUrl(imageUrl!); - if (!isImage) throw Exception('The URL provided was not an image'); - - final imageProvider = ExtendedNetworkImageProvider(imageUrl, cache: true, cacheRawData: true); + // The image provider should throw an error if a valid image is not found + // This is to catch cases where the URL may return a valid image, but the URL path does not conform to the expected format + final imageProvider = ExtendedNetworkImageProvider(imageUrl ?? '', cache: true, cacheRawData: true); final imageData = await imageProvider.getNetworkImageData(); if (imageData == null) throw Exception('Failed to retrieve image data from $imageUrl'); @@ -86,7 +91,7 @@ Future retrieveImageDimensions({String? imageUrl, Uint8List? imageBytes}) final frame = await codec.getNextFrame(); final uiImage = frame.image; - print('width: ${uiImage.width} \t height: ${uiImage.height} \t $imageUrl'); + debugPrint('width: ${uiImage.width} \t height: ${uiImage.height} \t $imageUrl'); return Size(uiImage.width.toDouble(), uiImage.height.toDouble()); } catch (e) { throw Exception('Failed to retrieve image dimensions from $imageUrl: $e'); @@ -105,7 +110,7 @@ void uploadImage(BuildContext context, ImageBloc imageBloc, {bool postImage = fa try { Account? account = await fetchActiveProfileAccount(); - imageBloc.add(ImageUploadEvent(imageFile: path, instance: account!.instance!, jwt: account.jwt!, postImage: postImage)); + imageBloc.add(ImageUploadEvent(imageFile: path, instance: account!.instance, jwt: account.jwt!, postImage: postImage)); } catch (e) { showSnackbar(AppLocalizations.of(context)!.postUploadImageError, leadingIcon: Icons.warning_rounded, leadingIconColor: Theme.of(context).colorScheme.errorContainer); }