mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-05 23:15:42 -06:00
- Fix grey screen bug when adding labels from documnet upload - Add more permission checks to conditionally show widgets
124 lines
3.5 KiB
Dart
124 lines
3.5 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:paperless_api/paperless_api.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
class FileService {
|
|
static Future<File> saveToFile(
|
|
Uint8List bytes,
|
|
String filename,
|
|
) async {
|
|
final dir = await documentsDirectory;
|
|
if (dir == null) {
|
|
throw const PaperlessApiException.unknown(); //TODO: better handling
|
|
}
|
|
File file = File("${dir.path}/$filename");
|
|
return file..writeAsBytes(bytes);
|
|
}
|
|
|
|
static Future<Directory?> getDirectory(PaperlessDirectoryType type) {
|
|
switch (type) {
|
|
case PaperlessDirectoryType.documents:
|
|
return documentsDirectory;
|
|
case PaperlessDirectoryType.temporary:
|
|
return temporaryDirectory;
|
|
case PaperlessDirectoryType.scans:
|
|
return scanDirectory;
|
|
case PaperlessDirectoryType.download:
|
|
return downloadsDirectory;
|
|
}
|
|
}
|
|
|
|
static Future<File> allocateTemporaryFile(
|
|
PaperlessDirectoryType type, {
|
|
required String extension,
|
|
String? fileName,
|
|
}) async {
|
|
final dir = await getDirectory(type);
|
|
final _fileName = (fileName ?? const Uuid().v1()) + '.$extension';
|
|
return File('${dir?.path}/$_fileName');
|
|
}
|
|
|
|
static Future<Directory> get temporaryDirectory => getTemporaryDirectory();
|
|
|
|
static Future<Directory?> get documentsDirectory async {
|
|
if (Platform.isAndroid) {
|
|
return (await getExternalStorageDirectories(
|
|
type: StorageDirectory.documents,
|
|
))!
|
|
.first;
|
|
} else if (Platform.isIOS) {
|
|
final appDir = await getApplicationDocumentsDirectory();
|
|
final dir = Directory('${appDir.path}/documents');
|
|
dir.createSync();
|
|
return dir;
|
|
} else {
|
|
throw UnsupportedError("Platform not supported.");
|
|
}
|
|
}
|
|
|
|
static Future<Directory> get downloadsDirectory async {
|
|
if (Platform.isAndroid) {
|
|
Directory directory = Directory('/storage/emulated/0/Download');
|
|
if (!directory.existsSync()) {
|
|
final downloadsDir = await getExternalStorageDirectories(
|
|
type: StorageDirectory.downloads,
|
|
);
|
|
directory = downloadsDir!.first;
|
|
}
|
|
return directory;
|
|
} else if (Platform.isIOS) {
|
|
final appDir = await getApplicationDocumentsDirectory();
|
|
final dir = Directory('${appDir.path}/downloads');
|
|
dir.createSync();
|
|
return dir;
|
|
} else {
|
|
throw UnsupportedError("Platform not supported.");
|
|
}
|
|
}
|
|
|
|
static Future<Directory?> get scanDirectory async {
|
|
if (Platform.isAndroid) {
|
|
final scanDir = await getExternalStorageDirectories(
|
|
type: StorageDirectory.dcim,
|
|
);
|
|
return scanDir!.first;
|
|
} else if (Platform.isIOS) {
|
|
final appDir = await getApplicationDocumentsDirectory();
|
|
final dir = Directory('${appDir.path}/scans');
|
|
dir.createSync();
|
|
return dir;
|
|
} else {
|
|
throw UnsupportedError("Platform not supported.");
|
|
}
|
|
}
|
|
|
|
static Future<void> clearUserData() async {
|
|
final scanDir = await scanDirectory;
|
|
final tempDir = await temporaryDirectory;
|
|
await scanDir?.delete(recursive: true);
|
|
await tempDir.delete(recursive: true);
|
|
}
|
|
|
|
static Future<void> clearDirectoryContent(PaperlessDirectoryType type) async {
|
|
final dir = await getDirectory(type);
|
|
|
|
if (dir == null || !(await dir.exists())) {
|
|
return;
|
|
}
|
|
|
|
await Future.wait(
|
|
dir.listSync().map((item) => item.delete(recursive: true)),
|
|
);
|
|
}
|
|
}
|
|
|
|
enum PaperlessDirectoryType {
|
|
documents,
|
|
temporary,
|
|
scans,
|
|
download;
|
|
}
|