cache.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'dart:io';
  2. import 'package:path_provider/path_provider.dart';
  3. import 'package:vitalapp/managers/interfaces/cache.dart';
  4. class CacheManager implements ICacheManager {
  5. /// 清理文件缓存(全部)
  6. @override
  7. void clearApplicationCache() async {
  8. Directory docDirectory = await getApplicationDocumentsDirectory();
  9. Directory tempDirectory = await getTemporaryDirectory();
  10. if (docDirectory.existsSync()) {
  11. await deleteDirectory(docDirectory);
  12. }
  13. if (tempDirectory.existsSync()) {
  14. await deleteDirectory(tempDirectory);
  15. }
  16. }
  17. @override
  18. void clearApplicationImageCache() async {
  19. Directory tempDirectory = await getTemporaryDirectory();
  20. if (tempDirectory.existsSync()) {
  21. await deleteImageDirectory(tempDirectory);
  22. }
  23. }
  24. /// 清理单个文件
  25. @override
  26. Future<void> deleteDirectory(FileSystemEntity file) async {
  27. if (file is Directory) {
  28. final List<FileSystemEntity> children = file.listSync();
  29. for (final FileSystemEntity child in children) {
  30. await deleteDirectory(child);
  31. await child.delete();
  32. }
  33. }
  34. }
  35. /// 递归清理图像
  36. Future<void> deleteImageDirectory(FileSystemEntity file) async {
  37. if (file is File) {
  38. if (file.path.endsWith('.png') ||
  39. file.path.endsWith('.jpg') ||
  40. file.path.endsWith('.jpeg') ||
  41. file.path.endsWith('.mp4') ||
  42. file.path.endsWith('.avi') ||
  43. file.path.endsWith('.mov')) {
  44. await file.delete();
  45. }
  46. }
  47. if (file is Directory) {
  48. final List<FileSystemEntity> children = file.listSync();
  49. for (final FileSystemEntity child in children) {
  50. await deleteImageDirectory(child);
  51. }
  52. }
  53. }
  54. /// 获取缓存文件大小
  55. @override
  56. Future<double> getCacheSize() async {
  57. //获取文件夹
  58. Directory docDirectory = await getApplicationDocumentsDirectory();
  59. Directory tempDirectory = await getTemporaryDirectory();
  60. double size = 0;
  61. if (docDirectory.existsSync()) {
  62. size += await getTotalSizeOfFilesInDir(docDirectory);
  63. }
  64. if (tempDirectory.existsSync()) {
  65. size += await getTotalSizeOfFilesInDir(tempDirectory);
  66. }
  67. return size;
  68. }
  69. @override
  70. Future<double> getImageCacheSize() async {
  71. //获取文件夹
  72. Directory docDirectory = await getApplicationDocumentsDirectory();
  73. Directory tempDirectory = await getTemporaryDirectory();
  74. double size = 0;
  75. if (docDirectory.existsSync()) {
  76. size += await getTotalSizeOfImageInDir(docDirectory);
  77. }
  78. if (tempDirectory.existsSync()) {
  79. size += await getTotalSizeOfImageInDir(tempDirectory);
  80. }
  81. return size;
  82. }
  83. static Future<double> getTotalSizeOfFilesInDir(
  84. final FileSystemEntity file) async {
  85. if (file is File) {
  86. int length = await file.length();
  87. return double.parse(length.toString());
  88. }
  89. if (file is Directory) {
  90. final List<FileSystemEntity> children = file.listSync();
  91. double total = 0;
  92. for (final FileSystemEntity child in children) {
  93. total += await getTotalSizeOfFilesInDir(child);
  94. }
  95. return total;
  96. }
  97. return 0;
  98. }
  99. static Future<double> getTotalSizeOfImageInDir(
  100. final FileSystemEntity file) async {
  101. if (file is File) {
  102. if (file.path.endsWith('.png') ||
  103. file.path.endsWith('.jpg') ||
  104. file.path.endsWith('.jpeg') ||
  105. file.path.endsWith('.mp4') ||
  106. file.path.endsWith('.avi') ||
  107. file.path.endsWith('.mov')) {
  108. int length = await file.length();
  109. return double.parse(length.toString());
  110. } else {
  111. return 0;
  112. }
  113. }
  114. if (file is Directory) {
  115. final List<FileSystemEntity> children = file.listSync();
  116. double total = 0;
  117. for (final FileSystemEntity child in children) {
  118. total += await getTotalSizeOfImageInDir(child);
  119. }
  120. return total;
  121. }
  122. return 0;
  123. }
  124. }