file_utils.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'dart:io';
  2. import 'dart:convert';
  3. import 'dart:typed_data';
  4. import 'package:clean/utils/image_util.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:photo_manager/photo_manager.dart';
  7. import '../model/asset_info.dart';
  8. enum FileType {
  9. analysis, // 照片分析
  10. privacy, // 隐私空间
  11. }
  12. class FileUtils {
  13. /// 获取 AssetEntity 保存目录
  14. static Future<String> getAssetPath(FileType type) async {
  15. final directory = await getApplicationDocumentsDirectory();
  16. var path = '${directory.path}/assets';
  17. if (type == FileType.privacy) {
  18. path = "$path/privacy";
  19. } else if (type == FileType.analysis) {
  20. path = "$path/analysis";
  21. }
  22. final dir = Directory(path);
  23. if (!dir.existsSync()) {
  24. dir.createSync(recursive: true);
  25. }
  26. return path;
  27. }
  28. /// 保存 AssetEntity 到本地
  29. static Future<AssetEntity?> saveAsset(FileType type, AssetEntity asset) async {
  30. try {
  31. final assetPath = await getAssetPath(type);
  32. final title = asset.id.substring(0, 36);
  33. final assetFile = File('$assetPath/$title.json');
  34. // // 将 AssetEntity 转换为 AssetInfo 后再序列化
  35. // final assetInfo = AssetInfo.fromAssetEntity(asset);
  36. // await assetFile.writeAsString(jsonEncode(assetInfo.toJson()));
  37. // 保存原始图片文件
  38. final file = await asset.file;
  39. if (file != null) {
  40. final imageFile = File('$assetPath/$title.jpg');
  41. await imageFile.writeAsBytes(await file.readAsBytes());
  42. // 保存缩略图
  43. final thumbData = await asset.thumbnailDataWithSize(ThumbnailSize(200, 200));
  44. if (thumbData != null) {
  45. final thumbFile = File('$assetPath/${title}thumb.jpg');
  46. await thumbFile.writeAsBytes(thumbData);
  47. // 创建并保存 AssetInfo
  48. final assetInfo = AssetInfo.fromAssetEntity(asset, '$assetPath/$title.json', '$assetPath/${title}thumb.jpg');
  49. final assetFile = File('$assetPath/$title.json');
  50. await assetFile.writeAsString(jsonEncode(assetInfo.toJson()));
  51. }
  52. }
  53. return asset;
  54. } catch (e) {
  55. print('保存 AssetEntity 失败: $e');
  56. return null;
  57. }
  58. }
  59. /// 从本地读取缩略图数据
  60. static Future<Uint8List?> getThumbData(FileType type, String assetId) async {
  61. try {
  62. final assetPath = await getAssetPath(type);
  63. final thumbFile = File('$assetPath/${assetId}thumb.jpg');
  64. if (await thumbFile.exists()) {
  65. return await thumbFile.readAsBytes();
  66. }
  67. return null;
  68. } catch (e) {
  69. print('读取缩略图数据失败: $e');
  70. return null;
  71. }
  72. }
  73. /// 从本地读取 AssetEntity
  74. // static Future<AssetEntity?> getAsset(FileType type, String fileName) async {
  75. // try {
  76. // final assetPath = await getAssetPath();
  77. // final assetFile = File('$assetPath/$fileName.json');
  78. // if (await assetFile.exists()) {
  79. // final jsonStr = await assetFile.readAsString();
  80. // final json = jsonDecode(jsonStr);
  81. // final assetInfo = AssetInfo.fromJson(json);
  82. // return await assetInfo.toAssetEntity();
  83. // }
  84. // return null;
  85. // } catch (e) {
  86. // print('读取 AssetEntity 失败: $e');
  87. // return null;
  88. // }
  89. // }
  90. /// 获取目录下所有 AssetEntity
  91. static Future<List<AssetInfo>> getAllAssets(FileType type) async {
  92. try {
  93. final assetPath = await getAssetPath(type);
  94. final assetDir = Directory(assetPath);
  95. if (!await assetDir.exists()) {
  96. return [];
  97. }
  98. final List<AssetInfo> assets = [];
  99. final List<FileSystemEntity> entities = await assetDir.list().toList();
  100. for (var entity in entities) {
  101. if (entity is File && entity.path.endsWith('.json')) {
  102. final jsonStr = await entity.readAsString();
  103. final json = jsonDecode(jsonStr);
  104. final assetInfo = AssetInfo.fromJson(json);
  105. File? file = await ImageUtil.getImageFile(type, assetInfo);
  106. if (file != null) {
  107. assetInfo.size = await FileUtils.getFileSize(file);
  108. }
  109. assets.add(assetInfo);
  110. }
  111. }
  112. return assets;
  113. } catch (e) {
  114. print('获取所有 AssetEntity 失败: $e');
  115. return [];
  116. }
  117. }
  118. /// 删除 AssetEntity 文件
  119. static Future<bool> deleteAsset(FileType type, String fileName) async {
  120. try {
  121. final assetPath = await getAssetPath(type);
  122. final assetFile = File('$assetPath/$fileName.json');
  123. if (await assetFile.exists()) {
  124. await assetFile.delete();
  125. }
  126. final assetFileImage = File('$assetPath/$fileName.jpg');
  127. if (await assetFileImage.exists()) {
  128. await assetFileImage.delete();
  129. }
  130. final assetFileIThumb = File('$assetPath/${fileName}thumb.jpg');
  131. if (await assetFileIThumb.exists()) {
  132. await assetFileIThumb.delete();
  133. }
  134. return true;
  135. } catch (e) {
  136. print('删除 AssetEntity 失败: $e');
  137. return false;
  138. }
  139. }
  140. // 获取文件大小
  141. static Future<int> getFileSize(File file) async {
  142. try {
  143. final bytes = await file.length();
  144. return bytes;
  145. } catch (e) {
  146. print('获取文件大小失败: $e');
  147. return 0;
  148. }
  149. }
  150. }