file_util.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. getFileFromServer(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 getFileFromServer(file).then((file) => file.readAsBytesSync());
  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<io.File> getFileFromServer(File file) async {
  76. if (!file.isEncrypted) {
  77. return DefaultCacheManager().getSingleFile(file.getDownloadUrl());
  78. } else {
  79. return DefaultCacheManager()
  80. .getFileFromCache(file.getDownloadUrl())
  81. .then((info) {
  82. if (info == null) {
  83. return _downloadAndDecrypt(file);
  84. } else {
  85. return info.file;
  86. }
  87. });
  88. }
  89. }
  90. Future<io.File> _downloadAndDecrypt(File file) async {
  91. final temporaryPath = Configuration.instance.getTempDirectory() +
  92. file.generatedID.toString() +
  93. ".aes";
  94. return Dio().download(file.getDownloadUrl(), temporaryPath).then((_) async {
  95. final data = await CryptoUtil.decryptFileToData(
  96. temporaryPath, Configuration.instance.getKey());
  97. io.File(temporaryPath).deleteSync();
  98. return DefaultCacheManager().putFile(file.getDownloadUrl(), data);
  99. });
  100. }