fix: desktop drop of HTTP content on linux
Signed-off-by: The one with the braid <the-one@with-the-braid.cf>pull/545/head
parent
464206b581
commit
4b1734fb45
@ -0,0 +1,48 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:http/http.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
abstract class DesktopDropDownloader {
|
||||
const DesktopDropDownloader._();
|
||||
|
||||
static Future<String?> unsupportedUriCallback(String url) async {
|
||||
if (kIsWeb) return null;
|
||||
final uri = Uri.tryParse(url);
|
||||
if (uri == null) return null;
|
||||
|
||||
if (!['http', 'https'].contains(uri.scheme)) return null;
|
||||
|
||||
Response response;
|
||||
|
||||
try {
|
||||
response = await get(uri);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Directory tmp;
|
||||
|
||||
// that's likely failing on many distros but future proof for upcoming
|
||||
// implementations
|
||||
try {
|
||||
tmp = await getTemporaryDirectory();
|
||||
} catch (_) {
|
||||
tmp =
|
||||
await getDownloadsDirectory() ?? await getApplicationCacheDirectory();
|
||||
}
|
||||
|
||||
try {
|
||||
await tmp.create(recursive: true);
|
||||
final file =
|
||||
File('${tmp.path}/desktop_drop_example/${uri.path.split('/').last}');
|
||||
await file.create(recursive: true);
|
||||
await file.writeAsBytes(response.bodyBytes);
|
||||
return file.path;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue