mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-12-22 21:42:35 +01:00
68 lines
2.1 KiB
Dart
68 lines
2.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:open_file/open_file.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:universal_html/prefer_universal/html.dart' as html;
|
|
import 'package:mime_type/mime_type.dart';
|
|
|
|
extension MatrixFileExtension on MatrixFile {
|
|
void open() async {
|
|
if (kIsWeb) {
|
|
final fileName = name.split('/').last;
|
|
final mimeType = mime(fileName);
|
|
var element = html.document.createElement('a');
|
|
element.setAttribute(
|
|
'href', html.Url.createObjectUrlFromBlob(html.Blob([bytes])));
|
|
element.setAttribute('target', '_blank');
|
|
element.setAttribute('rel', 'noopener');
|
|
element.setAttribute('download', fileName);
|
|
element.setAttribute('type', mimeType);
|
|
element.style.display = 'none';
|
|
html.document.body.append(element);
|
|
element.click();
|
|
element.remove();
|
|
} else {
|
|
final downloadsDir = Platform.isAndroid
|
|
? (await getExternalStorageDirectory())
|
|
: (await getApplicationDocumentsDirectory());
|
|
|
|
final file = File(downloadsDir.path + '/' + name.split('/').last);
|
|
file.writeAsBytesSync(bytes);
|
|
await OpenFile.open(file.path);
|
|
}
|
|
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';
|
|
}
|
|
}
|
|
}
|