fluffychat/lib/utils/matrix_sdk_extensions.dart/matrix_file_extension.dart

75 lines
2.4 KiB
Dart
Raw Normal View History

2020-03-29 20:13:25 +02:00
import 'dart:io';
2020-11-14 14:04:36 +01:00
import 'package:android_path_provider/android_path_provider.dart';
import 'package:matrix/matrix.dart';
2020-10-28 08:05:10 +01:00
import 'package:fluffychat/utils/platform_infos.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';
2021-04-21 14:19:54 +02:00
import 'package:universal_html/html.dart' as html;
2020-04-10 18:25:47 +02:00
import 'package:mime_type/mime_type.dart';
2020-10-28 08:05:10 +01:00
import 'package:permission_handler/permission_handler.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) {
2020-07-02 11:30:59 +02:00
final fileName = name.split('/').last;
2020-04-10 18:25:47 +02:00
final mimeType = mime(fileName);
2021-04-14 10:37:15 +02:00
final element = html.document.createElement('a');
2020-04-10 18:25:47 +02:00
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 {
2021-04-13 16:08:28 +02:00
if (PlatformInfos.isMobile &&
!(await Permission.storage.request()).isGranted) return;
2020-10-28 08:05:10 +01:00
final downloadsDir = PlatformInfos.isDesktop
2020-11-14 14:04:36 +01:00
? (await getDownloadsDirectory()).path
2020-10-28 08:05:10 +01:00
: Platform.isAndroid
2020-11-14 14:04:36 +01:00
? (await AndroidPathProvider.downloadsPath)
: (await getApplicationDocumentsDirectory()).path;
2020-09-21 19:21:24 +02:00
2020-11-14 14:04:36 +01:00
final file = File(downloadsDir + '/' + name.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;
}
2020-09-04 12:56:25 +02:00
MatrixFile get detectFileType {
if (msgType == MessageTypes.Image) {
return MatrixImageFile(bytes: bytes, name: name);
}
if (msgType == MessageTypes.Video) {
return MatrixVideoFile(bytes: bytes, name: name);
}
if (msgType == MessageTypes.Audio) {
return MatrixAudioFile(bytes: bytes, name: name);
}
return this;
}
String get sizeString {
var size = this.size.toDouble();
if (size < 1000000) {
size = size / 1000;
size = (size * 10).round() / 10;
return '${size.toString()} KB';
} else if (size < 1000000000) {
size = size / 1000000;
size = (size * 10).round() / 10;
return '${size.toString()} MB';
} else {
size = size / 1000000000;
size = (size * 10).round() / 10;
return '${size.toString()} GB';
}
}
2020-03-29 20:13:25 +02:00
}