download.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import 'dart:async';
  2. import 'dart:ui' as ui;
  3. import 'package:flutter/foundation.dart';
  4. import 'package:http/http.dart';
  5. import 'package:logging/logging.dart';
  6. import 'package:puzzleweave/models/api_helper.dart';
  7. import 'package:puzzleweave/models/items.dart';
  8. import 'package:puzzleweave/utils/utils.dart';
  9. import 'package:puzzleweave/utils/image_decoder.dart';
  10. import 'data.dart';
  11. final Logger _log = Logger('download.dart');
  12. /// ✅ 优化:增加缓存数量到3个
  13. const maxCachedItems = 3;
  14. /// Singleton
  15. class Download {
  16. static final Download _instance = Download._internal();
  17. factory Download() => _instance;
  18. Download._internal();
  19. final Map<String, DownloadItem> _cache = {};
  20. DownloadItem download(String url, String cachePath) {
  21. if (_cache[url] != null) {
  22. _log.info('Cache hit for $url');
  23. _cache[url]!.touch();
  24. return _cache[url]!;
  25. } else {
  26. final item = DownloadItem(url, cachePath);
  27. _cache[url] = item;
  28. _watch(item);
  29. return item;
  30. }
  31. }
  32. _watch(DownloadItem item) async {
  33. try {
  34. await item.loadCompleter.future;
  35. Future.microtask(_clean);
  36. } catch (err) {
  37. _log.info('Watch download item got error: $err');
  38. _cache.remove(item.url);
  39. }
  40. }
  41. _clean() {
  42. final list = _cache.values.where((item) => item.loadCompleter.isCompleted).toList();
  43. if (list.length <= maxCachedItems) return;
  44. _log.info('Cleaning download cache...');
  45. list.sort((a, b) => a.lastUsed.compareTo(b.lastUsed));
  46. while (list.length > maxCachedItems) {
  47. final item = list.removeAt(0);
  48. _log.info('Cleaning item from memory: $item');
  49. item.dispose();
  50. _cache.remove(item.url);
  51. }
  52. }
  53. /// ✅ 新增:清理所有内存中的数据(保留下载任务)
  54. void clearAllData() {
  55. _log.info('Clearing all download data from memory');
  56. int count = 0;
  57. _cache.forEach((key, item) {
  58. if (item.data != null) {
  59. item.clearData();
  60. count++;
  61. }
  62. });
  63. _log.info('Cleared $count download items from memory');
  64. }
  65. /// ✅ 新增:获取当前缓存统计
  66. Map<String, dynamic> getCacheStats() {
  67. final total = _cache.length;
  68. final completed = _cache.values.where((item) => item.loadCompleter.isCompleted).length;
  69. final withData = _cache.values.where((item) => item.data != null).length;
  70. return {'total': total, 'completed': completed, 'withData': withData};
  71. }
  72. clearAllCached() async {
  73. final file = await localFile('cache');
  74. await file.delete(recursive: true);
  75. }
  76. }
  77. class DownloadItem {
  78. final String url;
  79. final String cachePath;
  80. ValueNotifier<double> progress = ValueNotifier(0.0);
  81. final Completer<void> loadCompleter = Completer();
  82. Client? client;
  83. StreamSubscription? subscription;
  84. /// ✅ 新增:最后访问时间
  85. DateTime? _lastAccessTime;
  86. /// ✅ 新增:数据保留时长(5分钟未使用则自动清理)
  87. static const _dataRetentionDuration = Duration(minutes: 5);
  88. /// ✅ 新增:自动清理定时器
  89. Timer? _cleanupTimer;
  90. int lastUsed;
  91. Uint8List? _data;
  92. bool _isDisposed = false;
  93. DownloadItem(this.url, this.cachePath) : lastUsed = DateTime.now().millisecondsSinceEpoch {
  94. _log.info('New download item for: $url');
  95. _start();
  96. }
  97. Uint8List? get data => _data;
  98. set data(Uint8List? val) => _data = val;
  99. touch() {
  100. lastUsed = DateTime.now().millisecondsSinceEpoch;
  101. _lastAccessTime = DateTime.now();
  102. }
  103. _start() async {
  104. try {
  105. await _download();
  106. if (!loadCompleter.isCompleted) loadCompleter.complete();
  107. } catch (err) {
  108. if (!loadCompleter.isCompleted) loadCompleter.completeError(err);
  109. }
  110. }
  111. Future<void> _download() async {
  112. progress.value = 0;
  113. final file = await localFile(cachePath);
  114. _checkDispose();
  115. if (await file.exists()) {
  116. _log.info('Disk cache hit (Metadata only) for $cachePath');
  117. progress.value = 1.0;
  118. return;
  119. }
  120. final List<int> bytes = [];
  121. client = Client();
  122. final response = await client!.send(Request('GET', Uri.parse(url)));
  123. _checkDispose();
  124. if (response.statusCode != 200) throw Exception('Status:${response.statusCode}');
  125. final length = response.contentLength ?? 0;
  126. final streamCompleter = Completer();
  127. subscription = response.stream.listen(
  128. (value) {
  129. bytes.addAll(value);
  130. if (length > 0) progress.value = bytes.length / length;
  131. },
  132. onDone: () => streamCompleter.complete(),
  133. onError: (e) => streamCompleter.completeError(e),
  134. cancelOnError: true,
  135. );
  136. await streamCompleter.future;
  137. _checkDispose();
  138. if (bytes.length < 1024 + 24) {
  139. throw Exception('Downloaded data is too small (${bytes.length} bytes)');
  140. }
  141. final cleanBytes = Uint8List.fromList(bytes.sublist(24));
  142. _data = cleanBytes;
  143. /// ✅ 原子性写入优化
  144. final tempPath = '$cachePath.tmp';
  145. await saveBytes(tempPath, _data!);
  146. final tempFile = await localFile(tempPath);
  147. final finalFile = await localFile(cachePath);
  148. await tempFile.rename(finalFile.path);
  149. _log.info('Download and save complete for $url');
  150. client?.close();
  151. /// ✅ 调度自动清理
  152. _scheduleCleanup();
  153. }
  154. /// ✅ 供 Loader 真正需要数据时调用
  155. Future<Uint8List> ensureDataLoaded() async {
  156. _lastAccessTime = DateTime.now();
  157. if (_data != null) {
  158. _log.info('Data cache hit for $cachePath');
  159. _scheduleCleanup();
  160. return _data!;
  161. }
  162. _log.info('Performing late read from disk: $cachePath');
  163. final file = await localFile(cachePath);
  164. _data = await file.readAsBytes();
  165. _scheduleCleanup();
  166. return _data!;
  167. }
  168. /// ✅ 新增:调度自动清理
  169. void _scheduleCleanup() {
  170. _cleanupTimer?.cancel();
  171. _cleanupTimer = Timer(_dataRetentionDuration, () {
  172. if (_data != null && _lastAccessTime != null) {
  173. final timeSinceLastAccess = DateTime.now().difference(_lastAccessTime!);
  174. if (timeSinceLastAccess >= _dataRetentionDuration) {
  175. _log.info('Auto-cleaning unused data: $cachePath');
  176. _data = null;
  177. }
  178. }
  179. });
  180. }
  181. /// ✅ 新增:手动清理数据
  182. void clearData() {
  183. _cleanupTimer?.cancel();
  184. _data = null;
  185. _log.info('Manually cleared data: $cachePath');
  186. }
  187. _checkDispose() {
  188. if (_isDisposed) throw Exception('Disposed');
  189. }
  190. dispose() {
  191. _isDisposed = true;
  192. _cleanupTimer?.cancel();
  193. _data = null;
  194. subscription?.cancel();
  195. client?.close();
  196. }
  197. @override
  198. String toString() => '[$cachePath]';
  199. }
  200. abstract class ItemLoader {
  201. final Completer completer = Completer();
  202. Uint8List? get data;
  203. ValueNotifier<double> get progress;
  204. ItemLoader();
  205. Future<Uint8List> _prepareData() async {
  206. await completer.future;
  207. if (data == null) throw 'Data missing after completion';
  208. return data!;
  209. }
  210. Future<ui.Image> getImageBySize(int width, int height, {allowUpscaling = false}) async {
  211. final bytes = await _prepareData();
  212. final codec = await ui.instantiateImageCodec(bytes, targetHeight: height, targetWidth: width, allowUpscaling: allowUpscaling);
  213. return (await codec.getNextFrame()).image;
  214. }
  215. Future<ui.Image> getImage() async {
  216. final bytes = await _prepareData();
  217. final codec = await ui.instantiateImageCodec(bytes);
  218. return (await codec.getNextFrame()).image;
  219. }
  220. static void preload(ListItem item, String quality) {
  221. if (BuiltinRegistry.contains(item.id)) {
  222. _log.info('Preload: Skipping builtin ${item.id}');
  223. return;
  224. }
  225. if (item is RemoteItem) {
  226. Download().download(ApiHelper.imageUri(item.id, quality), item.cachePath);
  227. }
  228. }
  229. factory ItemLoader.load(ListItem item, String quality) {
  230. if (BuiltinRegistry.contains(item.id)) {
  231. _log.info('Built-in hit: ${item.id}, skip network.');
  232. return AssetItemLoader('assets/builtin/${item.id}.jpeg');
  233. }
  234. switch (item.runtimeType) {
  235. case RemoteItem:
  236. return RemoteItemLoader(ApiHelper.imageUri(item.id, quality), item.cachePath);
  237. case AssetItem:
  238. return AssetItemLoader((item as AssetItem).image);
  239. default:
  240. throw 'Unknown item type';
  241. }
  242. }
  243. }
  244. class AssetItemLoader extends ItemLoader {
  245. final String path;
  246. Uint8List? _data;
  247. @override
  248. ValueNotifier<double> progress = ValueNotifier(0);
  249. AssetItemLoader(this.path) {
  250. _load();
  251. }
  252. _load() async {
  253. try {
  254. _data = await loadFileDataFromAsset(path);
  255. completer.complete();
  256. progress.value = 1.0;
  257. } catch (e) {
  258. completer.completeError(e);
  259. }
  260. }
  261. @override
  262. Uint8List? get data => _data;
  263. }
  264. class RemoteItemLoader extends ItemLoader {
  265. final String url;
  266. final String cachePath;
  267. late final DownloadItem downloadItem;
  268. RemoteItemLoader(this.url, this.cachePath) {
  269. downloadItem = Download().download(url, cachePath);
  270. _load();
  271. }
  272. _load() async {
  273. try {
  274. await downloadItem.loadCompleter.future;
  275. completer.complete();
  276. } catch (err) {
  277. completer.completeError(err);
  278. }
  279. }
  280. @override
  281. Uint8List? get data => downloadItem.data;
  282. @override
  283. Future<Uint8List> _prepareData() async {
  284. await completer.future;
  285. return await downloadItem.ensureDataLoaded();
  286. }
  287. @override
  288. ValueNotifier<double> get progress => downloadItem.progress;
  289. }