file_download_util.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // @dart=2.9
  2. import 'dart:io' as io;
  3. import 'dart:typed_data';
  4. import 'package:dio/dio.dart';
  5. import 'package:flutter_sodium/flutter_sodium.dart';
  6. import 'package:logging/logging.dart';
  7. import 'package:photos/core/configuration.dart';
  8. import 'package:photos/core/network.dart';
  9. import 'package:photos/models/file.dart' as ente;
  10. import 'package:photos/services/collections_service.dart';
  11. import 'package:photos/utils/crypto_util.dart';
  12. final _logger = Logger("file_download_util");
  13. Future<io.File> downloadAndDecrypt(
  14. ente.File file, {
  15. ProgressCallback progressCallback,
  16. }) {
  17. _logger.info("Downloading file " + file.uploadedFileID.toString());
  18. final encryptedFilePath = Configuration.instance.getTempDirectory() +
  19. file.generatedID.toString() +
  20. ".encrypted";
  21. final encryptedFile = io.File(encryptedFilePath);
  22. final startTime = DateTime.now().millisecondsSinceEpoch;
  23. return Network.instance
  24. .getDio()
  25. .download(
  26. file.downloadUrl,
  27. encryptedFilePath,
  28. options: Options(
  29. headers: {"X-Auth-Token": Configuration.instance.getToken()},
  30. ),
  31. onReceiveProgress: progressCallback,
  32. )
  33. .then((response) async {
  34. if (response.statusCode != 200) {
  35. _logger.warning("Could not download file: ", response.toString());
  36. return null;
  37. } else if (!encryptedFile.existsSync()) {
  38. _logger.warning("File was not downloaded correctly.");
  39. return null;
  40. }
  41. _logger.info("File downloaded: " + file.uploadedFileID.toString());
  42. _logger.info(
  43. "Download speed: " +
  44. (await io.File(encryptedFilePath).length() /
  45. (DateTime.now().millisecondsSinceEpoch - startTime))
  46. .toString() +
  47. "kBps",
  48. );
  49. final decryptedFilePath = Configuration.instance.getTempDirectory() +
  50. file.generatedID.toString() +
  51. ".decrypted";
  52. final decryptedFile = io.File(decryptedFilePath);
  53. await CryptoUtil.decryptFile(
  54. encryptedFilePath,
  55. decryptedFilePath,
  56. Sodium.base642bin(file.fileDecryptionHeader),
  57. decryptFileKey(file),
  58. );
  59. _logger.info("File decrypted: " + file.uploadedFileID.toString());
  60. await encryptedFile.delete();
  61. return decryptedFile;
  62. });
  63. }
  64. Uint8List decryptFileKey(ente.File file) {
  65. final encryptedKey = Sodium.base642bin(file.encryptedKey);
  66. final nonce = Sodium.base642bin(file.keyDecryptionNonce);
  67. final collectionKey =
  68. CollectionsService.instance.getCollectionKey(file.collectionID);
  69. return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
  70. }