file_util.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import 'dart:io' as io;
  2. import 'dart:typed_data';
  3. import 'package:path/path.dart';
  4. import 'package:dio/dio.dart';
  5. import 'package:flutter_cache_manager/flutter_cache_manager.dart';
  6. import 'package:flutter_image_compress/flutter_image_compress.dart';
  7. import 'package:photo_manager/photo_manager.dart';
  8. import 'package:photos/core/cache/image_cache.dart';
  9. import 'package:photos/core/cache/thumbnail_cache.dart';
  10. import 'package:photos/core/cache/video_cache_manager.dart';
  11. import 'package:photos/core/configuration.dart';
  12. import 'package:photos/core/constants.dart';
  13. import 'package:photos/db/files_db.dart';
  14. import 'package:photos/models/file.dart';
  15. import 'package:photos/models/file_type.dart';
  16. import 'crypto_util.dart';
  17. Future<void> deleteFiles(List<File> files,
  18. {bool deleteEveryWhere = false}) async {
  19. await PhotoManager.editor
  20. .deleteWithIds(files.map((file) => file.localID).toList());
  21. for (File file in files) {
  22. deleteEveryWhere
  23. ? await FilesDB.instance.markForDeletion(file)
  24. : await FilesDB.instance.delete(file);
  25. }
  26. }
  27. void preloadFile(File file) {
  28. if (file.fileType == FileType.video) {
  29. return;
  30. }
  31. if (file.localID == null) {
  32. getFileFromServer(file);
  33. } else {
  34. if (FileLruCache.get(file) == null) {
  35. file.getAsset().then((asset) {
  36. asset.file.then((assetFile) {
  37. FileLruCache.put(file, assetFile);
  38. });
  39. });
  40. }
  41. }
  42. }
  43. void preloadLocalFileThumbnail(File file) {
  44. if (file.localID == null ||
  45. ThumbnailLruCache.get(file, THUMBNAIL_SMALL_SIZE) != null) {
  46. return;
  47. }
  48. file.getAsset().then((asset) {
  49. asset
  50. .thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
  51. .then((data) {
  52. ThumbnailLruCache.put(file, THUMBNAIL_SMALL_SIZE, data);
  53. });
  54. });
  55. }
  56. Future<Uint8List> getBytes(File file, {int quality = 100}) async {
  57. if (file.localID == null) {
  58. return getFileFromServer(file).then((file) => file.readAsBytesSync());
  59. } else {
  60. return await getBytesFromDisk(file, quality);
  61. }
  62. }
  63. Future<Uint8List> getBytesFromDisk(File file, int quality) async {
  64. final originalBytes = (await file.getAsset()).originBytes;
  65. if (extension(file.title) == ".HEIC" || quality != 100) {
  66. return originalBytes.then((bytes) {
  67. return FlutterImageCompress.compressWithList(bytes, quality: quality)
  68. .then((converted) {
  69. return Uint8List.fromList(converted);
  70. });
  71. });
  72. } else {
  73. return originalBytes;
  74. }
  75. }
  76. Future<io.File> getFileFromServer(File file) async {
  77. final cacheManager = file.fileType == FileType.video
  78. ? VideoCacheManager()
  79. : DefaultCacheManager();
  80. if (!file.isEncrypted) {
  81. return cacheManager.getSingleFile(file.getDownloadUrl());
  82. } else {
  83. return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
  84. if (info == null) {
  85. return _downloadAndDecrypt(file, cacheManager);
  86. } else {
  87. return info.file;
  88. }
  89. });
  90. }
  91. }
  92. Future<io.File> _downloadAndDecrypt(
  93. File file, BaseCacheManager cacheManager) async {
  94. final temporaryPath = Configuration.instance.getTempDirectory() +
  95. file.generatedID.toString() +
  96. ".aes";
  97. return Dio().download(file.getDownloadUrl(), temporaryPath).then((_) async {
  98. final data = await CryptoUtil.decryptFileToData(
  99. temporaryPath, Configuration.instance.getKey());
  100. io.File(temporaryPath).deleteSync();
  101. return cacheManager.putFile(file.getDownloadUrl(), data);
  102. });
  103. }