Merge pull request #558 from M0Rf30/fix/gpx-export-desktop
Save GPX export via file dialog on desktop
This commit is contained in:
5 files changed
+477
-432
No files matched your search
@@ -16,6 +16,12 @@ class MessageUrlImageHelper {
|
||||
static final LinkedHashMap<String, Future<String?>> _messageImageCache =
|
||||
LinkedHashMap<String, Future<String?>>();
|
||||
|
||||
/// HTTP client used to resolve sharing pages.
|
||||
///
|
||||
/// Tests override this because `flutter_test` blocks real network requests.
|
||||
@visibleForTesting
|
||||
static http.Client? httpClient;
|
||||
|
||||
static final RegExp _pyxPattern = RegExp(
|
||||
r'''https?://pyx\.li/\?i=([A-Za-z0-9_-]+)''',
|
||||
caseSensitive: false,
|
||||
@@ -129,8 +135,9 @@ class MessageUrlImageHelper {
|
||||
|
||||
static Future<String?> _extractFromUrl(String pageUrl, RegExp pattern) async {
|
||||
try {
|
||||
final response = await http
|
||||
.get(Uri.parse(pageUrl))
|
||||
final uri = Uri.parse(pageUrl);
|
||||
final client = httpClient;
|
||||
final response = await (client == null ? http.get(uri) : client.get(uri))
|
||||
.timeout(const Duration(seconds: 8));
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
return null;
|
||||
@@ -138,9 +145,7 @@ class MessageUrlImageHelper {
|
||||
final value = pattern.firstMatch(response.body)?.group(1);
|
||||
if (value == null || value.isEmpty) return null;
|
||||
|
||||
final imageUrl = Uri.parse(
|
||||
pageUrl,
|
||||
).resolve(value.replaceAll('&', '&'));
|
||||
final imageUrl = uri.resolve(value.replaceAll('&', '&'));
|
||||
return imageUrl.scheme == 'http' || imageUrl.scheme == 'https'
|
||||
? imageUrl.toString()
|
||||
: null;
|
||||
|
||||
+426
-423
File diff suppressed because it is too large.
Load diff
@@ -7,6 +7,7 @@ import 'dart:io';
|
||||
import '../utils/platform_info.dart';
|
||||
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
|
||||
class ContactExport {
|
||||
final String name;
|
||||
@@ -161,7 +162,6 @@ class GpxExport {
|
||||
final xml = GpxWriter().asString(gpx, pretty: true);
|
||||
|
||||
// 2. Save to file
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final timestamp = DateTime.now()
|
||||
.toUtc()
|
||||
.toIso8601String()
|
||||
@@ -169,8 +169,29 @@ class GpxExport {
|
||||
.replaceAll('.', '-')
|
||||
.split('T')
|
||||
.join('_');
|
||||
final suggestedName = '$filename$timestamp.gpx';
|
||||
|
||||
final path = '${dir.path}/$filename$timestamp.gpx';
|
||||
// share_plus cannot share files on desktop: Linux throws outright
|
||||
// ("Sharing files not supported on Linux") and Windows only supports it
|
||||
// from build 10.0.17763 up. Desktops have a real file system and a save
|
||||
// dialog, so write the file where the user asks instead of sharing it.
|
||||
if (PlatformInfo.isDesktop) {
|
||||
final location = await getSaveLocation(
|
||||
suggestedName: suggestedName,
|
||||
acceptedTypeGroups: const [
|
||||
XTypeGroup(label: 'GPX', extensions: ['gpx']),
|
||||
],
|
||||
);
|
||||
if (location == null) {
|
||||
debugPrint('Save dialog was dismissed / cancelled by user.');
|
||||
return gpxExportCancelled;
|
||||
}
|
||||
await File(location.path).writeAsString(xml);
|
||||
return gpxExportSuccess;
|
||||
}
|
||||
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final path = '${dir.path}/$suggestedName';
|
||||
|
||||
final file = File(path);
|
||||
await file.writeAsString(xml);
|
||||
|
||||
@@ -64,6 +64,7 @@ dependencies:
|
||||
flutter_linkify: ^6.0.0 # Auto-detect and linkify URLs in text
|
||||
gpx: ^2.3.0
|
||||
path_provider: ^2.1.5
|
||||
file_selector: ^1.0.3 # desktop save dialog for GPX export
|
||||
share_plus: ^13.1.0
|
||||
build_pipe: ^0.3.1
|
||||
material_symbols_icons: ^4.2928.1
|
||||
|
||||
@@ -1,23 +1,38 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:meshcore_open/helpers/message_url_image_helper.dart';
|
||||
|
||||
void main() {
|
||||
group('MessageUrlImageHelper', () {
|
||||
tearDown(() => MessageUrlImageHelper.httpClient = null);
|
||||
|
||||
void mockPyxPage(String id) {
|
||||
MessageUrlImageHelper.httpClient = MockClient((request) async {
|
||||
expect(request.url.toString(), 'https://pyx.li/?i=$id');
|
||||
return http.Response(
|
||||
'<html><body><img src="/i/$id.jpg"></body></html>',
|
||||
200,
|
||||
headers: {'content-type': 'text/html; charset=utf-8'},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
test('parses short pyx ids', () async {
|
||||
mockPyxPage('RzmdkTsE');
|
||||
final attachment = await MessageUrlImageHelper.parse(
|
||||
'my picture: https://pyx.li/?i=RzmdkTsE text',
|
||||
);
|
||||
|
||||
expect(attachment, isNotNull);
|
||||
expect(attachment, 'https://pyx.li/i/RzmdkTsE.jpg');
|
||||
});
|
||||
|
||||
test('parses direct pyx image urls', () async {
|
||||
mockPyxPage('Cd1KFiwu');
|
||||
final attachment = await MessageUrlImageHelper.parse(
|
||||
'https://pyx.li/?i=Cd1KFiwu',
|
||||
);
|
||||
|
||||
expect(attachment, isNotNull);
|
||||
expect(attachment, 'https://pyx.li/i/Cd1KFiwu.jpg');
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user