2023-03-18 17:02:12 +01:00
|
|
|
import 'dart:io';
|
|
|
|
|
2021-07-11 17:12:56 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2023-03-18 17:02:12 +01:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2023-03-18 17:02:12 +01:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2022-05-06 08:58:59 +02:00
|
|
|
import 'package:share_plus/share_plus.dart';
|
2023-03-18 17:02:12 +01:00
|
|
|
import 'package:universal_html/html.dart' as html;
|
2021-10-26 18:50:34 +02:00
|
|
|
|
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2022-07-10 09:26:16 +02:00
|
|
|
import 'package:fluffychat/utils/size_string.dart';
|
2020-03-29 20:13:25 +02:00
|
|
|
|
|
|
|
extension MatrixFileExtension on MatrixFile {
|
2021-07-11 17:12:56 +02:00
|
|
|
void save(BuildContext context) async {
|
2022-05-05 09:13:54 +02:00
|
|
|
if (PlatformInfos.isIOS) {
|
|
|
|
return share(context);
|
|
|
|
}
|
2022-04-30 17:43:38 +02:00
|
|
|
|
2023-03-18 17:02:12 +01:00
|
|
|
if (PlatformInfos.isWeb) {
|
|
|
|
_webDownload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final downloadPath = PlatformInfos.isAndroid
|
|
|
|
? await getDownloadPathAndroid()
|
|
|
|
: await FilePicker.platform.saveFile(
|
|
|
|
dialogTitle: L10n.of(context)!.saveFile,
|
|
|
|
fileName: name,
|
|
|
|
type: filePickerFileType,
|
|
|
|
);
|
|
|
|
if (downloadPath == null) return;
|
|
|
|
|
|
|
|
final result = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => File(downloadPath).writeAsBytes(bytes),
|
|
|
|
);
|
|
|
|
if (result.error != null) return;
|
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(
|
|
|
|
L10n.of(context)!.fileHasBeenSavedAt(downloadPath),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getDownloadPathAndroid() async {
|
2023-03-19 09:01:14 +01:00
|
|
|
final directory = await getDownloadDirectoryAndroid();
|
|
|
|
return '${directory.path}/$name';
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Directory> getDownloadDirectoryAndroid() async {
|
2023-03-19 09:03:56 +01:00
|
|
|
final defaultDownloadDirectory = Directory('/storage/emulated/0/Download');
|
|
|
|
if (await defaultDownloadDirectory.exists()) {
|
|
|
|
return defaultDownloadDirectory;
|
2023-03-18 17:02:12 +01:00
|
|
|
}
|
2023-03-19 09:01:14 +01:00
|
|
|
return await getApplicationDocumentsDirectory();
|
2023-03-18 17:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileType get filePickerFileType {
|
|
|
|
if (this is MatrixImageFile) return FileType.image;
|
|
|
|
if (this is MatrixAudioFile) return FileType.audio;
|
|
|
|
if (this is MatrixVideoFile) return FileType.video;
|
|
|
|
return FileType.any;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _webDownload() {
|
|
|
|
html.AnchorElement(
|
|
|
|
href: html.Url.createObjectUrlFromBlob(
|
|
|
|
html.Blob(
|
|
|
|
[bytes],
|
|
|
|
'application/octet-stream',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
..download = name
|
|
|
|
..click();
|
2022-04-30 17:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void share(BuildContext context) async {
|
2022-08-21 08:42:02 +02: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?;
|
|
|
|
|
2023-01-26 09:47:30 +01:00
|
|
|
await Share.shareXFiles(
|
|
|
|
[XFile.fromData(bytes)],
|
2022-08-21 08:42:02 +02:00
|
|
|
sharePositionOrigin:
|
|
|
|
box == null ? null : box.localToGlobal(Offset.zero) & box.size,
|
2022-06-15 13:00:12 +02:00
|
|
|
);
|
2022-04-30 17:43:38 +02:00
|
|
|
return;
|
2020-03-29 20:13:25 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-10 09:26:16 +02:00
|
|
|
String get sizeString => size.sizeString;
|
2020-03-29 20:13:25 +02:00
|
|
|
}
|