Fix manifest issue, update download function and original file download support

This commit is contained in:
Anton Stubenbord
2023-02-14 21:02:51 +01:00
parent 219f1c7e1b
commit 42dbaaf855
7 changed files with 79 additions and 49 deletions

View File

@@ -9,7 +9,6 @@
<uses-permission android:name="android.permission.CAMERA" />
<!-- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29"
tools:ignore="ScopedStorage"/>
android:maxSdkVersion="29"/>
<!-- <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> -->
</manifest>

View File

@@ -58,14 +58,12 @@ class FileService {
static Future<Directory> get downloadsDirectory async {
if (Platform.isAndroid) {
Directory? directory;
directory = Directory('/storage/emulated/0/Download');
// Try the default global folder, if it exists
if (!await directory.exists()) {
directory = (await getExternalStorageDirectories(
Directory directory = Directory('/storage/emulated/0/Download');
if (!directory.existsSync()) {
final downloadsDir = await getExternalStorageDirectories(
type: StorageDirectory.downloads,
))!
.first;
);
directory = downloadsDir!.first;
}
return directory;
} else if (Platform.isIOS) {
@@ -77,8 +75,10 @@ class FileService {
static Future<Directory?> get scanDirectory async {
if (Platform.isAndroid) {
return (await getExternalStorageDirectories(type: StorageDirectory.dcim))!
.first;
final scanDir = await getExternalStorageDirectories(
type: StorageDirectory.dcim,
);
return scanDir!.first;
} else if (Platform.isIOS) {
return getApplicationDocumentsDirectory();
} else {

View File

@@ -231,6 +231,7 @@ class _DocumentDetailsPageState extends State<DocumentDetailsPage> {
child: DocumentDownloadButton(
document: state.document,
enabled: isConnected,
metaData: _metaData,
),
),
IconButton(

View File

@@ -4,6 +4,7 @@ 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/settings/view/widgets/radio_settings_dialog.dart';
import 'package:paperless_mobile/generated/l10n.dart';
import 'package:paperless_mobile/helpers/message_helpers.dart';
import 'package:paperless_mobile/helpers/permission_helpers.dart';
@@ -14,10 +15,12 @@ import 'package:permission_handler/permission_handler.dart';
class DocumentDownloadButton extends StatefulWidget {
final DocumentModel? document;
final bool enabled;
final Future<DocumentMetaData> metaData;
const DocumentDownloadButton({
super.key,
required this.document,
this.enabled = true,
required this.metaData,
});
@override
@@ -44,11 +47,31 @@ class _DocumentDownloadButtonState extends State<DocumentDownloadButton> {
}
Future<void> _onDownload(DocumentModel document) async {
// if (!Platform.isAndroid) {
// showSnackBar(
// context, "This feature is currently only supported on Android!");
// return;
// }
final api = context.read<PaperlessDocumentsApi>();
final meta = await widget.metaData;
try {
final downloadOriginal = await showDialog<bool>(
context: context,
builder: (context) => RadioSettingsDialog(
titleText: "Choose filetype", //TODO: INTL
options: [
RadioOption(
value: true,
label:
"Original (${meta.originalMimeType.split("/").last})", //TODO: INTL
),
RadioOption(
value: false,
label: "Archived (pdf)", //TODO: INTL
),
],
initialValue: false,
),
);
if (downloadOriginal == null) {
// Download was cancelled
return;
}
if (Platform.isAndroid && androidInfo!.version.sdkInt! < 30) {
final isGranted = await askForPermission(Permission.storage);
if (!isGranted) {
@@ -56,12 +79,15 @@ class _DocumentDownloadButtonState extends State<DocumentDownloadButton> {
}
}
setState(() => _isDownloadPending = true);
final service = context.read<PaperlessDocumentsApi>();
try {
final bytes = await service.download(document);
final meta = await service.getMetaData(document);
final bytes = await api.download(
document,
original: downloadOriginal,
);
final Directory dir = await FileService.downloadsDirectory;
String filePath = "${dir.path}/${meta.mediaFilename}";
final fileExtension =
downloadOriginal ? meta.mediaFilename.split(".").last : 'pdf';
String filePath = "${dir.path}/${meta.mediaFilename}".split(".").first;
filePath += ".$fileExtension";
final createdFile = File(filePath);
createdFile.createSync(recursive: true);
createdFile.writeAsBytesSync(bytes);

View File

@@ -23,7 +23,7 @@ abstract class PaperlessDocumentsApi {
Future<Iterable<int>> bulkAction(BulkAction action);
Future<Uint8List> getPreview(int docId);
String getThumbnailUrl(int docId);
Future<Uint8List> download(DocumentModel document);
Future<Uint8List> download(DocumentModel document, {bool original});
Future<FieldSuggestions> findSuggestions(DocumentModel document);
Future<List<String>> autocomplete(String query, [int limit = 10]);

View File

@@ -196,10 +196,14 @@ class PaperlessDocumentsApiImpl implements PaperlessDocumentsApi {
}
@override
Future<Uint8List> download(DocumentModel document) async {
Future<Uint8List> download(
DocumentModel document, {
bool original = false,
}) async {
try {
final response = await client.get(
"/api/documents/${document.id}/download/",
queryParameters: original ? {'original': true} : {},
options: Options(responseType: ResponseType.bytes),
);
return response.data;

View File

@@ -245,10 +245,10 @@ packages:
dependency: "direct main"
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.17.0"
connectivity_plus:
dependency: "direct main"
description:
@@ -897,10 +897,10 @@ packages:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7"
url: "https://pub.dev"
source: hosted
version: "0.6.7"
version: "0.6.5"
json_annotation:
dependency: "direct main"
description:
@@ -977,10 +977,10 @@ packages:
dependency: transitive
description:
name: matcher
sha256: c94db23593b89766cda57aab9ac311e3616cf87c6fa4e9749df032f66f30dcb8
sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72"
url: "https://pub.dev"
source: hosted
version: "0.12.14"
version: "0.12.13"
material_color_utilities:
dependency: transitive
description:
@@ -993,10 +993,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "12307e7f0605ce3da64cf0db90e5fcab0869f3ca03f76be6bb2991ce0a55e82b"
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
version: "1.8.0"
mime:
dependency: "direct main"
description:
@@ -1128,10 +1128,10 @@ packages:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.8.2"
path_drawing:
dependency: transitive
description:
@@ -1621,26 +1621,26 @@ packages:
dependency: transitive
description:
name: test
sha256: b54d427664c00f2013ffb87797a698883c46aee9288e027a50b46eaee7486fa2
sha256: a5fcd2d25eeadbb6589e80198a47d6a464ba3e2049da473943b8af9797900c2d
url: "https://pub.dev"
source: hosted
version: "1.22.2"
version: "1.22.0"
test_api:
dependency: transitive
description:
name: test_api
sha256: "6182294da5abf431177fccc1ee02401f6df30f766bc6130a0852c6b6d7ee6b2d"
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
url: "https://pub.dev"
source: hosted
version: "0.4.18"
version: "0.4.16"
test_core:
dependency: transitive
description:
name: test_core
sha256: "95ecc12692d0dd59080ab2d38d9cf32c7e9844caba23ff6cd285690398ee8ef4"
sha256: "0ef9755ec6d746951ba0aabe62f874b707690b5ede0fecc818b138fcc9b14888"
url: "https://pub.dev"
source: hosted
version: "0.4.22"
version: "0.4.20"
timezone:
dependency: transitive
description:
@@ -1765,10 +1765,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "2277c73618916ae3c2082b6df67b6ebb64b4c69d9bf23b23700707952ac30e60"
sha256: e7fb6c2282f7631712b69c19d1bff82f3767eea33a2321c14fa59ad67ea391c7
url: "https://pub.dev"
source: hosted
version: "10.1.2"
version: "9.4.0"
watcher:
dependency: transitive
description:
@@ -1789,10 +1789,10 @@ packages:
dependency: transitive
description:
name: webdriver
sha256: "3c923e918918feeb90c4c9fdf1fe39220fa4c0e8e2c0fffaded174498ef86c49"
sha256: ef67178f0cc7e32c1494645b11639dd1335f1d18814aa8435113a92e9ef9d841
url: "https://pub.dev"
source: hosted
version: "3.0.2"
version: "3.0.1"
webkit_inspection_protocol:
dependency: transitive
description:
@@ -1834,5 +1834,5 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=2.19.0 <4.0.0"
dart: ">=2.19.0 <3.0.0"
flutter: ">=3.4.0-17.0.pre"