mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-11-20 11:09:25 +01:00
Merge branch 'soru/save-file-picker' into 'main'
feat: Add a proper file saver Closes #381 and #213 See merge request famedly/fluffychat!439
This commit is contained in:
commit
7b3d3781db
@ -2471,5 +2471,22 @@
|
|||||||
"@yourOwnUsername": {
|
"@yourOwnUsername": {
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"placeholders": {}
|
"placeholders": {}
|
||||||
|
},
|
||||||
|
"saveFile": "Save file",
|
||||||
|
"@saveFile": {
|
||||||
|
"type": "text",
|
||||||
|
"placeholders": {}
|
||||||
|
},
|
||||||
|
"saveFileToFolder": "Save file to this foler",
|
||||||
|
"@saveFileToFolder": {
|
||||||
|
"type": "text",
|
||||||
|
"placeholders": {}
|
||||||
|
},
|
||||||
|
"savedFileAs": "Saved file as {filename}",
|
||||||
|
"@savedFileAs": {
|
||||||
|
"type": "text",
|
||||||
|
"placeholders": {
|
||||||
|
"filename": {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,8 @@ class ImageViewerController extends State<ImageViewer> {
|
|||||||
VRouter.of(context).to('/rooms');
|
VRouter.of(context).to('/rooms');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Open this file with a system call.
|
/// Save this file with a system call.
|
||||||
void openFileAction() => widget.event.openFile(context);
|
void saveFileAction() => widget.event.saveFile(context);
|
||||||
|
|
||||||
/// Go back if user swiped it away
|
/// Go back if user swiped it away
|
||||||
void onInteractionEnds(ScaleEndDetails endDetails) {
|
void onInteractionEnds(ScaleEndDetails endDetails) {
|
||||||
|
@ -31,7 +31,7 @@ class ImageViewerView extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(Icons.download_outlined),
|
icon: Icon(Icons.download_outlined),
|
||||||
onPressed: controller.openFileAction,
|
onPressed: controller.saveFileAction,
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
tooltip: L10n.of(context).downloadFile,
|
tooltip: L10n.of(context).downloadFile,
|
||||||
),
|
),
|
||||||
|
@ -6,12 +6,13 @@ import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|||||||
import 'matrix_file_extension.dart';
|
import 'matrix_file_extension.dart';
|
||||||
|
|
||||||
extension LocalizedBody on Event {
|
extension LocalizedBody on Event {
|
||||||
void openFile(BuildContext context) async {
|
void saveFile(BuildContext context) async {
|
||||||
final matrixFile = await showFutureLoadingDialog(
|
final matrixFile = await showFutureLoadingDialog(
|
||||||
context: context,
|
context: context,
|
||||||
future: () => downloadAndDecryptAttachmentCached(),
|
future: () => downloadAndDecryptAttachmentCached(),
|
||||||
);
|
);
|
||||||
matrixFile.result?.open();
|
|
||||||
|
matrixFile.result?.save(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
IconData get statusIcon {
|
IconData get statusIcon {
|
||||||
|
@ -1,45 +1,53 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:android_path_provider/android_path_provider.dart';
|
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
import 'package:fluffychat/utils/platform_infos.dart';
|
import 'package:fluffychat/utils/platform_infos.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:open_file/open_file.dart';
|
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
import 'package:universal_html/html.dart' as html;
|
|
||||||
import 'package:mime_type/mime_type.dart';
|
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
||||||
|
import 'package:filesystem_picker/filesystem_picker.dart';
|
||||||
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
|
|
||||||
extension MatrixFileExtension on MatrixFile {
|
extension MatrixFileExtension on MatrixFile {
|
||||||
void open() async {
|
void save(BuildContext context) async {
|
||||||
if (kIsWeb) {
|
if (PlatformInfos.isMobile &&
|
||||||
final fileName = name.split('/').last;
|
!(await Permission.storage.request()).isGranted) return;
|
||||||
final mimeType = mime(fileName);
|
final fileName = name.split('/').last;
|
||||||
final element = html.document.createElement('a');
|
if (PlatformInfos.isAndroid) {
|
||||||
element.setAttribute(
|
final path = await FilesystemPicker.open(
|
||||||
'href', html.Url.createObjectUrlFromBlob(html.Blob([bytes])));
|
title: L10n.of(context).saveFile,
|
||||||
element.setAttribute('target', '_blank');
|
context: context,
|
||||||
element.setAttribute('rel', 'noopener');
|
rootDirectory: Directory('/sdcard/'),
|
||||||
element.setAttribute('download', fileName);
|
fsType: FilesystemType.folder,
|
||||||
element.setAttribute('type', mimeType);
|
pickText: L10n.of(context).saveFileToFolder,
|
||||||
element.style.display = 'none';
|
folderIconColor: Theme.of(context).primaryColor,
|
||||||
html.document.body.append(element);
|
requestPermission: () async =>
|
||||||
element.click();
|
await Permission.storage.request().isGranted,
|
||||||
element.remove();
|
);
|
||||||
|
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))));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (PlatformInfos.isMobile &&
|
final file = FilePickerCross(bytes);
|
||||||
!(await Permission.storage.request()).isGranted) return;
|
await file.exportToStorage(fileName: fileName);
|
||||||
final downloadsDir = PlatformInfos.isDesktop
|
|
||||||
? (await getDownloadsDirectory()).path
|
|
||||||
: Platform.isAndroid
|
|
||||||
? (await AndroidPathProvider.downloadsPath)
|
|
||||||
: (await getApplicationDocumentsDirectory()).path;
|
|
||||||
|
|
||||||
final file = File(downloadsDir + '/' + name.split('/').last);
|
|
||||||
file.writeAsBytesSync(bytes);
|
|
||||||
await OpenFile.open(file.path);
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixFile get detectFileType {
|
MatrixFile get detectFileType {
|
||||||
|
@ -24,7 +24,7 @@ class MessageDownloadContent extends StatelessWidget {
|
|||||||
primary: Theme.of(context).scaffoldBackgroundColor,
|
primary: Theme.of(context).scaffoldBackgroundColor,
|
||||||
onPrimary: Theme.of(context).textTheme.bodyText1.color,
|
onPrimary: Theme.of(context).textTheme.bodyText1.color,
|
||||||
),
|
),
|
||||||
onPressed: () => event.openFile(context),
|
onPressed: () => event.saveFile(context),
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
|
28
pubspec.lock
28
pubspec.lock
@ -29,13 +29,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.5.0"
|
version: "1.5.0"
|
||||||
android_path_provider:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: android_path_provider
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "0.2.1"
|
|
||||||
animations:
|
animations:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -297,6 +290,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.0.2"
|
version: "0.0.2"
|
||||||
|
filesystem_picker:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: filesystem_picker
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.4"
|
||||||
firebase_core:
|
firebase_core:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -653,13 +653,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.0"
|
version: "1.0.0"
|
||||||
mime_type:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: mime_type
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.0.0"
|
|
||||||
moor:
|
moor:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -711,13 +704,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
open_file:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: open_file
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "3.2.1"
|
|
||||||
open_noti_settings:
|
open_noti_settings:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -9,7 +9,6 @@ environment:
|
|||||||
dependencies:
|
dependencies:
|
||||||
adaptive_dialog: ^1.0.1
|
adaptive_dialog: ^1.0.1
|
||||||
adaptive_theme: ^2.2.0
|
adaptive_theme: ^2.2.0
|
||||||
android_path_provider: ^0.2.1
|
|
||||||
audioplayers: ^0.19.1
|
audioplayers: ^0.19.1
|
||||||
cached_network_image: ^3.0.0
|
cached_network_image: ^3.0.0
|
||||||
cupertino_icons: any
|
cupertino_icons: any
|
||||||
@ -21,6 +20,7 @@ dependencies:
|
|||||||
url: https://gitlab.com/famedly/libraries/fcm_shared_isolate.git
|
url: https://gitlab.com/famedly/libraries/fcm_shared_isolate.git
|
||||||
ref: main
|
ref: main
|
||||||
file_picker_cross: ^4.4.2
|
file_picker_cross: ^4.4.2
|
||||||
|
filesystem_picker: ^1.0.4
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
flutter_app_badger: ^1.2.0
|
flutter_app_badger: ^1.2.0
|
||||||
@ -43,12 +43,10 @@ dependencies:
|
|||||||
intl: any
|
intl: any
|
||||||
localstorage: ^4.0.0+1
|
localstorage: ^4.0.0+1
|
||||||
matrix: ^0.1.7
|
matrix: ^0.1.7
|
||||||
mime_type: ^1.0.0
|
|
||||||
native_imaging:
|
native_imaging:
|
||||||
git:
|
git:
|
||||||
url: https://gitlab.com/famedly/libraries/native_imaging.git
|
url: https://gitlab.com/famedly/libraries/native_imaging.git
|
||||||
ref: master
|
ref: master
|
||||||
open_file: ^3.2.1
|
|
||||||
open_noti_settings: ^0.2.0
|
open_noti_settings: ^0.2.0
|
||||||
package_info_plus: ^1.0.3
|
package_info_plus: ^1.0.3
|
||||||
path_provider: ^2.0.2
|
path_provider: ^2.0.2
|
||||||
|
Loading…
Reference in New Issue
Block a user