fix onyl pyx.li JPG through parsing of website, as added imgbb.com provider in same way.
This commit is contained in:
1 parent
55529c3a61
commit
b137dd8fe1
2 files changed
+63
-21
No files matched your search
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class MessageImageHelper {
|
||||
static final Map<String, Future<bool>> _verifiedImageCache =
|
||||
@@ -22,49 +23,88 @@ class MessageImageHelper {
|
||||
caseSensitive: false,
|
||||
);
|
||||
|
||||
static final RegExp _ibbSharePattern = RegExp(
|
||||
r'''https?://(?:www\.)?ibb\.co/[^\s<>'"`/?#]+(?:[/?#][^\s<>'"`]*)?''',
|
||||
caseSensitive: false,
|
||||
);
|
||||
|
||||
static final RegExp _imagePattern = RegExp(
|
||||
r'''https?://[^\s<>'"`]+?\.(?:png|jpe?g|webp)(?:\?[^\s<>'"`]+)?(?:#[^\s<>'"`]+)?''',
|
||||
caseSensitive: false,
|
||||
);
|
||||
|
||||
static String? parse(String text) {
|
||||
static Future<String?> parse(String text) async {
|
||||
final trimmed = text.trim();
|
||||
if (trimmed.isEmpty) return null;
|
||||
String? imageUrl;
|
||||
|
||||
final pyxMatch = _pyxPattern.firstMatch(trimmed);
|
||||
if (pyxMatch != null) {
|
||||
final id = _trimBoundary(pyxMatch.group(1)!);
|
||||
imageUrl = 'https://pyx.li/i/$id.jpg';
|
||||
return _extractFromUrl(
|
||||
_trimBoundary(pyxMatch.group(0)!),
|
||||
RegExp(r'''<img[^>]+src=["']([^"']+)["']''', caseSensitive: false),
|
||||
);
|
||||
}
|
||||
|
||||
final ipfsUrlMatch = _ipfsUrlPattern.firstMatch(trimmed);
|
||||
if (ipfsUrlMatch != null) {
|
||||
imageUrl = _trimBoundary(ipfsUrlMatch.group(0)!);
|
||||
return _trimBoundary(ipfsUrlMatch.group(0)!);
|
||||
}
|
||||
|
||||
final ipfsCidMatch = _ipfsCidPattern.firstMatch(trimmed);
|
||||
if (ipfsCidMatch != null) {
|
||||
final cid = _trimBoundary(ipfsCidMatch.group(1)!);
|
||||
imageUrl = 'https://ipfs.io/ipfs/$cid';
|
||||
return 'https://ipfs.io/ipfs/$cid';
|
||||
}
|
||||
|
||||
final ibbMatch = _ibbSharePattern.firstMatch(trimmed);
|
||||
if (ibbMatch != null) {
|
||||
return _extractFromUrl(
|
||||
_trimBoundary(ibbMatch.group(0)!),
|
||||
RegExp(
|
||||
r'''<meta[^>]+property=["']og:image["'][^>]+content=["']([^"']+)["']''',
|
||||
caseSensitive: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final imageMatch = _imagePattern.firstMatch(trimmed);
|
||||
if (imageMatch != null) {
|
||||
imageUrl = _trimBoundary(imageMatch.group(0)!);
|
||||
return _trimBoundary(imageMatch.group(0)!);
|
||||
}
|
||||
|
||||
return imageUrl;
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<String?> parseVerified(String text) async {
|
||||
final imageUrl = parse(text);
|
||||
final imageUrl = await parse(text);
|
||||
if (imageUrl == null) return null;
|
||||
|
||||
final verified = await _verifyImageUrl(imageUrl);
|
||||
return verified ? imageUrl : null;
|
||||
}
|
||||
|
||||
static Future<String?> _extractFromUrl(String pageUrl, RegExp pattern) async {
|
||||
try {
|
||||
final response = await http
|
||||
.get(Uri.parse(pageUrl))
|
||||
.timeout(const Duration(seconds: 8));
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
return null;
|
||||
}
|
||||
final value = pattern.firstMatch(response.body)?.group(1);
|
||||
if (value == null || value.isEmpty) return null;
|
||||
|
||||
final imageUrl = Uri.parse(
|
||||
pageUrl,
|
||||
).resolve(value.replaceAll('&', '&'));
|
||||
return imageUrl.scheme == 'http' || imageUrl.scheme == 'https'
|
||||
? imageUrl.toString()
|
||||
: null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static String _trimBoundary(String value) {
|
||||
return value.trim().replaceAll(RegExp(r"[.,;:!?]+$"), '');
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ import 'package:meshcore_open/helpers/message_image_helper.dart';
|
||||
|
||||
void main() {
|
||||
group('MessageImageHelper', () {
|
||||
test('parses short pyx ids', () {
|
||||
final attachment = MessageImageHelper.parse(
|
||||
test('parses short pyx ids', () async {
|
||||
final attachment = await MessageImageHelper.parse(
|
||||
'my picture: https://pyx.li/?i=RzmdkTsE text',
|
||||
);
|
||||
|
||||
@@ -12,15 +12,17 @@ void main() {
|
||||
expect(attachment, 'https://pyx.li/i/RzmdkTsE.jpg');
|
||||
});
|
||||
|
||||
test('parses direct pyx image urls', () {
|
||||
final attachment = MessageImageHelper.parse('https://pyx.li/?i=Cd1KFiwu');
|
||||
test('parses direct pyx image urls', () async {
|
||||
final attachment = await MessageImageHelper.parse(
|
||||
'https://pyx.li/?i=Cd1KFiwu',
|
||||
);
|
||||
|
||||
expect(attachment, isNotNull);
|
||||
expect(attachment, 'https://pyx.li/i/Cd1KFiwu.jpg');
|
||||
});
|
||||
|
||||
test('parses provider-hosted ipfs links', () {
|
||||
final attachment = MessageImageHelper.parse(
|
||||
test('parses provider-hosted ipfs links', () async {
|
||||
final attachment = await MessageImageHelper.parse(
|
||||
'my picture: https://uneven-plum-tarantula.myfilebase.com/ipfs/bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly text',
|
||||
);
|
||||
|
||||
@@ -31,8 +33,8 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('parses direct ipfs cid urls', () {
|
||||
final attachment = MessageImageHelper.parse(
|
||||
test('parses direct ipfs cid urls', () async {
|
||||
final attachment = await MessageImageHelper.parse(
|
||||
'my picture: ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly text',
|
||||
);
|
||||
|
||||
@@ -43,8 +45,8 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('parses http image urls', () {
|
||||
final attachment = MessageImageHelper.parse(
|
||||
test('parses http image urls', () async {
|
||||
final attachment = await MessageImageHelper.parse(
|
||||
'see this image https://example.com/picture.png?size=large additional text',
|
||||
);
|
||||
|
||||
@@ -52,8 +54,8 @@ void main() {
|
||||
expect(attachment, 'https://example.com/picture.png?size=large');
|
||||
});
|
||||
|
||||
test('returns null for plain text', () {
|
||||
expect(MessageImageHelper.parse('hello world'), isNull);
|
||||
test('returns null for plain text', () async {
|
||||
expect(await MessageImageHelper.parse('hello world'), isNull);
|
||||
});
|
||||
|
||||
test('returns null for non-image ipfs gateway url', () async {
|
||||
|
||||
Reference in new issue
Block a user