From 213c815eee9ada697391b9c1da28937a8ceb8a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Thu, 8 Jun 2023 19:06:59 +0200 Subject: [PATCH] Added file upload in talk --- .../files-sharing/fileSharingApi.dart | 30 ++++++++++++ .../files-sharing/fileSharingApiParams.dart | 23 +++++++++ .../files-sharing/fileSharingApiParams.g.dart | 27 +++++++++++ lib/view/pages/files/fileUploadDialog.dart | 16 +++++-- lib/view/pages/files/files.dart | 2 +- lib/view/pages/talk/chatTextfield.dart | 48 +++++++++++-------- pubspec.yaml | 1 + 7 files changed, 123 insertions(+), 24 deletions(-) create mode 100644 lib/api/marianumcloud/files-sharing/fileSharingApi.dart create mode 100644 lib/api/marianumcloud/files-sharing/fileSharingApiParams.dart create mode 100644 lib/api/marianumcloud/files-sharing/fileSharingApiParams.g.dart diff --git a/lib/api/marianumcloud/files-sharing/fileSharingApi.dart b/lib/api/marianumcloud/files-sharing/fileSharingApi.dart new file mode 100644 index 0000000..1ef1070 --- /dev/null +++ b/lib/api/marianumcloud/files-sharing/fileSharingApi.dart @@ -0,0 +1,30 @@ +import 'dart:developer'; +import 'dart:io'; + +import 'package:http/http.dart' as http; +import 'package:http/http.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'fileSharingApiParams.dart'; + +class FileSharingApi { + Future share(FileSharingApiParams query) async { + var preferences = await SharedPreferences.getInstance(); + + Map headers = {}; + headers.putIfAbsent("Accept", () => "application/json"); + headers.putIfAbsent("OCS-APIRequest", () => "true"); + + Uri endpoint = Uri.https("${preferences.getString("username")!}:${preferences.getString("password")!}@cloud.marianum-fulda.de", "/ocs/v2.php/apps/files_sharing/api/v1/shares", query.toJson().map((key, value) => MapEntry(key, value.toString()))); + + log("request file share"); + Response response = await http.post(endpoint, headers: headers); + + if(response.statusCode != HttpStatus.ok) { + throw Exception("Api call failed with ${response.statusCode}: ${response.body}"); + } else { + log("File share successfull: ${response.body}"); + } + + } +} \ No newline at end of file diff --git a/lib/api/marianumcloud/files-sharing/fileSharingApiParams.dart b/lib/api/marianumcloud/files-sharing/fileSharingApiParams.dart new file mode 100644 index 0000000..3dac0dd --- /dev/null +++ b/lib/api/marianumcloud/files-sharing/fileSharingApiParams.dart @@ -0,0 +1,23 @@ +import 'package:json_annotation/json_annotation.dart'; + +part 'fileSharingApiParams.g.dart'; + +@JsonSerializable() +class FileSharingApiParams { + int shareType; + String shareWith; + String path; + String? referenceId; + String? talkMetaData; + + FileSharingApiParams({ + required this.shareType, + required this.shareWith, + required this.path, + this.referenceId, + this.talkMetaData + }); + + factory FileSharingApiParams.fromJson(Map json) => _$FileSharingApiParamsFromJson(json); + Map toJson() => _$FileSharingApiParamsToJson(this); +} \ No newline at end of file diff --git a/lib/api/marianumcloud/files-sharing/fileSharingApiParams.g.dart b/lib/api/marianumcloud/files-sharing/fileSharingApiParams.g.dart new file mode 100644 index 0000000..79abf90 --- /dev/null +++ b/lib/api/marianumcloud/files-sharing/fileSharingApiParams.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'fileSharingApiParams.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +FileSharingApiParams _$FileSharingApiParamsFromJson( + Map json) => + FileSharingApiParams( + shareType: json['shareType'] as int, + shareWith: json['shareWith'] as String, + path: json['path'] as String, + referenceId: json['referenceId'] as String?, + talkMetaData: json['talkMetaData'] as String?, + ); + +Map _$FileSharingApiParamsToJson( + FileSharingApiParams instance) => + { + 'shareType': instance.shareType, + 'shareWith': instance.shareWith, + 'path': instance.path, + 'referenceId': instance.referenceId, + 'talkMetaData': instance.talkMetaData, + }; diff --git a/lib/view/pages/files/fileUploadDialog.dart b/lib/view/pages/files/fileUploadDialog.dart index 67a4000..f061ef1 100644 --- a/lib/view/pages/files/fileUploadDialog.dart +++ b/lib/view/pages/files/fileUploadDialog.dart @@ -11,8 +11,11 @@ class FileUploadDialog extends StatefulWidget { final String localPath; final List remotePath; final String fileName; - final void Function() triggerReload; - const FileUploadDialog({Key? key, required this.localPath, required this.remotePath, required this.fileName, required this.triggerReload}) : super(key: key); + final void Function() onUploadFinished; + + final bool doShowFinish; + + const FileUploadDialog({Key? key, required this.localPath, required this.remotePath, required this.fileName, required this.onUploadFinished, this.doShowFinish = true}) : super(key: key); @override State createState() => _FileUploadDialogState(); @@ -40,6 +43,7 @@ class _FileUploadDialogState extends State { setState(() { state = FileUploadState.checkConflict; }); + await (await WebdavApi.webdav).mkdirs(widget.remotePath.join("/")); List result = (await webdavClient.ls(widget.remotePath.join("/"))).responses; if(result.any((element) => element.href!.endsWith("/$targetFileName"))) { setState(() { @@ -174,9 +178,13 @@ class _FileUploadDialogState extends State { } if(state == FileUploadState.done) { - widget.triggerReload(); + widget.onUploadFinished(); + if(!widget.doShowFinish) { + Navigator.of(context).pop(); + return const SizedBox.shrink(); + } return AlertDialog( - icon: const Icon(Icons.upload), + icon: const Icon(Icons.done), title: const Text("Upload fertig"), content: Column( mainAxisSize: MainAxisSize.min, diff --git a/lib/view/pages/files/files.dart b/lib/view/pages/files/files.dart index 9350579..9654fec 100644 --- a/lib/view/pages/files/files.dart +++ b/lib/view/pages/files/files.dart @@ -243,6 +243,6 @@ class _FilesState extends State { } var fileName = path.split(Platform.pathSeparator).last; - showDialog(context: context, builder: (context) => FileUploadDialog(localPath: path, remotePath: widget.path, fileName: fileName, triggerReload: () => _query()), barrierDismissible: false); + showDialog(context: context, builder: (context) => FileUploadDialog(localPath: path, remotePath: widget.path, fileName: fileName, onUploadFinished: () => _query()), barrierDismissible: false); } } diff --git a/lib/view/pages/talk/chatTextfield.dart b/lib/view/pages/talk/chatTextfield.dart index 5acad82..4d231c1 100644 --- a/lib/view/pages/talk/chatTextfield.dart +++ b/lib/view/pages/talk/chatTextfield.dart @@ -1,14 +1,17 @@ import 'dart:developer'; -import 'dart:io'; import 'package:flutter/material.dart'; import 'package:loader_overlay/loader_overlay.dart'; import 'package:provider/provider.dart'; +import 'package:uuid/uuid.dart'; +import '../../../api/marianumcloud/files-sharing/fileSharingApi.dart'; +import '../../../api/marianumcloud/files-sharing/fileSharingApiParams.dart'; import '../../../api/marianumcloud/talk/sendMessage/sendMessage.dart'; import '../../../api/marianumcloud/talk/sendMessage/sendMessageParams.dart'; import '../../../model/chatList/chatProps.dart'; import '../../../widget/filePick.dart'; +import '../files/fileUploadDialog.dart'; class ChatTextfield extends StatefulWidget { final String sendToToken; @@ -23,27 +26,34 @@ class _ChatTextfieldState extends State { bool sending = false; bool isLoading = false; - void mediaUpload(String? path) { + void _query() { + Provider.of(context, listen: false).run(); + } + + void mediaUpload(String? path) async { + context.loaderOverlay.hide(); + if(path == null) { - context.loaderOverlay.hide(); return; } - showDialog(context: context, builder: (context) { - return AlertDialog( - title: const Text("Datei senden"), - content: Image.file(File(path)), - actions: [ - TextButton(onPressed: () { - Navigator.of(context).pop(); - context.loaderOverlay.hide(); - }, child: const Text("Abbrechen")), - TextButton(onPressed: () { - context.loaderOverlay.hide(); - }, child: const Text("Senden")), - ], - ); - }); + String filename = "${path.split("/").last.split(".").first}-${const Uuid().v4()}.${path.split(".").last}"; + String shareFolder = "MarianumMobile"; + showDialog(context: context, builder: (context) => FileUploadDialog( + doShowFinish: false, + fileName: filename, + localPath: path, + remotePath: [shareFolder], + onUploadFinished: () { + FileSharingApi().share(FileSharingApiParams( + shareType: 10, + shareWith: widget.sendToToken, + path: "$shareFolder/$filename", + //referenceId: "eae2d4497f0e1ffa1c71e6d86f7a59a43fd49198e799cc08a8a0fa8205b99969", + //talkMetaData: "{\"messageType\":\"\"}" + )).then((value) => _query()); + }, + ), barrierDismissible: false); } @override @@ -123,7 +133,7 @@ class _ChatTextfieldState extends State { sending = true; }); SendMessage(widget.sendToToken, SendMessageParams(_textBoxController.text)).run().then((value) => { - Provider.of(context, listen: false).run(), + _query(), _textBoxController.text = "", setState(() { sending = false; diff --git a/pubspec.yaml b/pubspec.yaml index 642328f..f994d91 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -80,6 +80,7 @@ dependencies: animated_digit: ^3.2.1 syncfusion_flutter_pdfviewer: ^21.2.8 photo_view: ^0.14.0 + uuid: ^3.0.7 dev_dependencies: flutter_test: