file_storage.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:typed_data';
  4. import 'package:fis_common/index.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:vitalapp/architecture/values/store_keys.dart';
  7. import 'package:vitalapp/rpc.dart';
  8. import 'shared_storage.dart';
  9. import 'text_storage.dart';
  10. /// 文件存储
  11. class FileStorage {
  12. FileStorage({
  13. required this.directory,
  14. });
  15. /// 存储目录
  16. final String directory;
  17. Future<Uint8List?> read(String fileName) async {
  18. final storagePath = await _getStoragePath();
  19. final filePath = "$storagePath/$fileName";
  20. final file = File(filePath);
  21. if (!await file.exists()) return null;
  22. final text = await file.readAsBytes();
  23. return text;
  24. }
  25. Future<bool> save(String fileName, Uint8List file) async {
  26. final storagePath = await _getStoragePath();
  27. final filePath = "$storagePath/$fileName";
  28. File fileToSave = File(filePath);
  29. _checkAndCreateDir(storagePath);
  30. if (!await fileToSave.exists()) {
  31. try {
  32. fileToSave = await fileToSave.create(recursive: true);
  33. } catch (e) {
  34. print(e);
  35. }
  36. }
  37. await fileToSave.writeAsBytes(file.toList());
  38. return true;
  39. }
  40. Future<bool> delete(String fileName) async {
  41. final storagePath = await _getStoragePath();
  42. final filePath = "$storagePath/$fileName";
  43. final file = File(filePath);
  44. if (await file.exists()) {
  45. try {
  46. await file.delete();
  47. return true;
  48. } catch (e) {
  49. print(e);
  50. return false;
  51. }
  52. }
  53. return false;
  54. }
  55. Future<bool> deleteDirectory(String directoryPath) async {
  56. final storagePath = await _getStoragePath();
  57. final directory = Directory(storagePath);
  58. final result = await directory.exists();
  59. if (result) {
  60. try {
  61. await directory.delete(recursive: true);
  62. return true;
  63. } catch (e) {
  64. print(e);
  65. return false;
  66. }
  67. }
  68. return false;
  69. }
  70. Future<void> _checkAndCreateDir(String dir) async {
  71. final directory = Directory(dir);
  72. final parentDirectory = directory.parent;
  73. try {
  74. if (!await parentDirectory.exists()) {
  75. await parentDirectory.create(recursive: true);
  76. }
  77. } catch (e) {
  78. print(e);
  79. }
  80. if (!await directory.exists()) {
  81. try {
  82. await directory.create();
  83. } catch (e) {
  84. print(e);
  85. }
  86. }
  87. }
  88. Future<String> _getStoragePath() async {
  89. Directory? dir;
  90. if (Platform.isAndroid) {
  91. dir = await getExternalStorageDirectory();
  92. }
  93. dir ??= await getApplicationDocumentsDirectory();
  94. if (directory.isNotEmpty) {
  95. return "${dir.path}/$directory";
  96. }
  97. return dir.path;
  98. }
  99. }
  100. /// 基于Base64字符串的壳子下文件导出
  101. class TextMediaFileExporterForShell {
  102. static Future<bool> exportFile(List<int> files, String storageName) async {
  103. final platform = FPlatform.current;
  104. if (platform == FPlatformEnum.webOnMac ||
  105. platform == FPlatformEnum.webOnWin) {
  106. var data = base64Encode(files);
  107. return await rpc.platform.setBytesToExport(data, storageName);
  108. } else {
  109. //Web端不需要特殊处理,手机端也有native的处理,因此直接返回true
  110. return true;
  111. }
  112. }
  113. }