o2_file_path_util.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 'dart:io';
  2. import 'package:path_provider/path_provider.dart';
  3. import 'package:path/path.dart' as path;
  4. import 'md5_util.dart';
  5. /// O2 目录存储工具类
  6. ///
  7. ///
  8. ///
  9. /// getApplicationSupportDirectory() 内部存储目录 多端都有
  10. /// getExternalStorageDirectory() 获取的是 android 的外部存储 (External Storage)只有Android端有
  11. /// getApplicationDocumentsDirectory 外部存储目录 多端都有
  12. class O2FilePathUtil {
  13. static const bbsPathName = 'bbs'; // bbs
  14. static const cmsPathName = 'cms'; // 门户
  15. static const portalPathName = 'portal'; // 门户
  16. static const processAttachmentPathName = 'process'; // 流程
  17. static const cloudFilePathName = 'pan'; // 云盘
  18. static const imFilePathName = 'im'; // 聊天
  19. static const logFilePathName = 'logs'; // 日志目录
  20. ///
  21. /// bbs webview下载文件存储路径
  22. ///
  23. static Future<String?> getBBSFileDownloadLocalPath(String folder, String fileName) async {
  24. return getFileDownloadLocalPathByType(folder, fileName, bbsPathName);
  25. }
  26. ///
  27. /// cms webview下载文件存储路径
  28. ///
  29. static Future<String?> getCmsFileDownloadLocalPath(String folder, String fileName) async {
  30. return getFileDownloadLocalPathByType(folder, fileName, cmsPathName);
  31. }
  32. ///
  33. /// 门户webview下载文件存储路径
  34. ///
  35. static Future<String?> getPortalFileDownloadLocalPath(String folder, String fileName) async {
  36. return getFileDownloadLocalPathByType(folder, fileName, portalPathName);
  37. }
  38. ///
  39. /// 流程文件存储路径
  40. ///
  41. static Future<String?> getProcessFileDownloadLocalPath(String folder, String fileName) async {
  42. return getFileDownloadLocalPathByType(folder, fileName, processAttachmentPathName);
  43. }
  44. /// 云盘文件存储路径
  45. static Future<String?> getCloudDiskFileDownloadLocalPath(String folder, String fileName) async {
  46. return getFileDownloadLocalPathByType(folder, fileName, cloudFilePathName);
  47. }
  48. ///
  49. /// 临时目录
  50. /// url作为目录
  51. ///
  52. static Future<String?> getTempFolderWithUrl(String url) async {
  53. try {
  54. String md5 = Md5Util.encode(url);
  55. Directory directory = await getTemporaryDirectory();
  56. var dir = Directory(path.join(directory.path, md5));
  57. if (!dir.existsSync()) {
  58. dir.createSync();
  59. }
  60. return dir.path;
  61. } catch (e) {
  62. // ignore: avoid_print
  63. print(e);
  64. return null;
  65. }
  66. }
  67. static Future<String?> getFileDownloadLocalPathByType(String folder, String fileName, String type) async {
  68. try {
  69. Directory? process = await _parentPath(type);
  70. if (process != null) {
  71. var parentDir = Directory(path.join(process.path, folder));
  72. if (!parentDir.existsSync()) {
  73. parentDir.createSync();
  74. }
  75. return path.join(parentDir.path, fileName);
  76. }
  77. } catch (e) {
  78. // ignore: avoid_print
  79. print(e);
  80. }
  81. return null;
  82. }
  83. ///
  84. /// 聊天文件存储路径
  85. ///
  86. static Future<String?> getImFileDownloadLocalPath(String folder, String fileName) async {
  87. try {
  88. Directory? im = await _parentPath(imFilePathName);
  89. if (im != null) {
  90. var parentDir = Directory(path.join(im.path, folder));
  91. if (!parentDir.existsSync()) {
  92. parentDir.createSync();
  93. }
  94. return path.join(parentDir.path, fileName);
  95. }
  96. } catch (e) {
  97. // ignore: avoid_print
  98. print(e);
  99. }
  100. return null;
  101. }
  102. ///
  103. /// 日志目录
  104. ///
  105. static Future<Directory?> getLogPath() async {
  106. return await _parentPath(logFilePathName);
  107. }
  108. ///
  109. /// 日志文件路径
  110. ///
  111. static Future<String?> getLogFilePath(String fileName) async {
  112. try {
  113. Directory? log = await getLogPath();
  114. if (log != null) {
  115. return path.join(log.path, fileName);
  116. }
  117. } catch (e) {
  118. // ignore: avoid_print
  119. print(e);
  120. }
  121. return null;
  122. }
  123. ///
  124. /// 缓存目录文件大小统计
  125. ///
  126. static Future<int> cacheSizeTotal() async {
  127. try {
  128. final dir = await getTemporaryDirectory();
  129. return await _recursionCountCacheSize(dir);
  130. }catch (e) {
  131. // ignore: avoid_print
  132. print(e);
  133. return 0;
  134. }
  135. }
  136. ///
  137. /// 清除缓存
  138. /// @TODO 把业务文件也删除了?
  139. ///
  140. static Future<void> clearCache() async {
  141. try {
  142. final dir = await getTemporaryDirectory();
  143. await _recursionDeleteCache(dir);
  144. }catch (e) {
  145. // ignore: avoid_print
  146. print(e);
  147. }
  148. }
  149. static Future<void> _recursionDeleteCache(FileSystemEntity file) async {
  150. if (file is File) {
  151. await file.delete();
  152. } else if ( file is Directory) {
  153. final list = file.listSync();
  154. if (list.isNotEmpty) {
  155. for (var element in list) {
  156. await _recursionDeleteCache(element);
  157. }
  158. }
  159. }
  160. }
  161. static Future<int> _recursionCountCacheSize(FileSystemEntity file) async {
  162. if (file is File) {
  163. return await file.length();
  164. }
  165. if (file is Directory) {
  166. final list = file.listSync();
  167. int size = 0;
  168. if (list.isNotEmpty) {
  169. for (final element in list) {
  170. size += await _recursionCountCacheSize(element);
  171. }
  172. }
  173. return size;
  174. }
  175. return 0;
  176. }
  177. static Future<Directory?> _parentPath(String parentDirName) async {
  178. try {
  179. Directory directory = await getApplicationSupportDirectory();
  180. // if (Platform.isAndroid) {
  181. // directory = await getExternalStorageDirectory();
  182. // } else if (Platform.isIOS) {
  183. // directory = await getApplicationDocumentsDirectory();
  184. // } else {
  185. // directory = await getApplicationSupportDirectory();
  186. // }
  187. // if (directory == null) {
  188. // return null;
  189. // }
  190. var dir = Directory(path.join(directory.path, parentDirName));
  191. if (!dir.existsSync()) {
  192. dir.createSync();
  193. }
  194. return dir;
  195. } catch (e) {
  196. // ignore: avoid_print
  197. print(e);
  198. return null;
  199. }
  200. }
  201. }