import 'package:nextcloud/nextcloud.dart'; import '../files_sharing/file_sharing_api.dart'; import '../files_sharing/file_sharing_api_params.dart'; import '../webdav/webdav_api.dart'; /// WebDAV folder under which Talk-shared files are uploaded before being /// linked into a chat. const String talkShareFolder = 'MarianumMobile'; Future? _shareFolderReady; /// Creates [talkShareFolder] if it is missing, at most once per session — the /// folder is permanent, so every later upload would just pay a round trip to /// be told it already exists (WebDAV answers MKCOL on an existing collection /// with 405, which is the normal case here and must not surface as an error). Future ensureTalkShareFolder() => _shareFolderReady ??= _createTalkShareFolder(); Future _createTalkShareFolder() async { try { final webdav = await WebdavApi.webdav; await webdav.mkcol(PathUri.parse('/$talkShareFolder')); } on DynamiteApiException catch (e) { // Anything but "already exists" leaves the folder unconfirmed, so the next // upload has to try again. if (e.statusCode != 405) _shareFolderReady = null; } catch (_) { _shareFolderReady = null; } } /// Posts each already-uploaded WebDAV path as a Talk share (ShareType 10) to /// the given conversation token. Calls run concurrently — the server accepts /// parallel posts and the picker UI is blocked anyway, so we shouldn't pay /// O(n*RTT) latency per share. Future shareFilesToChat({ required String token, required List remoteFilePaths, }) => Future.wait( remoteFilePaths.map( (path) => FileSharingApi().share( FileSharingApiParams(shareType: 10, shareWith: token, path: path), ), ), );