file_util.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import 'dart:async';
  2. import 'dart:io' as io;
  3. import 'dart:typed_data';
  4. import 'package:flutter_sodium/flutter_sodium.dart';
  5. import 'package:logging/logging.dart';
  6. import 'package:path/path.dart';
  7. import 'package:dio/dio.dart';
  8. import 'package:flutter_cache_manager/flutter_cache_manager.dart';
  9. import 'package:flutter_image_compress/flutter_image_compress.dart';
  10. import 'package:photos/core/cache/image_cache.dart';
  11. import 'package:photos/core/cache/thumbnail_cache.dart';
  12. import 'package:photos/core/cache/video_cache_manager.dart';
  13. import 'package:photos/core/configuration.dart';
  14. import 'package:photos/core/constants.dart';
  15. import 'package:photos/core/network.dart';
  16. import 'package:photos/models/file.dart' as ente;
  17. import 'package:photos/models/file_type.dart';
  18. import 'package:photos/services/collections_service.dart';
  19. import 'package:photos/utils/thumbnail_util.dart';
  20. import 'crypto_util.dart';
  21. final _logger = Logger("FileUtil");
  22. void preloadFile(ente.File file) {
  23. if (file.fileType == FileType.video) {
  24. return;
  25. }
  26. getFile(file);
  27. }
  28. Future<io.File> getFile(ente.File file) async {
  29. if (file.localID == null) {
  30. return getFileFromServer(file);
  31. } else {
  32. final cachedFile = FileLruCache.get(file);
  33. if (cachedFile == null) {
  34. final diskFile = await (await file.getAsset()).file;
  35. FileLruCache.put(file, diskFile);
  36. return diskFile;
  37. }
  38. return cachedFile;
  39. }
  40. }
  41. void preloadThumbnail(ente.File file) {
  42. if (file.localID == null) {
  43. getThumbnailFromServer(file);
  44. } else {
  45. if (ThumbnailLruCache.get(file, THUMBNAIL_SMALL_SIZE) != null) {
  46. return;
  47. }
  48. file.getAsset().then((asset) {
  49. if (asset != null) {
  50. asset
  51. .thumbDataWithSize(
  52. THUMBNAIL_SMALL_SIZE,
  53. THUMBNAIL_SMALL_SIZE,
  54. quality: THUMBNAIL_QUALITY,
  55. )
  56. .then((data) {
  57. ThumbnailLruCache.put(file, data, THUMBNAIL_SMALL_SIZE);
  58. });
  59. }
  60. });
  61. }
  62. }
  63. final Map<int, Future<io.File>> fileDownloadsInProgress =
  64. Map<int, Future<io.File>>();
  65. Future<io.File> getFileFromServer(ente.File file,
  66. {ProgressCallback progressCallback}) async {
  67. final cacheManager = file.fileType == FileType.video
  68. ? VideoCacheManager.instance
  69. : DefaultCacheManager();
  70. return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
  71. if (info == null) {
  72. if (!fileDownloadsInProgress.containsKey(file.uploadedFileID)) {
  73. fileDownloadsInProgress[file.uploadedFileID] = _downloadAndDecrypt(
  74. file,
  75. cacheManager,
  76. progressCallback: progressCallback,
  77. );
  78. }
  79. return fileDownloadsInProgress[file.uploadedFileID];
  80. } else {
  81. return info.file;
  82. }
  83. });
  84. }
  85. Future<io.File> _downloadAndDecrypt(
  86. ente.File file, BaseCacheManager cacheManager,
  87. {ProgressCallback progressCallback}) async {
  88. _logger.info("Downloading file " + file.uploadedFileID.toString());
  89. final encryptedFilePath = Configuration.instance.getTempDirectory() +
  90. file.generatedID.toString() +
  91. ".encrypted";
  92. final decryptedFilePath = Configuration.instance.getTempDirectory() +
  93. file.generatedID.toString() +
  94. ".decrypted";
  95. final encryptedFile = io.File(encryptedFilePath);
  96. final decryptedFile = io.File(decryptedFilePath);
  97. final startTime = DateTime.now().millisecondsSinceEpoch;
  98. return Network.instance
  99. .getDio()
  100. .download(
  101. file.getDownloadUrl(),
  102. encryptedFilePath,
  103. options: Options(
  104. headers: {"X-Auth-Token": Configuration.instance.getToken()},
  105. ),
  106. onReceiveProgress: progressCallback,
  107. )
  108. .then((response) async {
  109. if (response.statusCode != 200) {
  110. _logger.warning("Could not download file: ", response.toString());
  111. return null;
  112. } else if (!encryptedFile.existsSync()) {
  113. _logger.warning("File was not downloaded correctly.");
  114. return null;
  115. }
  116. _logger.info("File downloaded: " + file.uploadedFileID.toString());
  117. _logger.info("Download speed: " +
  118. (io.File(encryptedFilePath).lengthSync() /
  119. (DateTime.now().millisecondsSinceEpoch - startTime))
  120. .toString() +
  121. "kBps");
  122. await CryptoUtil.decryptFile(encryptedFilePath, decryptedFilePath,
  123. Sodium.base642bin(file.fileDecryptionHeader), decryptFileKey(file));
  124. _logger.info("File decrypted: " + file.uploadedFileID.toString());
  125. encryptedFile.deleteSync();
  126. var fileExtension = "unknown";
  127. try {
  128. fileExtension = extension(file.title).substring(1).toLowerCase();
  129. } catch(e) {
  130. _logger.severe("Could not capture file extension");
  131. }
  132. var outputFile = decryptedFile;
  133. if ((fileExtension=="unknown" && file.fileType == FileType.image) ||
  134. (io.Platform.isAndroid && fileExtension == "heic")) {
  135. outputFile = await FlutterImageCompress.compressAndGetFile(
  136. decryptedFilePath,
  137. decryptedFilePath + ".jpg",
  138. keepExif: true,
  139. );
  140. decryptedFile.deleteSync();
  141. }
  142. final cachedFile = await cacheManager.putFile(
  143. file.getDownloadUrl(),
  144. outputFile.readAsBytesSync(),
  145. eTag: file.getDownloadUrl(),
  146. maxAge: Duration(days: 365),
  147. fileExtension: fileExtension,
  148. );
  149. outputFile.deleteSync();
  150. fileDownloadsInProgress.remove(file.uploadedFileID);
  151. return cachedFile;
  152. }).catchError((e) {
  153. fileDownloadsInProgress.remove(file.uploadedFileID);
  154. });
  155. }
  156. Uint8List decryptFileKey(ente.File file) {
  157. final encryptedKey = Sodium.base642bin(file.encryptedKey);
  158. final nonce = Sodium.base642bin(file.keyDecryptionNonce);
  159. final collectionKey =
  160. CollectionsService.instance.getCollectionKey(file.collectionID);
  161. return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
  162. }
  163. Future<Uint8List> compressThumbnail(Uint8List thumbnail) {
  164. return FlutterImageCompress.compressWithList(
  165. thumbnail,
  166. minHeight: COMPRESSED_THUMBNAIL_RESOLUTION,
  167. minWidth: COMPRESSED_THUMBNAIL_RESOLUTION,
  168. quality: 25,
  169. );
  170. }
  171. void clearCache(ente.File file) {
  172. if (file.fileType == FileType.video) {
  173. VideoCacheManager.instance.removeFile(file.getDownloadUrl());
  174. } else {
  175. DefaultCacheManager().removeFile(file.getDownloadUrl());
  176. }
  177. final cachedThumbnail = io.File(
  178. Configuration.instance.getThumbnailCacheDirectory() +
  179. "/" +
  180. file.uploadedFileID.toString());
  181. if (cachedThumbnail.existsSync()) {
  182. cachedThumbnail.deleteSync();
  183. }
  184. }