2020-03-29 20:13:25 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2021-06-18 10:29:48 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2020-10-28 08:05:10 +01:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2021-07-11 17:12:56 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-07-24 10:47:34 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2020-10-28 08:05:10 +01:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2021-07-11 17:12:56 +02:00
|
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
|
|
|
import 'package:filesystem_picker/filesystem_picker.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2021-07-24 10:47:34 +02:00
|
|
|
import 'package:share/share.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 {
|
|
|
|
final fileName = name.split('/').last;
|
2021-07-24 10:47:34 +02:00
|
|
|
if (PlatformInfos.isIOS) {
|
|
|
|
final tmpDirectory = await getTemporaryDirectory();
|
|
|
|
final path = '${tmpDirectory.path}$fileName';
|
|
|
|
await File(path).writeAsBytes(bytes);
|
|
|
|
await Share.shareFiles([path]);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-11 17:12:56 +02:00
|
|
|
if (PlatformInfos.isAndroid) {
|
2021-07-24 10:47:34 +02:00
|
|
|
if (!(await Permission.storage.request()).isGranted) return;
|
2021-07-11 17:12:56 +02:00
|
|
|
final path = await FilesystemPicker.open(
|
|
|
|
title: L10n.of(context).saveFile,
|
|
|
|
context: context,
|
|
|
|
rootDirectory: Directory('/sdcard/'),
|
|
|
|
fsType: FilesystemType.folder,
|
|
|
|
pickText: L10n.of(context).saveFileToFolder,
|
|
|
|
folderIconColor: Theme.of(context).primaryColor,
|
|
|
|
requestPermission: () async =>
|
|
|
|
await Permission.storage.request().isGranted,
|
|
|
|
);
|
|
|
|
if (path != null) {
|
|
|
|
// determine a unique filename
|
|
|
|
// somefile-number.extension, e.g. helloworld-1.txt
|
|
|
|
var file = File('$path/$fileName');
|
|
|
|
var i = 0;
|
|
|
|
var extension = '';
|
|
|
|
if (fileName.contains('.')) {
|
|
|
|
extension = fileName.substring(fileName.lastIndexOf('.'));
|
|
|
|
}
|
|
|
|
final fileNameWithoutExtension =
|
|
|
|
fileName.substring(0, fileName.lastIndexOf('.'));
|
|
|
|
while (await file.exists()) {
|
|
|
|
i++;
|
|
|
|
file = File('$path/$fileNameWithoutExtension-$i$extension');
|
|
|
|
}
|
|
|
|
await file.writeAsBytes(bytes);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content:
|
|
|
|
Text(L10n.of(context).savedFileAs(file.path.split('/').last))));
|
|
|
|
}
|
2020-04-10 18:25:47 +02:00
|
|
|
} else {
|
2021-07-11 17:12:56 +02:00
|
|
|
final file = FilePickerCross(bytes);
|
|
|
|
await file.exportToStorage(fileName: fileName);
|
2020-04-10 18:25:47 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|