Skip to content

Commit

Permalink
Improve handling of instances using image proxies (#1528)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hjiangsu authored Aug 13, 2024
1 parent 1584234 commit d38c37a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
7 changes: 0 additions & 7 deletions lib/post/utils/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,6 @@ Future<PostViewMedia> 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);
Expand Down
17 changes: 14 additions & 3 deletions lib/shared/common_markdown_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
),
],
),
);
Expand Down
21 changes: 13 additions & 8 deletions lib/utils/media/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -74,19 +81,17 @@ Future<Size> 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');

final codec = await instantiateImageCodec(imageData);
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');
Expand All @@ -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);
}
Expand Down

0 comments on commit d38c37a

Please sign in to comment.