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 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 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 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 get temporaryDirectory => getTemporaryDirectory(); static Future 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 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 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 clearUserData() async { final scanDir = await scanDirectory; final tempDir = await temporaryDirectory; await scanDir?.delete(recursive: true); await tempDir.delete(recursive: true); } static Future 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; }