From c7da398c535d121d34a20874199784137de2b710 Mon Sep 17 00:00:00 2001 From: Anton Stubenbord Date: Tue, 7 Mar 2023 00:23:23 +0100 Subject: [PATCH] fix: Fix download and share --- .../cubit/document_details_cubit.dart | 36 ++++++++++++++----- .../widgets/document_download_button.dart | 5 ++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/features/document_details/cubit/document_details_cubit.dart b/lib/features/document_details/cubit/document_details_cubit.dart index 7c8e90b..026b1f4 100644 --- a/lib/features/document_details/cubit/document_details_cubit.dart +++ b/lib/features/document_details/cubit/document_details_cubit.dart @@ -78,22 +78,27 @@ class DocumentDetailsCubit extends Cubit { Future openDocumentInSystemViewer() async { final cacheDir = await FileService.temporaryDirectory; - + await FileService.clearDirectoryContent(PaperlessDirectoryType.temporary); if (state.metaData == null) { await loadMetaData(); } + final desc = FileDescription.fromPath( + state.metaData!.mediaFilename.replaceAll("/", " ")); - await _api.downloadToFile( - state.document, - '${cacheDir.path}/${state.metaData!.mediaFilename}', - ); + final fileName = "${desc.filename}.pdf"; + final file = File("${cacheDir.path}/$fileName"); + if (!file.existsSync()) { + file.createSync(); + await _api.downloadToFile( + state.document, + file.path, + ); + } return OpenFilex.open( - '${cacheDir.path}/${state.metaData!.mediaFilename}', + file.path, type: "application/pdf", - ).then( - (value) => value.type, - ); + ).then((value) => value.type); } void replace(DocumentModel document) { @@ -115,6 +120,18 @@ class DocumentDetailsCubit extends Cubit { state.metaData!.mediaFilename .replaceAll("/", " "), // Flatten directory structure ); + if (!File(filePath).existsSync()) { + File(filePath).createSync(); + } else { + return _notificationService.notifyFileDownload( + document: state.document, + filename: "${desc.filename}.${desc.extension}", + filePath: filePath, + finished: true, + locale: locale, + ); + } + await _notificationService.notifyFileDownload( document: state.document, filename: "${desc.filename}.${desc.extension}", @@ -122,6 +139,7 @@ class DocumentDetailsCubit extends Cubit { finished: false, locale: locale, ); + await _api.downloadToFile( state.document, filePath, diff --git a/lib/features/document_details/view/widgets/document_download_button.dart b/lib/features/document_details/view/widgets/document_download_button.dart index 654a81c..63d98ae 100644 --- a/lib/features/document_details/view/widgets/document_download_button.dart +++ b/lib/features/document_details/view/widgets/document_download_button.dart @@ -2,12 +2,10 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:paperless_api/paperless_api.dart'; -import 'package:paperless_mobile/core/service/file_service.dart'; import 'package:paperless_mobile/extensions/flutter_extensions.dart'; import 'package:paperless_mobile/features/document_details/cubit/document_details_cubit.dart'; import 'package:paperless_mobile/features/document_details/view/dialogs/select_file_type_dialog.dart'; import 'package:paperless_mobile/features/settings/cubit/application_settings_cubit.dart'; -import 'package:paperless_mobile/features/settings/view/widgets/radio_settings_dialog.dart'; import 'package:paperless_mobile/generated/l10n/app_localizations.dart'; import 'package:paperless_mobile/helpers/message_helpers.dart'; @@ -61,10 +59,11 @@ class _DocumentDownloadButtonState extends State { // Download was cancelled return; } - if (Platform.isAndroid && androidInfo!.version.sdkInt! < 30) { + if (Platform.isAndroid && androidInfo!.version.sdkInt! <= 29) { final isGranted = await askForPermission(Permission.storage); if (!isGranted) { return; + //TODO: Tell user to grant permissions } } setState(() => _isDownloadPending = true);