file_util.dart 6.5 KB

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