fluffychat/lib/utils/matrix_file_extension.dart

46 lines
1.6 KiB
Dart
Raw Permalink Normal View History

2020-03-29 20:13:25 +02:00
import 'dart:io';
import 'package:famedlysdk/famedlysdk.dart';
2020-04-26 08:24:41 +02:00
import 'package:fluffychat/components/matrix.dart';
2020-04-10 18:25:47 +02:00
import 'package:flutter/foundation.dart';
2020-03-29 20:13:25 +02:00
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
2020-04-26 08:24:41 +02:00
import 'package:system/system.dart';
2020-04-10 18:25:47 +02:00
import 'package:universal_html/prefer_universal/html.dart' as html;
import 'package:mime_type/mime_type.dart';
2020-03-29 20:13:25 +02:00
extension MatrixFileExtension on MatrixFile {
void open() async {
2020-04-10 18:25:47 +02:00
if (kIsWeb) {
final fileName = path.split('/').last;
final mimeType = mime(fileName);
var element = html.document.createElement('a');
element.setAttribute(
'href', html.Url.createObjectUrlFromBlob(html.Blob([bytes])));
2020-04-10 18:52:40 +02:00
element.setAttribute('target', "_blank");
element.setAttribute('rel', "noopener");
element.setAttribute('download', fileName);
2020-04-10 18:25:47 +02:00
element.setAttribute('type', mimeType);
element.style.display = 'none';
html.document.body.append(element);
element.click();
element.remove();
2020-04-26 08:24:41 +02:00
} else if (Matrix.isMobile) {
Directory tempDir = await getTemporaryDirectory();
final file = File(tempDir.path + "/" + path.split("/").last);
file.writeAsBytesSync(bytes);
2020-04-10 18:25:47 +02:00
await OpenFile.open(file.path);
2020-04-26 08:24:41 +02:00
} else if (Platform.isLinux) {
2020-04-26 08:55:43 +02:00
final directory = Directory('Downloads');
if (await directory.exists() == false) {
await directory.create();
}
final filePath = "${directory.path}/";
2020-04-26 08:24:41 +02:00
final file = File(filePath + path.split("/").last);
file.writeAsBytesSync(bytes);
System.invoke('xdg-open $filePath');
2020-04-10 18:25:47 +02:00
}
2020-03-29 20:13:25 +02:00
return;
}
}