fluffychat/lib/utils/matrix_sdk_extensions/matrix_file_extension.dart

57 lines
1.6 KiB
Dart
Raw Normal View History

2020-03-29 18:13:25 +00:00
import 'dart:io';
2021-07-11 15:12:56 +00:00
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
2021-07-11 15:12:56 +00:00
import 'package:file_picker_cross/file_picker_cross.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
2022-05-06 06:58:59 +00:00
import 'package:share_plus/share_plus.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
2022-07-10 07:26:16 +00:00
import 'package:fluffychat/utils/size_string.dart';
2020-03-29 18:13:25 +00:00
extension MatrixFileExtension on MatrixFile {
2021-07-11 15:12:56 +00:00
void save(BuildContext context) async {
2022-05-05 07:13:54 +00:00
if (PlatformInfos.isIOS) {
return share(context);
}
2021-07-11 15:12:56 +00:00
final fileName = name.split('/').last;
final file = FilePickerCross(bytes);
await file.exportToStorage(fileName: fileName, share: false);
}
void share(BuildContext context) async {
final fileName = name.split('/').last;
2022-05-06 06:58:59 +00:00
final tmpDirectory = await getTemporaryDirectory();
final path = '${tmpDirectory.path}$fileName';
await File(path).writeAsBytes(bytes);
2022-08-21 06:42:02 +00:00
// Workaround for iPad from
// https://github.com/fluttercommunity/plus_plugins/tree/main/packages/share_plus/share_plus#ipad
final box = context.findRenderObject() as RenderBox?;
2022-06-15 11:00:12 +00:00
await Share.shareFiles(
[path],
2022-08-21 06:42:02 +00:00
sharePositionOrigin:
box == null ? null : box.localToGlobal(Offset.zero) & box.size,
2022-06-15 11:00:12 +00:00
);
return;
2020-03-29 18:13:25 +00:00
}
2020-09-04 10:56:25 +00: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;
}
2022-07-10 07:26:16 +00:00
String get sizeString => size.sizeString;
2020-03-29 18:13:25 +00:00
}