file_util.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import 'dart:io' as io;
  2. import 'dart:typed_data';
  3. import 'package:flutter_sodium/flutter_sodium.dart';
  4. import 'package:logging/logging.dart';
  5. import 'package:path/path.dart';
  6. import 'package:dio/dio.dart';
  7. import 'package:flutter_cache_manager/flutter_cache_manager.dart';
  8. import 'package:flutter_image_compress/flutter_image_compress.dart';
  9. import 'package:photo_manager/photo_manager.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/thumbnail_cache_manager.dart';
  13. import 'package:photos/core/cache/video_cache_manager.dart';
  14. import 'package:photos/core/configuration.dart';
  15. import 'package:photos/core/constants.dart';
  16. import 'package:photos/db/files_db.dart';
  17. import 'package:photos/models/file.dart';
  18. import 'package:photos/models/file_type.dart';
  19. import 'package:photos/services/collections_service.dart';
  20. import 'crypto_util.dart';
  21. final logger = Logger("FileUtil");
  22. Future<void> deleteFiles(List<File> files,
  23. {bool deleteEveryWhere = false}) async {
  24. await PhotoManager.editor
  25. .deleteWithIds(files.map((file) => file.localID).toList());
  26. for (File file in files) {
  27. deleteEveryWhere
  28. ? await FilesDB.instance.markForDeletion(file)
  29. : await FilesDB.instance.delete(file);
  30. }
  31. }
  32. void preloadFile(File file) {
  33. if (file.fileType == FileType.video) {
  34. return;
  35. }
  36. if (file.localID == null) {
  37. // getFileFromServer(file);
  38. } else {
  39. if (FileLruCache.get(file) == null) {
  40. file.getAsset().then((asset) {
  41. asset.file.then((assetFile) {
  42. FileLruCache.put(file, assetFile);
  43. });
  44. });
  45. }
  46. }
  47. }
  48. void preloadLocalFileThumbnail(File file) {
  49. if (file.localID == null ||
  50. ThumbnailLruCache.get(file, THUMBNAIL_SMALL_SIZE) != null) {
  51. return;
  52. }
  53. file.getAsset().then((asset) {
  54. asset
  55. .thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
  56. .then((data) {
  57. ThumbnailLruCache.put(file, THUMBNAIL_SMALL_SIZE, data);
  58. });
  59. });
  60. }
  61. Future<io.File> getNativeFile(File file) async {
  62. if (file.localID == null) {
  63. return getFileFromServer(file);
  64. } else {
  65. return file.getAsset().then((asset) => asset.file);
  66. }
  67. }
  68. Future<Uint8List> getBytes(File file, {int quality = 100}) async {
  69. if (file.localID == null) {
  70. return getFileFromServer(file).then((file) => file.readAsBytesSync());
  71. } else {
  72. return await getBytesFromDisk(file, quality: quality);
  73. }
  74. }
  75. Future<Uint8List> getBytesFromDisk(File file, {int quality = 100}) async {
  76. final originalBytes = (await file.getAsset()).originBytes;
  77. if (extension(file.title) == ".HEIC" || quality != 100) {
  78. return originalBytes.then((bytes) {
  79. return FlutterImageCompress.compressWithList(bytes, quality: quality)
  80. .then((converted) {
  81. return Uint8List.fromList(converted);
  82. });
  83. });
  84. } else {
  85. return originalBytes;
  86. }
  87. }
  88. final Map<int, Future<io.File>> downloadsInProgress =
  89. Map<int, Future<io.File>>();
  90. Future<io.File> getFileFromServer(File file,
  91. {ProgressCallback progressCallback}) async {
  92. final cacheManager = file.fileType == FileType.video
  93. ? VideoCacheManager()
  94. : DefaultCacheManager();
  95. if (!file.isEncrypted) {
  96. return cacheManager.getSingleFile(file.getDownloadUrl());
  97. } else {
  98. return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
  99. if (info == null) {
  100. if (!downloadsInProgress.containsKey(file.uploadedFileID)) {
  101. downloadsInProgress[file.uploadedFileID] = _downloadAndDecrypt(
  102. file,
  103. cacheManager,
  104. progressCallback: progressCallback,
  105. );
  106. }
  107. return downloadsInProgress[file.uploadedFileID];
  108. } else {
  109. return info.file;
  110. }
  111. });
  112. }
  113. }
  114. Future<io.File> getThumbnailFromServer(File file) async {
  115. if (!file.isEncrypted) {
  116. return ThumbnailCacheManager()
  117. .getSingleFile(file.getThumbnailUrl())
  118. .then((data) {
  119. ThumbnailFileLruCache.put(file, data);
  120. return data;
  121. });
  122. } else {
  123. return ThumbnailCacheManager()
  124. .getFileFromCache(file.getThumbnailUrl())
  125. .then((info) {
  126. if (info == null) {
  127. return _downloadAndDecryptThumbnail(file).then((data) {
  128. ThumbnailFileLruCache.put(file, data);
  129. return data;
  130. });
  131. } else {
  132. ThumbnailFileLruCache.put(file, info.file);
  133. return info.file;
  134. }
  135. });
  136. }
  137. }
  138. Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
  139. {ProgressCallback progressCallback}) async {
  140. logger.info("Downloading file " + file.uploadedFileID.toString());
  141. final encryptedFilePath = Configuration.instance.getTempDirectory() +
  142. file.generatedID.toString() +
  143. ".encrypted";
  144. final decryptedFilePath = Configuration.instance.getTempDirectory() +
  145. file.generatedID.toString() +
  146. ".decrypted";
  147. final encryptedFile = io.File(encryptedFilePath);
  148. final decryptedFile = io.File(decryptedFilePath);
  149. final startTime = DateTime.now().millisecondsSinceEpoch;
  150. return Dio()
  151. .download(
  152. file.getDownloadUrl(),
  153. encryptedFilePath,
  154. onReceiveProgress: progressCallback,
  155. )
  156. .then((response) async {
  157. if (response.statusCode != 200) {
  158. logger.warning("Could not download file: ", response.toString());
  159. return null;
  160. } else if (!encryptedFile.existsSync()) {
  161. logger.warning("File was not downloaded correctly.");
  162. return null;
  163. }
  164. logger.info("File downloaded: " + file.uploadedFileID.toString());
  165. logger.info("Download speed: " +
  166. (io.File(encryptedFilePath).lengthSync() /
  167. (DateTime.now().millisecondsSinceEpoch - startTime))
  168. .toString() +
  169. "kBps");
  170. await CryptoUtil.decryptFile(encryptedFilePath, decryptedFilePath,
  171. Sodium.base642bin(file.fileDecryptionHeader), decryptFileKey(file));
  172. logger.info("File decrypted: " + file.uploadedFileID.toString());
  173. io.File(encryptedFilePath).deleteSync();
  174. final fileExtension = extension(file.title).substring(1).toLowerCase();
  175. final cachedFile = await cacheManager.putFile(
  176. file.getDownloadUrl(),
  177. decryptedFile.readAsBytesSync(),
  178. eTag: file.getDownloadUrl(),
  179. maxAge: Duration(days: 365),
  180. fileExtension: fileExtension,
  181. );
  182. decryptedFile.deleteSync();
  183. downloadsInProgress.remove(file.uploadedFileID);
  184. return cachedFile;
  185. }).catchError((e) {
  186. downloadsInProgress.remove(file.uploadedFileID);
  187. });
  188. }
  189. Future<io.File> _downloadAndDecryptThumbnail(File file) async {
  190. final temporaryPath = Configuration.instance.getTempDirectory() +
  191. file.generatedID.toString() +
  192. "_thumbnail.decrypted";
  193. return Dio().download(file.getThumbnailUrl(), temporaryPath).then((_) async {
  194. final encryptedFile = io.File(temporaryPath);
  195. final thumbnailDecryptionKey = decryptFileKey(file);
  196. final data = CryptoUtil.decryptChaCha(
  197. encryptedFile.readAsBytesSync(),
  198. thumbnailDecryptionKey,
  199. Sodium.base642bin(file.thumbnailDecryptionHeader),
  200. );
  201. encryptedFile.deleteSync();
  202. return ThumbnailCacheManager().putFile(
  203. file.getThumbnailUrl(),
  204. data,
  205. eTag: file.getThumbnailUrl(),
  206. maxAge: Duration(days: 365),
  207. );
  208. });
  209. }
  210. Uint8List decryptFileKey(File file) {
  211. final encryptedKey = Sodium.base642bin(file.encryptedKey);
  212. final nonce = Sodium.base642bin(file.keyDecryptionNonce);
  213. final collectionKey =
  214. CollectionsService.instance.getCollectionKey(file.collectionID);
  215. return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
  216. }