fluffychat/lib/utils/matrix_file_extension.dart

35 lines
1.2 KiB
Dart
Raw Normal View History

2020-03-29 20:13:25 +02:00
import 'dart:io';
import 'package:famedlysdk/famedlysdk.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-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-05-13 15:58:59 +02:00
element.setAttribute('target', '_blank');
element.setAttribute('rel', 'noopener');
2020-04-10 18:52:40 +02:00
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();
} else {
2020-05-13 15:58:59 +02:00
var tempDir = await getTemporaryDirectory();
final file = File(tempDir.path + '/' + path.split('/').last);
2020-05-07 11:19:57 +02:00
file.writeAsBytesSync(bytes);
2020-04-10 18:25:47 +02:00
await OpenFile.open(file.path);
}
2020-03-29 20:13:25 +02:00
return;
}
}