You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fluffychat/lib/utils/matrix_file_extension.dart

74 lines
2.4 KiB
Dart

5 years ago
import 'dart:io';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/utils/platform_infos.dart';
5 years ago
import 'package:flutter/foundation.dart';
5 years ago
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
5 years ago
import 'package:universal_html/prefer_universal/html.dart' as html;
import 'package:mime_type/mime_type.dart';
import 'package:downloads_path_provider_28/downloads_path_provider_28.dart';
import 'package:permission_handler/permission_handler.dart';
5 years ago
extension MatrixFileExtension on MatrixFile {
void open() async {
5 years ago
if (kIsWeb) {
final fileName = name.split('/').last;
5 years ago
final mimeType = mime(fileName);
var element = html.document.createElement('a');
element.setAttribute(
'href', html.Url.createObjectUrlFromBlob(html.Blob([bytes])));
5 years ago
element.setAttribute('target', '_blank');
element.setAttribute('rel', 'noopener');
element.setAttribute('download', fileName);
5 years ago
element.setAttribute('type', mimeType);
element.style.display = 'none';
html.document.body.append(element);
element.click();
element.remove();
} else {
if (!(await Permission.storage.request()).isGranted) return;
final downloadsDir = PlatformInfos.isDesktop
? (await getDownloadsDirectory())
: Platform.isAndroid
? (await DownloadsPathProvider.downloadsDirectory)
: (await getApplicationDocumentsDirectory());
final file = File(downloadsDir.path + '/' + name.split('/').last);
5 years ago
file.writeAsBytesSync(bytes);
5 years ago
await OpenFile.open(file.path);
}
5 years ago
return;
}
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';
}
}
5 years ago
}