From 54153f195831c27dad19be07bff30ef719c9419b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Sat, 11 Mar 2023 20:26:00 +0100 Subject: [PATCH] Implemented simple and basic file downloading --- .idea/libraries/Dart_Packages.xml | 568 +++++++++--------- .idea/libraries/Flutter_Plugins.xml | 42 +- .../queries/downloadFile/downloadFile.dart | 21 + .../downloadFile/downloadFileParams.dart | 16 + .../downloadFile/downloadFileParams.g.dart | 21 + .../downloadFile/downloadFileResponse.dart | 14 + .../downloadFile/downloadFileResponse.g.dart | 19 + lib/api/marianumcloud/webdav/webdavApi.dart | 7 + lib/data/files/filesProps.dart | 1 + lib/screen/pages/files/fileDownload.dart | 89 +++ lib/screen/pages/files/files.dart | 10 +- pubspec.yaml | 9 +- 12 files changed, 505 insertions(+), 312 deletions(-) create mode 100644 lib/api/marianumcloud/webdav/queries/downloadFile/downloadFile.dart create mode 100644 lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.dart create mode 100644 lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.g.dart create mode 100644 lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.dart create mode 100644 lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.g.dart create mode 100644 lib/screen/pages/files/fileDownload.dart diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index ce99d18..e7f3432 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -5,287 +5,294 @@ - - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -299,49 +306,49 @@ - - - - - - - @@ -362,406 +369,399 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -775,376 +775,376 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml index 4255a83..6824382 100644 --- a/.idea/libraries/Flutter_Plugins.xml +++ b/.idea/libraries/Flutter_Plugins.xml @@ -1,27 +1,27 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFile.dart b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFile.dart new file mode 100644 index 0000000..01f54b5 --- /dev/null +++ b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFile.dart @@ -0,0 +1,21 @@ + +import 'package:marianum_mobile/api/apiResponse.dart'; +import 'package:marianum_mobile/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.dart'; +import 'package:marianum_mobile/api/marianumcloud/webdav/webdavApi.dart'; + +class DownloadFile extends WebdavApi { + DownloadFileParams params; + + DownloadFile(this.params) : super(params); + + @override + Future run() async { + + // final file = await File(localPath).create(); + // Uint8List downloadedFile = await (await WebdavApi.webdav).download(params.webdavPath); + // file.writeAsBytesSync(downloadedFile, flush: true, mode: FileMode.write); + // + // OpenFile.open(localPath); + throw UnimplementedError(); + } +} \ No newline at end of file diff --git a/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.dart b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.dart new file mode 100644 index 0000000..2f2c569 --- /dev/null +++ b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.dart @@ -0,0 +1,16 @@ +import 'package:json_annotation/json_annotation.dart'; +import 'package:marianum_mobile/api/apiParams.dart'; + +part 'downloadFileParams.g.dart'; + +@JsonSerializable() +class DownloadFileParams extends ApiParams { + String webdavSourcePath; + String localTargetPath; + String filename; + + DownloadFileParams(this.webdavSourcePath, this.localTargetPath, this.filename); + + factory DownloadFileParams.fromJson(Map json) => _$DownloadFileParamsFromJson(json); + Map toJson() => _$DownloadFileParamsToJson(this); +} \ No newline at end of file diff --git a/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.g.dart b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.g.dart new file mode 100644 index 0000000..3b56332 --- /dev/null +++ b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileParams.g.dart @@ -0,0 +1,21 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'downloadFileParams.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +DownloadFileParams _$DownloadFileParamsFromJson(Map json) => + DownloadFileParams( + json['webdavSourcePath'] as String, + json['localTargetPath'] as String, + json['filename'] as String, + ); + +Map _$DownloadFileParamsToJson(DownloadFileParams instance) => + { + 'webdavSourcePath': instance.webdavSourcePath, + 'localTargetPath': instance.localTargetPath, + 'filename': instance.filename, + }; diff --git a/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.dart b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.dart new file mode 100644 index 0000000..5dfc741 --- /dev/null +++ b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.dart @@ -0,0 +1,14 @@ + +import 'package:json_annotation/json_annotation.dart'; + +part 'downloadFileResponse.g.dart'; + +@JsonSerializable() +class DownloadFileResponse { + String path; + + DownloadFileResponse(this.path); + + factory DownloadFileResponse.fromJson(Map json) => _$DownloadFileResponseFromJson(json); + Map toJson() => _$DownloadFileResponseToJson(this); +} \ No newline at end of file diff --git a/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.g.dart b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.g.dart new file mode 100644 index 0000000..156b892 --- /dev/null +++ b/lib/api/marianumcloud/webdav/queries/downloadFile/downloadFileResponse.g.dart @@ -0,0 +1,19 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'downloadFileResponse.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +DownloadFileResponse _$DownloadFileResponseFromJson( + Map json) => + DownloadFileResponse( + json['path'] as String, + ); + +Map _$DownloadFileResponseToJson( + DownloadFileResponse instance) => + { + 'path': instance.path, + }; diff --git a/lib/api/marianumcloud/webdav/webdavApi.dart b/lib/api/marianumcloud/webdav/webdavApi.dart index 06f1131..b003370 100644 --- a/lib/api/marianumcloud/webdav/webdavApi.dart +++ b/lib/api/marianumcloud/webdav/webdavApi.dart @@ -14,10 +14,17 @@ abstract class WebdavApi extends ApiRequest { Future run(); static Future webdav = establishWebdavConnection(); + static Future webdavConnectString = buildWebdavConnectString(); static Future establishWebdavConnection() async { SharedPreferences preferences = await SharedPreferences.getInstance(); return NextcloudClient("https://cloud.marianum-fulda.de/", username: preferences.getString("username"), password: preferences.getString("password"), loginName: preferences.getString("username")).webdav; } + + static Future buildWebdavConnectString() async { + SharedPreferences preferences = await SharedPreferences.getInstance(); + + return "https://${preferences.getString("username")}:${preferences.getString("password")}@cloud.marianum-fulda.de/remote.php/dav/files/${preferences.getString("username")}/"; + } } \ No newline at end of file diff --git a/lib/data/files/filesProps.dart b/lib/data/files/filesProps.dart index eb6f0cd..f5c083e 100644 --- a/lib/data/files/filesProps.dart +++ b/lib/data/files/filesProps.dart @@ -1,3 +1,4 @@ + import 'package:marianum_mobile/api/apiResponse.dart'; import 'package:marianum_mobile/api/marianumcloud/webdav/queries/listFiles/listFilesCache.dart'; import 'package:marianum_mobile/api/marianumcloud/webdav/queries/listFiles/listFilesResponse.dart'; diff --git a/lib/screen/pages/files/fileDownload.dart b/lib/screen/pages/files/fileDownload.dart new file mode 100644 index 0000000..e600ab9 --- /dev/null +++ b/lib/screen/pages/files/fileDownload.dart @@ -0,0 +1,89 @@ +import 'dart:developer'; +import 'dart:io'; + +import 'package:flowder/flowder.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:marianum_mobile/api/marianumcloud/webdav/webdavApi.dart'; +import 'package:path_provider/path_provider.dart'; + +class FileDownload extends StatefulWidget { + String source; + String name; + FileDownload(this.source, this.name, {Key? key}) : super(key: key); + + @override + State createState() => _FileDownloadState(); +} + +class _FileDownloadState extends State { + late DownloaderCore core; + + void download(String remotePath, String name) async { + List? paths = await getExternalStorageDirectories(type: StorageDirectory.downloads); + + String local = paths!.first.path + Platform.pathSeparator + name; + + DownloaderUtils options = DownloaderUtils( + progressCallback: (current, total) { + final progress = (current / total) * 100; + setState(() => { + percent = progress, + }); + }, + file: File(local), + progress: ProgressImplementation(), + deleteOnCancel: true, + onDone: () { + // OpenFile.open(local).then((value) => () { + // log("Result: ${value.toString()}"); + // }); + Navigator.of(context).pop(); + showDialog(context: context, builder: (context) { + return AlertDialog( + icon: const Icon(Icons.link), + title: const Text("Dateipfad"), + content: Text(local), + ); + }); + }, + ); + + log(local); + + core = await Flowder.download( + "${await WebdavApi.webdavConnectString}$remotePath", + options, + ); + } + + double percent = 0; + + @override + void initState() { + super.initState(); + log("Downloading file: Source(${widget.source}), Name(${widget.name})"); + download(widget.source, widget.name); + } + + @override + Widget build(BuildContext context) { + return AlertDialog( + title: const Text("Download"), + content: ListTile( + leading: const Icon(Icons.download), + title: LinearProgressIndicator(value: percent/100), + trailing: Text("${percent.round()}%"), + ), + actions: [ + TextButton( + child: const Text("Abbrechen"), + onPressed: () => () { + if(!core.isCancelled) core.cancel(); + Navigator.of(context, rootNavigator: true).pop(); + }, + ) + ], + ); + } +} diff --git a/lib/screen/pages/files/files.dart b/lib/screen/pages/files/files.dart index 86fd567..a52af36 100644 --- a/lib/screen/pages/files/files.dart +++ b/lib/screen/pages/files/files.dart @@ -1,7 +1,10 @@ +import 'dart:developer'; + import 'package:filesize/filesize.dart'; import 'package:flutter/material.dart'; import 'package:jiffy/jiffy.dart'; import 'package:marianum_mobile/api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart'; +import 'package:marianum_mobile/screen/pages/files/fileDownload.dart'; import 'package:marianum_mobile/widget/errorView.dart'; import 'package:provider/provider.dart'; @@ -66,11 +69,10 @@ class _FilesState extends State { props.enterFolder(file.name); } else { //TODO implement file download / view + log(file.path); + showDialog(context: context, builder: (context) { - return const AlertDialog( - title: Text("Datei öffnen"), - content: Text("Das öffnen von Dateien ist noch nicht implementiert!"), - ); + return FileDownload(file.path, file.name); }); } }, diff --git a/pubspec.yaml b/pubspec.yaml index 61280dd..ba40ad1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -51,14 +51,17 @@ dependencies: git: url: https://github.com/provokateurin/nextcloud-neon path: packages/nextcloud - flutter_launcher_icons: ^0.11.0 + flutter_launcher_icons: ^0.12.0 pretty_json: ^2.0.0 cached_network_image: ^3.2.3 - open_file: ^2.1.1 - flowder: ^0.2.0 url_launcher: ^6.1.10 flutter_linkify: ^5.0.2 filesize: ^2.0.1 + path_provider: ^2.0.13 + better_open_file: ^3.6.4 + flowder: + git: + url: https://github.com/Harsh223/flowder.git dependency_overrides: xml: ^6.2.2