file_util.dart 7.0 KB

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