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