Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point we could make this into a separate SvgViewer widget.

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