FurryChat/lib/utils/matrix_file_extension.dart

35 lines
1.2 KiB
Dart
Raw Normal View History

2020-03-29 18:13:25 +00:00
import 'dart:io';
import 'package:famedlysdk/famedlysdk.dart';
2020-04-10 16:25:47 +00:00
import 'package:flutter/foundation.dart';
2020-03-29 18:13:25 +00:00
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
2020-04-10 16:25:47 +00:00
import 'package:universal_html/prefer_universal/html.dart' as html;
import 'package:mime_type/mime_type.dart';
2020-03-29 18:13:25 +00:00
extension MatrixFileExtension on MatrixFile {
void open() async {
2020-04-10 16:25:47 +00: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 16:52:40 +00:00
element.setAttribute('target', "_blank");
element.setAttribute('rel', "noopener");
element.setAttribute('download', fileName);
2020-04-10 16:25:47 +00:00
element.setAttribute('type', mimeType);
element.style.display = 'none';
html.document.body.append(element);
element.click();
element.remove();
} else {
2020-05-07 09:19:57 +00:00
Directory tempDir = await getTemporaryDirectory();
final file = File(tempDir.path + "/" + path.split("/").last);
file.writeAsBytesSync(bytes);
2020-04-10 16:25:47 +00:00
await OpenFile.open(file.path);
}
2020-03-29 18:13:25 +00:00
return;
}
}