file_util.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. Future<Uint8List> getBytes(ente.File file, {int quality = 100}) async {
  64. if (file.localID == null) {
  65. return getFileFromServer(file).then((file) => file.readAsBytesSync());
  66. } else {
  67. return await getBytesFromDisk(file, quality: quality);
  68. }
  69. }
  70. Future<Uint8List> getBytesFromDisk(ente.File file, {int quality = 100}) async {
  71. final originalBytes = (await file.getAsset()).originBytes;
  72. if (extension(file.title) == ".HEIC" || quality != 100) {
  73. return originalBytes.then((bytes) {
  74. return FlutterImageCompress.compressWithList(bytes, quality: quality)
  75. .then((converted) {
  76. return Uint8List.fromList(converted);
  77. });
  78. });
  79. } else {
  80. return originalBytes;
  81. }
  82. }
  83. final Map<int, Future<io.File>> fileDownloadsInProgress =
  84. Map<int, Future<io.File>>();
  85. Future<io.File> getFileFromServer(ente.File file,
  86. {ProgressCallback progressCallback}) async {
  87. final cacheManager = file.fileType == FileType.video
  88. ? VideoCacheManager.instance
  89. : DefaultCacheManager();
  90. return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
  91. if (info == null) {
  92. if (!fileDownloadsInProgress.containsKey(file.uploadedFileID)) {
  93. fileDownloadsInProgress[file.uploadedFileID] = _downloadAndDecrypt(
  94. file,
  95. cacheManager,
  96. progressCallback: progressCallback,
  97. );
  98. }
  99. return fileDownloadsInProgress[file.uploadedFileID];
  100. } else {
  101. return info.file;
  102. }
  103. });
  104. }
  105. Future<io.File> _downloadAndDecrypt(
  106. ente.File file, BaseCacheManager cacheManager,
  107. {ProgressCallback progressCallback}) async {
  108. _logger.info("Downloading file " + file.uploadedFileID.toString());
  109. final encryptedFilePath = Configuration.instance.getTempDirectory() +
  110. file.generatedID.toString() +
  111. ".encrypted";
  112. final decryptedFilePath = Configuration.instance.getTempDirectory() +
  113. file.generatedID.toString() +
  114. ".decrypted";
  115. final encryptedFile = io.File(encryptedFilePath);
  116. final decryptedFile = io.File(decryptedFilePath);
  117. final startTime = DateTime.now().millisecondsSinceEpoch;
  118. return Network.instance
  119. .getDio()
  120. .download(
  121. file.getDownloadUrl(),
  122. encryptedFilePath,
  123. options: Options(
  124. headers: {"X-Auth-Token": Configuration.instance.getToken()},
  125. ),
  126. onReceiveProgress: progressCallback,
  127. )
  128. .then((response) async {
  129. if (response.statusCode != 200) {
  130. _logger.warning("Could not download file: ", response.toString());
  131. return null;
  132. } else if (!encryptedFile.existsSync()) {
  133. _logger.warning("File was not downloaded correctly.");
  134. return null;
  135. }
  136. _logger.info("File downloaded: " + file.uploadedFileID.toString());
  137. _logger.info("Download speed: " +
  138. (io.File(encryptedFilePath).lengthSync() /
  139. (DateTime.now().millisecondsSinceEpoch - startTime))
  140. .toString() +
  141. "kBps");
  142. await CryptoUtil.decryptFile(encryptedFilePath, decryptedFilePath,
  143. Sodium.base642bin(file.fileDecryptionHeader), decryptFileKey(file));
  144. _logger.info("File decrypted: " + file.uploadedFileID.toString());
  145. encryptedFile.deleteSync();
  146. var fileExtension = extension(file.title).substring(1).toLowerCase();
  147. var outputFile = decryptedFile;
  148. if (io.Platform.isAndroid && fileExtension == "heic") {
  149. outputFile = await FlutterImageCompress.compressAndGetFile(
  150. decryptedFilePath,
  151. decryptedFilePath + ".jpg",
  152. keepExif: true,
  153. );
  154. decryptedFile.deleteSync();
  155. }
  156. final cachedFile = await cacheManager.putFile(
  157. file.getDownloadUrl(),
  158. outputFile.readAsBytesSync(),
  159. eTag: file.getDownloadUrl(),
  160. maxAge: Duration(days: 365),
  161. fileExtension: fileExtension,
  162. );
  163. outputFile.deleteSync();
  164. fileDownloadsInProgress.remove(file.uploadedFileID);
  165. return cachedFile;
  166. }).catchError((e) {
  167. fileDownloadsInProgress.remove(file.uploadedFileID);
  168. });
  169. }
  170. Uint8List decryptFileKey(ente.File file) {
  171. final encryptedKey = Sodium.base642bin(file.encryptedKey);
  172. final nonce = Sodium.base642bin(file.keyDecryptionNonce);
  173. final collectionKey =
  174. CollectionsService.instance.getCollectionKey(file.collectionID);
  175. return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
  176. }
  177. Future<Uint8List> compressThumbnail(Uint8List thumbnail) {
  178. return FlutterImageCompress.compressWithList(
  179. thumbnail,
  180. minHeight: COMPRESSED_THUMBNAIL_RESOLUTION,
  181. minWidth: COMPRESSED_THUMBNAIL_RESOLUTION,
  182. quality: 25,
  183. );
  184. }
  185. void clearCache(ente.File file) {
  186. if (file.fileType == FileType.video) {
  187. VideoCacheManager.instance.removeFile(file.getDownloadUrl());
  188. } else {
  189. DefaultCacheManager().removeFile(file.getDownloadUrl());
  190. }
  191. final cachedThumbnail = io.File(
  192. Configuration.instance.getThumbnailCacheDirectory() +
  193. "/" +
  194. file.uploadedFileID.toString());
  195. if (cachedThumbnail.existsSync()) {
  196. cachedThumbnail.deleteSync();
  197. }
  198. }